in windows I try to run this code. Serial works fine and compass value converted to float matplotlib figure opened but matplotlib window "not responding" not draws anything.
import serial
import numpy
import matplotlib.pyplot as plt
ser = serial.Serial('COM8',9600,timeout=2)
ciz,=plt.plot([],[])
def update_ciz(ciz,newdata):
ciz.set_xdata(numpy.append(ciz.get_xdata(),newdata))
ciz.set_ydata(numpy.append(ciz.get_ydata(),newdata))
plt.draw()
while (True):
line = ser.readline()
k=line.split(":")
temperature=k[0]
pressure= k[1]
attitude=k[2]
realAttitude=k[3]
compass=float(k[4])
gx=k[5]
gy=k[6]
gz=k[7]
ax=k[8]
ay=k[9]
az=k[10]
acond=k[11]
update_ciz(ciz,compass)
In matplotlib you need to use "plt.show()" to display the plot. Since you are using "plt.draw()" to update the plot, you probably also want to use the interactive mode.
Try including this after your "ciz,=plt.plot([],[])" command:
plt.ion()
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With