Python Forum
Generate matplot animation on a link
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generate matplot animation on a link
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Matplot / numpy noisy data problem the57chambers 1 693 Feb-09-2023, 03:27 AM
Last Post: deanhystad
  Error when running a matplot lib example aurelius_nero 3 6,864 Apr-24-2022, 01:24 PM
Last Post: Axel_Erfurt
  How to save Matplot chart to temp file? Morkus 2 4,514 Jun-12-2021, 10:52 AM
Last Post: Morkus
  remove weekends from matplot mr_byte31 3 4,486 Aug-10-2019, 08:37 AM
Last Post: DeaD_EyE
  Cannot close matplot figure landlord1984 5 9,000 May-11-2018, 04:42 PM
Last Post: Aung_Myo_Htut

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020