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.
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