I'm new to the matplotlib library but saw there is a new animation function for dynamic graphs. In this code I am reading serial data then parsing it and inputting it into a bar graph. This all works fine if there is no dynamic animation. I added the functions to get it dynamic but it keeps erroring out with "AttributeError: 'function' object has no attribute 'FuncAnimation'" and I'm not sure why. I also have another code for the "manual way" to update the graph but I've read this isn't as efficient. This way works though but only for a few updates before the graph freezes. At this point whatever I can get working would be fine. Thank you.
from matplotlib import pyplot as plt
from matplotlib import animation
import serial
# sensor data placeholder
sensorSerial = [" "," "," "," "," "," "," "," "," "]
humidityData = [.01, .01, .01, .01,.01,.01,.01,.01,.01]
tempData = [.01, .01, .01, .01,.01,.01,.01,.01,.01]
# bar details
bar_width = 0.25
opacity = 0.4
# create serial port
serobject = serial.Serial("COM4", 9600)
# create subplot object for 2 y-axis scales
fig, ax = plt.subplots()
# set graph colors
ax.set_axis_bgcolor('black')
fig.patch.set_facecolor('black')
ax.tick_params(axis='x', colors='white', length=0)
# setup axis ticks
ax.set_yticks(range(0,100,20))
ax.set_xticks(range(1,10))
# add x axis labels
ax.set_xticklabels(('Sensor1', 'Sensor2', 'Sensor3', 'Sensor4'))
tempBar = ax.bar([],[])
humidBar = ax.bar([],[])
def init():
tempBar.set_data([],[])
humidBar.set_data([],[])
return tempBar, humidBar
def animation(i):
# get sensor string data and parse
data1 = serobject.readline().split(" ")
data2 = serobject.readline().split(" ")
# add data to arrays
sensorSerial[0] = data1[0]
humidityData[0] = int(data1[1])
tempData[0] = int(data1[2])
sensorSerial[1] = data2[0]
humidityData[1] = int(data2[1])
tempData[1] = int(data2[2])
# create new bar graphs
tempBar = ax.bar([i+0.25 for i in range(1,10)], tempData, bar_width, alpha=opacity,
color='r', label='Temperature', edgecolor='white', linewidth = 3)
humidBar = ax.bar([i+0.5 for i in range(1,10)], humidityData, bar_width, alpha=opacity,
color='b', label='Humidity', edgecolor='white', linewidth = 3)
return tempBar, humidBar
anim = animation.FuncAnimation(fig, animation, init_func=init)
plt.show()
#import libraries
import matplotlib.pyplot as plt
import serial
import time
import matplotlib.animation as animation
# sensor data placeholder
sensorSerial = [" "," "," "," "," "," "," "," "," "]
humidityData = [.01, .01, .01, .01,.01,.01,.01,.01,.01]
tempData = [.01, .01, .01, .01,.01,.01,.01,.01,.01]
# bar details
bar_width = 0.25
opacity = 0.4
# create serial port
serobject = serial.Serial("COM4", 9600)
# create subplot object for 2 y-axis scales
fig, ax = plt.subplots()
# set graph colors
ax.set_axis_bgcolor('black')
fig.patch.set_facecolor('black')
ax.tick_params(axis='x', colors='white', length=0)
# setup axis ticks
ax.set_yticks(range(0,100,20))
ax.set_xticks(range(1,10))
# add x axis labels
ax.set_xticklabels(('Sensor1', 'Sensor2', 'Sensor3', 'Sensor4'))
tempBar = ax.bar([],[])
humidBar = ax.bar([],[])
# dynamic plotting
plt.ion()
# show plot
plt.show()
while(True):
# get sensor string data and parse
data1 = serobject.readline().split(" ")
data2 = serobject.readline().split(" ")
# add data to arrays
sensorSerial[0] = data1[0]
humidityData[0] = int(data1[1])
tempData[0] = int(data1[2])
sensorSerial[1] = data2[0]
humidityData[1] = int(data2[1])
tempData[1] = int(data2[2])
#remove old data from plot
tempBar.remove()
humidBar.remove()
# redraw new data
tempBar = ax.bar([i+0.25 for i in range(1,10)], tempData, bar_width, alpha=opacity,
color='r', label='Temperature', edgecolor='white', linewidth = 3)
humidBar = ax.bar([i+0.25*2 for i in range(1,10)], humidityData, bar_width, alpha=opacity,
color='b', label='Humidity', edgecolor='white', linewidth = 3)
plt.draw()
Question
AdverseDeviant
I'm new to the matplotlib library but saw there is a new animation function for dynamic graphs. In this code I am reading serial data then parsing it and inputting it into a bar graph. This all works fine if there is no dynamic animation. I added the functions to get it dynamic but it keeps erroring out with "AttributeError: 'function' object has no attribute 'FuncAnimation'" and I'm not sure why. I also have another code for the "manual way" to update the graph but I've read this isn't as efficient. This way works though but only for a few updates before the graph freezes. At this point whatever I can get working would be fine. Thank you.
Link to comment
https://www.neowin.net/forum/topic/1207225-using-python-matplotlib-to-read-serial-data-and-plot-bar-graph-dynamically/Share on other sites
6 answers to this question
Recommended Posts