How To Plot Simple Line Graph On Matplotlib In Real Time When Data Is Appending Every Second In List?
I am using an API which returns a dictionary containing a key whose value keeps changing every second. This is the API call link which returns the data in this form. {'symbol':'ETH
Solution 1:
Animation sets up the basic shape of the graph you want to animate, and then displays it by repeatedly assigning it to the line graph with the animation function. This example shows adding a value to the y-axis and getting a value in a slice and displaying it. 30 frames at 1 second intervals are drawn. You can understand the animation if you check the behavior of the animation while modifying the parameters. I'm in a jupyterlab environment so I have several libraries in place. I have also introduced another library for creating GIF images.
import requests
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from IPython.display import HTML # for jupyter labfrom matplotlib.animation import PillowWriter # Create GIF animation
url = "https://api.binance.com/api/v3/ticker/price?symbol=ETHUSDT"
eth_price =[]
last_price = 10
x = np.arange(31)
fig = plt.figure()
ax = plt.axes(xlim=(0, 31), ylim=(580, 600))
line, = ax.plot([], [], 'r-', marker='.', lw=2)
defani_plot(i):
r = requests.get(url)
response_dict = r.json()
price = float(response_dict['price'])
if price != last_price:
eth_price.append(price)
line.set_data(x[:i], eth_price[:i])
# print(price)return line,
anim = FuncAnimation(fig, ani_plot, frames=31, interval=1000, repeat=False)
plt.show()
anim.save('realtime_plot_ani.gif', writer='pillow')
# jupyter lab #plt.close()#HTML(anim.to_html5_video())
Post a Comment for "How To Plot Simple Line Graph On Matplotlib In Real Time When Data Is Appending Every Second In List?"