Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to change graph after every second in python

import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import tmp102

# Create figure for plotting
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
xs = []
ys = []

# Initialize communication with TMP102
tmp102.init()

# This function is called periodically from FuncAnimation
def animate(i, xs, ys):

    # Read temperature (Celsius) from TMP102
    temp_c = round(tmp102.read_temp(), 2)

    # Add x and y to lists
    xs.append(dt.datetime.now().strftime('%H:%M:%S.%f'))
    ys.append(temp_c)

    # Limit x and y lists to 20 items
    xs = xs[-20:]
    ys = ys[-20:]

    # Draw x and y lists
    ax.clear()
    ax.plot(xs, ys)

    # Format plot
    plt.xticks(rotation=45, ha='right')
    plt.subplots_adjust(bottom=0.30)
    plt.title('TMP102 Temperature over Time')
    plt.ylabel('Temperature (deg C)')

# Set up plot to call animate() function periodically
ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys), interval=1000)
plt.show()
Comment

PREVIOUS NEXT
Code Example
Python :: How to Use the abs() Function in Python? A Syntax Breakdown for Beginners 
Python :: the requested url was not found on the server. flask 
Python :: python google translator 
Python :: python setup specify c++ version 
Python :: filter parent based on related child name values 
Python :: some problem occurred shows payubiz 
Python :: custom_settings in scrpay 
Python :: captcha.image install in python 
Python :: python copy file create intermediate directories 
Python :: tkinter radiobutton "bind_all" 
Python :: create canvas for signature flutter 
Python :: monthly precipitation in python 
Python :: qlabel click python 
Python :: ValueError: expected sparse matrix with integer values, found float values 
Python :: como resolver números primos em python 
Python :: créer un dict python avec une liste 
Python :: tuple parameter function python is None 
Python :: username__icontains in django 
Python :: python sha256 crypt decrypt 
Python :: add up all the numbers in each row and output that number output the grand total of all rows 
Python :: Add up the elements in this RDD 
Python :: python compressed for concatenate string 
Python :: scrapy get raw html content of selector innerhtml 
Python :: hovering over canvas item tkinter event 
Python :: convert python to c++ online 
Python :: banner grabber api 
Python :: pythonpath manager spyder 
Python :: query json array 
Python :: numpy euclidean distance matrix broadcasting 
Python :: ensure string length 2 python 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =