• 0

using python matplotlib to read serial data and plot bar graph dynamically


Question

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()

6 answers to this question

Recommended Posts

  • 0

You named your own function animation, which is clobbering over the matplotlib.animation object.

 

e.g.

def animation(i):

...

... = animation.FuncAnimation(fig, animation, ...)

Just switch the call to matplotlib.animation.FuncAnimation(...) instead. That is what I use.

  • 0

okay, that fixed that but now i get "AttributeError: 'BarContainer' object has no attribute 'axes'" but dont see a reference to the line where the error is at. here is my full error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 276, in resize
    self.show()
  File "C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 348, in draw
    FigureCanvasAgg.draw(self)
  File "C:\Python27\lib\site-packages\matplotlib\backends\backend_agg.py", line 451, in draw
    self.figure.draw(self.renderer)
  File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\figure.py", line 1040, in draw
    self.canvas.draw_event(renderer)
  File "C:\Python27\lib\site-packages\matplotlib\backend_bases.py", line 1693, in draw_event
    self.callbacks.process(s, event)
  File "C:\Python27\lib\site-packages\matplotlib\cbook.py", line 527, in process
    proxy(*args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\cbook.py", line 405, in __call__
    return mtd(*args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 832, in _end_redraw
    self._post_draw(None, self._blit)
  File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 778, in _post_draw
    self._blit_draw(self._drawn_artists, self._blit_cache)
  File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 791, in _blit_draw
    if a.axes not in bg_cache:
AttributeError: 'BarContainer' object has no attribute 'axes'
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 = ax.bar([],[])
    humidBar = ax.bar([],[])
    return tempBar, humidBar

def animator(i):
    # get sensor string data and parse
    data1 = serobject.readline().split(" ")
    data2 = serobject.readline().split(" ")

    print(data1)
    print(data2)

    # 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, animator, init_func=init, frames=100, interval=2000, blit=True)
plt.show()
  • 0

I believe it doesn't like your return value for animation() or init(). When I've used animatefunc(), I haven't actually return anything (for example if I'm using ax.pcolormesh). I would try without the returns and see if it works that way. You can also try returning your ax or your plot there. I'm not 100% how the return works, but it looks like it is to tell the animator what part of the plot to update. Perhaps, if you don't return anything it just updates everything?

  • 0

hmm, with the return gone for init function i get "TypeError: 'NoneType' object is not iterable" taking out the return for the animator function does nothing. 

 

returning 'ax' gives me a "TypeError: 'AxesSubplot' object is not iterable" error.

 

returning 'plt' gives me a "TypeError: 'module' object is not iterable" error.

 

This is the guide i followed and it works that way and seemed straigh forward but alas im here

http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/


EDIT: oh, so i changed it so both functions only return tempBar and not both and it works that way but doenst show humidBar at all. I guess it didnt like the double return.  Is there any way to have it update more bars?

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • "A Microsoft spokesperson said that it had been in contact with the court since February “throughout the process that resulted in the disconnection of its sanctioned official from Microsoft services.” The spokesperson added that “at no point did Microsoft cease or suspend its services to the ICC.” Microsoft declined to comment further in response to questions regarding the exact process that led to Khan's email disconnection, and exactly what it meant by “disconnection.” The ICC declined to comment. However, German business magazine WirtschaftsWoche reported Tuesday that Microsoft's lawyers have now reached the view that it merely provides a technical platform and that its customers decide whether to give their employees access to its services. Microsoft would no longer intervene in scenarios similar to the ICC case, WirtschaftsWoche wrote." Source: https://www.politico.eu/articl...ump-tech-icc-amazon-google/ So while they haven't targeted the ICC, they seem to have targeted a sanctioned individual and cut that individual off from their services. At least I cannot read the statement from their spokesperson in any other way. It can however be understood as they did this on behalf of the ICC, which would line up with Smith's statements that they never cut off services to the ICC. But I don't know if there is any confirmed sources back that up.
    • Oof, that's a pretty glaring issue. They seem to be consistently proving that they don't do the necessary QA before sending things out.
    • They have removed th way to download directly the iso of windows from rufus ? i have tried the version 4.9, the button selection isn't a select list to choose download
  • Recent Achievements

    • Week One Done
      patrickft456 earned a badge
      Week One Done
    • One Month Later
      patrickft456 earned a badge
      One Month Later
    • One Month Later
      Jdoe25 earned a badge
      One Month Later
    • Explorer
      Legend20 went up a rank
      Explorer
    • One Month Later
      jezzzy earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      617
    2. 2
      ATLien_0
      281
    3. 3
      +FloatingFatMan
      174
    4. 4
      Michael Scrip
      153
    5. 5
      Steven P.
      125
  • Tell a friend

    Love Neowin? Tell a friend!