SourceForge logo
SourceForge logo
Menu

matplotlib-users

From: Alejandro U. <ale...@gm...> - 2015年06月03日 16:17:59
Hi, I am trying to get a live scrolling graph built from data send by two
arduino sensors. Although live data is being shown in the graph I am not
able to get it scrolling. The arduino and Python codes I am working with
are included below. I would very much appreciate if you can help me getting
the scrolling graph working.
PYTHON CODE:
import serial # import Serial Library
import numpy # Import numpy
import matplotlib.pyplot as plt #import matplotlib library
from drawnow import *
tempF= []
pressure= []
arduinoData = serial.Serial('com6', 115200) #Creating our serial object
named arduinoData
plt.ion() #Tell matplotlib you want interactive mode to plot live data
cnt=0
def makeFig(): #Create a function that makes our desired plot
 plt.ylim(0,500) #Set y min and max
values
 plt.title('Frequency vs Time') #Plot the title
 plt.grid(True) #Turn the grid on
 plt.ylabel('Frequency (pulses/sec)') #Set
ylabels
 plt.plot(tempF, 'ro-', label='pulses/sec') #plot the temperature
 plt.legend(loc='upper left') #plot the legend
 plt2=plt.twinx() #Create a second y axis
 plt.ylim(0,500) #Set limits of second y axis-
adjust to readings you are getting
 plt2.plot(pressure, 'b^-', label='Pressure (Pa)') #plot pressure data
 plt2.set_ylabel('Pressrue (Pa)') #label second y axis
 plt2.ticklabel_format(useOffset=False) #Force matplotlib to
NOT autoscale y axis
 plt2.legend(loc='upper right') #plot the legend
while True: # While loop that loops forever
 while (arduinoData.inWaiting()==0): #Wait here until there is data
 pass #do nothing
 arduinoString = arduinoData.readline() #read the line of text from the
serial port
 dataArray = arduinoString.split(',') #Split it into an array called
dataArray
 temp = float(dataArray[0]) #Convert first element to floating
number and put in temp
 pres = float(dataArray[1]) #Convert second element to
floating number and put in P
 tempF.append(temp) #Build our tempF array by
appending temp readings
 pressure.append(pres) #Building our pressure
array by appending P readings
 drawnow(makeFig) #Call drawnow to update our live
graph
 plt.pause(.000001) #Pause Briefly. Important to
keep drawnow from crashing
 cnt=cnt+1
 if(cnt>10): #If you have 50 or more points,
delete the first one from the array
 tempF.pop(0) #This allows us to just see the
last 50 data points
 pressure.pop(0)
 ARDUINO CODE:
#include "Wire.h" // imports the wire library for talking over I2C
#include "Adafruit_BMP085.h" // import the Pressure Sensor Library
Adafruit_BMP085 mySensor; // create sensor object called mySensor
float tempC; // Variable for holding temp in C
float tempF; // Variable for holding temp in F
float pressure; //Variable for holding pressure reading
void setup(){
Serial.begin(115200); //turn on serial monitor
mySensor.begin(); //initialize mySensor
}
void loop() {
tempC = mySensor.readTemperature(); // Be sure to declare your variables
tempF = tempC*1.8 + 32.; // Convert degrees C to F
pressure=mySensor.readPressure(); //Read Pressure
Serial.print(tempF);
Serial.print(" , ");
Serial.println(pressure);
delay(250); //Pause between readings.
}
From: Benjamin R. <ben...@ou...> - 2015年06月03日 16:43:23
The plot will autoscale base on the data that has been plotted to it. In
your code, you are repeatedly calling plot(), albeit with a "scrolled"
version of the data, but all of the previous calls to plot() are still
visible. Also, no x-coordinate information is provided to the calls to
plot(), so each new call to plot() only overlays on top of the previous
calls.
I also see that you are using the interactive mode. This isn't really
necessary. I would suggest reading through some of the animation examples
to see how to automatically update your plot:
http://matplotlib.org/examples/animation/index.html . I would particularly
point out the "animate_decay" example. While it isn't a scrolling example,
you can see how to update an existing plot with new data from a generator.
It would then just be a matter of updating the x-limits for each update.
I hope that helps!
Ben Root
On Wed, Jun 3, 2015 at 12:17 PM, Alejandro Ureta <
ale...@gm...> wrote:
> Hi, I am trying to get a live scrolling graph built from data send by two
> arduino sensors. Although live data is being shown in the graph I am not
> able to get it scrolling. The arduino and Python codes I am working with
> are included below. I would very much appreciate if you can help me getting
> the scrolling graph working.
>
>
>
> PYTHON CODE:
>
> import serial # import Serial Library
>
> import numpy # Import numpy
>
> import matplotlib.pyplot as plt #import matplotlib library
>
> from drawnow import *
>
>
>
> tempF= []
>
> pressure= []
>
>
>
> arduinoData = serial.Serial('com6', 115200) #Creating our serial object
> named arduinoData
>
> plt.ion() #Tell matplotlib you want interactive mode to plot live data
>
> cnt=0
>
>
>
> def makeFig(): #Create a function that makes our desired plot
>
> plt.ylim(0,500) #Set y min and max
> values
>
> plt.title('Frequency vs Time') #Plot the title
>
> plt.grid(True) #Turn the grid on
>
> plt.ylabel('Frequency (pulses/sec)') #Set
> ylabels
>
> plt.plot(tempF, 'ro-', label='pulses/sec') #plot the temperature
>
> plt.legend(loc='upper left') #plot the legend
>
>
>
>
>
> plt2=plt.twinx() #Create a second y axis
>
> plt.ylim(0,500) #Set limits of second y
> axis- adjust to readings you are getting
>
> plt2.plot(pressure, 'b^-', label='Pressure (Pa)') #plot pressure data
>
> plt2.set_ylabel('Pressrue (Pa)') #label second y
> axis
>
> plt2.ticklabel_format(useOffset=False) #Force matplotlib to
> NOT autoscale y axis
>
> plt2.legend(loc='upper right') #plot the legend
>
>
>
>
>
> while True: # While loop that loops forever
>
> while (arduinoData.inWaiting()==0): #Wait here until there is data
>
> pass #do nothing
>
> arduinoString = arduinoData.readline() #read the line of text from the
> serial port
>
> dataArray = arduinoString.split(',') #Split it into an array called
> dataArray
>
> temp = float(dataArray[0]) #Convert first element to
> floating number and put in temp
>
> pres = float(dataArray[1]) #Convert second element to
> floating number and put in P
>
> tempF.append(temp) #Build our tempF array by
> appending temp readings
>
> pressure.append(pres) #Building our pressure
> array by appending P readings
>
> drawnow(makeFig) #Call drawnow to update our
> live graph
>
> plt.pause(.000001) #Pause Briefly. Important to
> keep drawnow from crashing
>
> cnt=cnt+1
>
> if(cnt>10): #If you have 50 or more points,
> delete the first one from the array
>
> tempF.pop(0) #This allows us to just see the
> last 50 data points
>
> pressure.pop(0)
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> ARDUINO CODE:
>
>
>
>
>
> #include "Wire.h" // imports the wire library for talking over I2C
>
> #include "Adafruit_BMP085.h" // import the Pressure Sensor Library
>
> Adafruit_BMP085 mySensor; // create sensor object called mySensor
>
>
>
> float tempC; // Variable for holding temp in C
>
> float tempF; // Variable for holding temp in F
>
> float pressure; //Variable for holding pressure reading
>
>
>
> void setup(){
>
> Serial.begin(115200); //turn on serial monitor
>
> mySensor.begin(); //initialize mySensor
>
> }
>
>
>
> void loop() {
>
> tempC = mySensor.readTemperature(); // Be sure to declare your variables
>
> tempF = tempC*1.8 + 32.; // Convert degrees C to F
>
> pressure=mySensor.readPressure(); //Read Pressure
>
>
>
>
>
> Serial.print(tempF);
>
> Serial.print(" , ");
>
> Serial.println(pressure);
>
> delay(250); //Pause between readings.
>
> }
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> ------------------------------------------------------------------------------
>
> _______________________________________________
> Matplotlib-users mailing list
> Mat...@li...
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.
Thanks for helping keep SourceForge clean.
X





Briefly describe the problem (required):
Upload screenshot of ad (required):
Select a file, or drag & drop file here.
Screenshot instructions:

Click URL instructions:
Right-click on the ad, choose "Copy Link", then paste here →
(This may not be possible with some types of ads)

More information about our ad policies

Ad destination/click URL:

AltStyle によって変換されたページ (->オリジナル) /