Python Forum
Generate matplot animation on a link - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Generate matplot animation on a link (/thread-17177.html)



Generate matplot animation on a link - ambush - Apr-01-2019

Hello friends

In below program , trying to achieve a graph which will have live updates

1.prepared the data
2.saved into db
3.retrieved the last entry

4.plotted live graph using Animate function in matplolib

5.used mpld3 but not so good for plotting on browser

The next thing is to take this to link where users can see the live update.
this should be displayed in browser with automatic zoom.In specific i want to display it using django




import matplotlib.pyplot as plt,mpld3
import matplotlib.animation as animation
import random
import datetime
import numpy 
import sqlite3






fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
Time = []
Temp = []
Temp1 = []
Temp2 = []



def animate(i):
    x = str(datetime.datetime.now().hour) + ":" + str(datetime.datetime.now().minute) + ":" + str(datetime.datetime.now().second) + " " + str(datetime.datetime.now().microsecond) 
    y = numpy.random.randint(1,50)
    z = numpy.random.randint(1,50)
    z1 = y * z
    db = sqlite3.connect('new1db.db')
    cursor = db.cursor()
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS energy32(id INTEGER PRIMARY KEY,time1 DATETIME2(3), current INTEGER,
                           voltage INTEGER, power INTEGER) 
    ''')
    cursor.execute('''INSERT INTO energy32(time1 ,current,voltage,power)
                      VALUES(?,?,?,?)''', (x,y,z,z1))
    cursor.execute('''SELECT power,voltage,current,time1 FROM energy32 ORDER BY id DESC LIMIT 1;''')
    result_set = cursor.fetchall()
    for x_result in result_set:
        print(x_result[0],x_result[1],x_result[2],x_result[3])

    db.commit()
    Time.append(x_result[3])
    Temp.append(int(x_result[1]))  
    Temp1.append(int(x_result[2]))
    Temp2.append(int(x_result[0]))  
    ax1.plot(Temp,Time)
    ax1.plot(Temp1,Time)
    ax1.plot(Temp2,Time)
    print(Temp)
    print(Temp1)
    print(Temp2)

ani = animation.FuncAnimation(fig, animate)
plot.show()
Thanks & Regards
Sai