Use the graph creation library Plotly to create graphs that fluctuate in real time. The code has been uploaded to GitHub. https://github.com/bridget462/plotly_live_graph/blob/master/plotly_live_graph.ipynb
Plotly is a library that makes it easy to create nice-looking graphs.
You can create a graph like this with the default settings
Plotly allows you to create interactive graphs. For example, if you hover the mouse, the value of nearby data will be displayed, and you can zoom in / out.
Actually, I wanted to put a graph in Qiita's article, but I could not put a graph in an interactive html file, so I will post a video.
There are many articles on how to create different types of graphs on Plotly, but there are few explanations on how to update graphs in real time, so I will summarize them here. In this article, we will use random numbers to create a graph that reflects changing data in real time. If you replace this random number part with measurement data, you can visualize the data in real time.
Create a real-time graph. Interactive operation was possible even with real-time graphs.
Write the code on the jupyter notebook
.
First, import the library.
# importing libraries
import plotly.graph_objects as go # to make a graph
import numpy as np # to generate test data
The procedure for creating a real-time graph with plotly is roughly:
is.
# creating a figure widget. this will be updated in later cells
fig = go.FigureWidget()
fig
When I run this code on the jupyter notebook
, I see an empty graph in the cell output.
We will update the fig
created here in other cells. Updates are reflected in real time.
# let's add plot. add ; at the end of the code to ommit the output in this cell,
# because the figure widget above will be updated instead
N = 100 # data size
x = np.linspace(0, 20, N)
y = np.random.rand(N)
fig.add_scatter(x=x, y=y);
As an example, let's add data that takes a random number from 0 to 1 on the x-axis and fixed on the y-axis.
This is because the last semicolon ;
in fig.add_scatter (x = x, y = y);
does not duplicate the graph.
If there is no ;
, the graph with the added data will also be displayed below the executed cell, but it is unnecessary because the graph that created the figure widget will be updated.
# to update the plot, take the correct reference by fig.data[i] where i is the order which you added the plot
# in this example, let's modefiy the first scatter plot (fig.data[0])
# getting reference from the graph
first_scatter_plot = fig.data[0]
# assine new random values. as soon as this code is excuted, the graph above will be updated.
first_scatter_plot.y = np.random.rand(N)
It is an image to update the data added in 2. To refer to the data added first with fig.add_scatter ()
, write fig.data [0]
.
If you have added other data, you need to remember the order in which you added it (see fig.data [1]
for the second data added).
Let's update the graph using for minutes to update the graph multiple times. The titles of the cells below are also changed to indicate how many times they have been updated.
# let's update both title and the y data at the same time
FRAME = 1000 # how many times which update the graph
first_scatter_plot = fig.data[0] # getting reference from the graph
for i in range(FRAME):
first_scatter_plot.y = np.random.rand(N) # updating y data
fig.update_layout(title=f'FRAME: {i + 1}');
When executed, you can create a graph that changes like the video at the beginning of the article.
Other codes such as setting the display range of the y-axis and saving the graph are in the notes on GitHub, so please refer to them if you are interested. https://github.com/bridget462/plotly_live_graph/blob/master/plotly_live_graph.ipynb
Recommended Posts