Our objective with this series of lessons is to plot live data coming from arduino using Python and Matplotlib. We have taken a few lessons to get familiar with Matplotlib, and we have built a circuit to stream live data from arduino to python. We need to install one more library to enable Matplotlib to plot live sensor data in real time. The magic library is called drawnow. The bad news is that this library is hard to install on windows. The good news it that PIP installs it very easy. So, if you have not done so already, you need to go to Python with Arduino LESSON 6 and install PIP. PIP makes it very easy to install drawnow.

# Tag Archives: Matplotlib

# Python with Arduino LESSON 8: Introduction to Graphing with Matplotlib

While it is cool to create 3D visuals using vPython to represent our data coming from arduino, sometimes we want to make more quantitative graphs and charts from the data. To do this, we need to learn how to create graphs in Python. We do this using the library Matplotlib. We learned how to install and download this library in Python with Arduino LESSON 7: If you have not installed the library yet, make sure to go back and do in as shown in LESSON 7.

With the library installed, we are ready to learn Matplotlib. The video takes you through an introductory tutorial with step-by-step instructions. The code below is a sample of how to plot a sin and cos wave. You can watch the video, and then play around with the parameters to become familiar with this library.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import numpy as np #import numpy library import matplotlib.pyplot as plt #import matplotlib x= np.linspace( 0, 2*np.pi, 50)#create your x array y= np.sin(x) #create y array z= np.cos(x) #create z array plt.plot(x,y, 'b-d', linewidth=2, label='sinx') #plot y plt.plot(x,z, 'r-o', linewidth=2, label='cosx') #plot z plt.grid(True) #display background grid plt.axis([0,2*np.pi,-1.5,1.5]) #set range on axis plt.title('My Sin and Cos Waves') #chart title plt.xlabel('Time in Seconds') #label x axis plt.ylabel('My Waves') #label y axis plt.legend() #show legend plt.show() #show the plot |

# Python with Arduino LESSON 7: Installing Matplotlib for Graphing

In this video we provide step-by-step instructions on how to install Matplotlib. Hopefully you have been following our lessons, and have already installed Python 2.7, the 32 bit version. If you stay on the same path as me as far as libraries and software versions, all my tutorials should work easily for you. In order for matplotlib to work, you need to have the numpy library installed. In Python with Arduino LESSON 2 we installed the vPython library. If you installed the vPython library, you already have numpy, as that was part of the vPython installation. If you have not already installed vPython, you should review that lesson now and do that.

Now, to install matplotlib, go to this page:

https://github.com/matplotlib/matplotlib/downloads/

If you are following my lessons and software versions, you will want to install this version, which should be towards the top of the page:

matplotlib-1.2.0.win32-py2.7.exe — Binary installer for 32-bit Windows, built using python.org’s 2.7 and Numpy 1.6.2

When the file downloads, then click on it and then follow the simple installation instructions.

Once it is installed you can test things with this simple program, which should plot a sine wave.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import numpy as np import matplotlib.pyplot as plt x=[] y=[] for i in np.arange(0,2*np.pi,2*np.pi/1000): x.append(i) y.append(np.sin(i)) plt.plot(x,y, 'b-', linewidth=2) plt.grid(True) plt.axis([0,2*np.pi,-1.5,1.5]) plt.title('My Sine Wave') plt.xlabel('Time in Seconds') plt.ylabel('Sin(t)') plt.show() |