Real-time graphs on Plotly (Python)

What to introduce in this article

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

What is Plotly

plotly logo Plotly is a library that makes it easy to create nice-looking graphs.

file.png

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. plotly_intraction.gif

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.

Real-time graphs on Plotly

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.

plotly_live_intraction.gif

Create a real-time graph. Interactive operation was possible even with real-time graphs.

Implementation

environment

Write the code on the jupyter notebook.

code

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:

  1. Make a figure widget
  2. Add data to figure widget
  3. Refer to the figure widget to update the data

is.

1. Make a figure widget
# 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. plotly_empty_figure_widget.png

We will update the fig created here in other cells. Updates are reflected in real time.

2. Add data to figure widget
# 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.

plotly_updated_graph.png > The graph you just created will be updated.
3. Refer to the figure widget to update the data
# 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).

Other

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.

plotly_live_intraction.gif

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

Real-time graphs on Plotly (Python)
Python on Windows
twitter on python3
python on mac
[Python] Write multi-line plots on Plotly Express
Python on Windbg
Python conda on cygwin
Install python on WSL
PyOpenGL setup on Python 3
Install Scrapy on python3
Install Python on Mac
Install Python 3 on Mac
Install Python3.4 on CentOS 6.6
Labeled graphs on NetworkX
Installing pandas on python2.6
python basic on windows ②
Install python on windows
Install Python 2.7.3 on CentOS 5.4
build Python on Ubuntu
Install Python 3.3 on Ubuntu 12.04
Install Python 3.4 on Mac
Install Python 3.6 on Docker
Set-enable Python virtualenv on Windows
Set up Python 3.4 on Ubuntu
Install Python 3.8 on RHEL 8 (AppStream)
Use matplotlib on Ubuntu 12 & Python
Install watchdog on Windows + Python 3.3
Python on Ruby and angry Ruby on Python
Install pygame on python3.4 on mac
Python + Kivy development on Windows
Install Python 3.8 on CentOS 7 (SCL)
Plotly Dash on Google Colab
Made python available on macOS
Sphinx-autobuild (0.5.2) on Windows7, Python 3.5.1, Sphinx 1.3.5
Put Python 3.x on Ubuntu
Fastest Python installation on Windows
[Python] Notes on data analysis
Build Python environment on Windows
Run Tensorflow 2.x on Python 3.7
Handling of python on mac
Update python on Mac to 3.7-> 3.8
Install pandas 0.14 on python3.4 [on Mac]
Install OpenCV on Ubuntu + python
Notes on installing Python on Mac
Run Python CGI on CORESERVER
Twitter posts on Python 3 etc.
Run unix command on python
Build python environment on windows
Python --Install MySQLDB on EC2
Introducing TensorFlow on Ubuntu + Python 2.7
Install Python 3.8 on CentOS 8 (AppStream)
I ran python on windows
Broadcast on LINE using python
[Python] [Chainer] [Windows] Install Chainer on Windows
Use Python on Windows (PyCharm)
Notes on installing Python on CentOS
Real-time display of video acquired from webcam on Jupyter notebook (Python3)
docker build python based on alpine
Building a Python environment on Mac
Install Python Pillow on Amazon Linux
Install Python 3.8 on Ubuntu 18.04 (OS standard)