Apr-23-2020, 07:07 PM
I am trying to read the latest row from a table and plot the graph using animate and matplotlib in python. The table gets updated every 1 second with a new value. I need to simulate the graph by live plotting the values. However, when I use animate function with an interval of 1 second, I get the same value for every interval fetch.
I am adding the code for your reference. Please let me know what I am missing. The same code is working good when I use a flat file instead of MySql table.
I am adding the code for your reference. Please let me know what I am missing. The same code is working good when I use a flat file instead of MySql table.
import matplotlib.pyplot as plt import matplotlib.animation as animation from matplotlib import style import mysql.connector import pandas as pd style.use('fivethirtyeight') mydb = mysql.connector.connect( host="xxxxxxxxxx", user="xxxxx", passwd="xxxxxxx", database="sakila" ) fig = plt.figure(figsize=(8,5)) ax = plt.subplot2grid((1,1), (0,0)) plt.ion() cursor = mydb.cursor() def animate(i): df = pd.read_sql("SELECT * FROM DATA_TEST ORDER BY ID DESC LIMIT 1", mydb) y = df["VALUE"] x = df["ID"] xs = [] ys = [] xs.append(x) ys.append(float(y)*100) ax.clear() ax.plot(xs,ys) ani = animation.FuncAnimation(fig, animate, interval=1000) plt.show()