Real-Time Line Plot with HoloViews Streaming

  • Share this:

Code introduction


This code defines a function that creates a real-time updating line plot using the HoloViews library. The function accepts x and y data, as well as an update interval. It uses the `streams` module of HoloViews to create a data stream, which is then converted into a line plot using the `map` method. The plot updates in an infinite loop.


Technology Stack : HoloViews, Numpy, Bokeh

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import numpy as np
import holoviews as hv
from holoviews import streams

def plot_streaming_data(x, y, interval=1):
    """
    Plots a streaming line plot using HoloViews.
    """
    stream = streams.Stream(value=np.random.randn(10))
    line_plot = stream.map(lambda data: hv.Curve((data[:, 0], data[:, 1])))
    line_plot.show()
    
    def update_plot():
        new_data = np.random.randn(1, 2)
        stream.value = np.vstack((stream.value, new_data))
        hv.extension('bokeh').show(line_plot)

    # Start the update loop
    import threading
    update_thread = threading.Thread(target=update_plot, daemon=True)
    update_thread.start()

    # Wait for the plot to finish updating
    import time
    time.sleep(10)