Reputation: 91
I'm making an oscilloscope with python and Arduino UNO. And I'm having two strange problems. First, I would like to emphasize that my code works, but I want to understand why these problems happen.
Firstly, if I try to title my figure, my code stops working. I've commented and ended with 3 question marks ???
the three lines concerned.
See code: My second question is why the buttons are not colorful (they are all black) and not clickable. See image
import serial
import numpy as np
import matplotlib.pyplot as plt
from drawnow import *
voltsent = [] #le signal reçu en chaine de caractères
signalsentfloat = [] #le signal reçu converti en float
ser = serial.Serial("com9",115200)
#affichage interactive des données
plt.ion()
cnt = 0
def figuresignal(): #fonction qui affiche le siganl analogue
#fig = plt.figure() ???
plt.grid(True)
#plt.legend(loc='upper left')
plt.subplot(2,1,1)
#fig.suptitle('Visualisation de signaux générés par le GBF') ???
plt.ylabel('Signal')
plt.plot(voltsent, '.')
plt.subplot(2,1,2)
plt.ylabel('Fourrier Transform')
plt.plot(voltsentFT, '-')
while True: #while loop that loops forever
while(ser.inWaiting()==0) :#wait here until there is a data
pass
signalsent = ser.readline() #read line of text from serial port
signalsentfloat = float(signalsent) #convert it
voltsent.append(signalsentfloat) #append its values to array
voltsentFT = np.fft.fft(voltsent)
drawnow(figuresignal) #call drawnow to update our the graph
#plt.pause(.000001)
cnt = cnt+1
if(cnt>50):
voltsent.pop(0)
Upvotes: 0
Views: 812
Reputation: 91
I've found a solution to my problem in above question by following a tutorial on Toptechboy.com
To give a title to my graph I used the function : plt.title('Le titre ici')
Upvotes: 0
Reputation: 339230
fig = plt.figure()
creates a new figure. So you would end up with a lot of figures, eventually leading the application to crash. What you want instead is to have one single figure, so put fig = plt.figure()
outside the loop.
Since this would still plot a lot of lines on top of each other, the better option is to update the lines.
import serial
import numpy as np
import matplotlib.pyplot as plt
voltsent = [] #le signal reçu en chaine de caractères
signalsentfloat = [] #le signal reçu converti en float
ser = serial.Serial("com9",115200)
#affichage interactive des données
plt.ion()
cnt = 0
fig = plt.figure()
fig.suptitle(u'Visualisation de signaux générés par le GBF')
ax = plt.subplot(2,1,1)
ax.grid(True)
ax.set_ylabel('Signal')
line1, = ax.plot([],".")
ax2 = plt.subplot(2,1,2)
ax2.grid(True)
ax2.set_ylabel('Fourrier Transform')
line2, = ax2.plot([],"-")
def figuresignal(): #fonction qui affiche le siganl analogue
line1.set_data(np.arange(len(voltsent)),voltsent )
line2.set_data(np.arange(len(voltsentFT)),voltsentFT )
while True: #while loop that loops forever
while(ser.inWaiting()==0) :#wait here until there is a data
pass
signalsent = ser.readline() #read line of text from serial port
signalsentfloat = float(signalsent) #convert it
voltsent.append(signalsentfloat) #append its values to array
voltsentFT = np.fft.fft(voltsent)
figuresignal()
plt.pause(.001)
cnt = cnt+1
if(cnt>50):
voltsent.pop(0)
Upvotes: 1