120 likes | 142 Views
Python plotting curves chapter 10_5. From Think Python How to Think Like a Computer Scientist. MatPlotLib.
E N D
Pythonplotting curveschapter 10_5 From Think Python How to Think Like a Computer Scientist
MatPlotLib • matplotlib.pyplotis a collection of command style functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: eg, create a figure, create a plotting area in a figure, plot some lines in a plotting area, decorate the plot with labels, etc.... import matplotlib.pyplot as plt plt.plot([1,2,3,4]) plt.ylabel('some numbers') plt.show() from matplotlib.pyplotimport * plot([1,2,3,4]) ylabel('some numbers') show() Auto generates x values here!
PLOT • plot()is a versatile command, and will take an arbitrary number of arguments. For example, to plot x versus y, you can issue the command: • plt.plot([1,2,3,4], [1,4,9,16]) • For every x, y pair of arguments, there is an optional third argument which is the format string that indicates the color and line type of the plot. • import matplotlib.pyplot as plt • plt.plot([1,2,3,4], [1,4,9,16], 'ro') • plt.axis([0, 6, 0, 20]) • plt.show() red circle
Line and Text Properties • import matplotlib.pyplot as plt • plt.plot([1,2,3,4], [1,4,9,16], linewidth=8.0 ) #thick line • plt.axis([0, 6, 0, 20]) #sets y axis values • plt.xlabel('This is the x axis', fontsize=14,color = 'red') • plt.ylabel('This is the y axis') • plt.title('Our Example Graph') • plt.show() 2d Line Properties Text Properties
Two graphs • import matplotlib.pyplot as plt • # plot graphs • plt.plot([1,2,3,4,5], [1,4,9,16,25], linewidth = 4.0 , color='blue') #thick line • plt.plot([1,2,3,4,5],[1,2,3,4,5], linewidth = 4.0, color='red') • plt.axis([0, 6, 0, 30]) #sets y axis values • plt.xlabel('This is the x axis', fontsize = 14, color = 'green') • plt.ylabel('This is the y axis', fontsize = 14, color = 'green') • plt.title('Our Example with two Graphs', fontsize = 18, color = 'blue') • plt.show()
CSV Files • Comma separated value files are very popular and it turns out that python can easily process these guys. These are quite simple involving nothing more than data separated by commas. • Normally each line of a csv file has the same number of values listed. Spaces are allowed. • 12, 34, 54, 6, 5 • 5,43,77,2,33 • 78,12,8,4, 23 • and so on. We normally read each line of the file and split it up using the commas as an indication where to separate the values.
Example using Split • Suppose the past slides data is in file data.txt. The following program will read in each line and print it out separated by spaces (just to show it works) • file = open('data.txt','r') • for line in file: • nums= line.split(',') # nums is now a list of the values • for v in nums: • print v, OUTPUT: 12 34 54 6 5 5 43 77 2 33 78 12 8 4 23
CSV Files and EXCEl • Excel knows about CSV files if .csv is used as the extension • Suppose we create a csv file using python. • x=-10 • x_values=[] • file=open('data.csv','w') • while(x<=10): • x_values.append(x) • x=x+.5 • for x in x_values: • print >>file,x,’,’, x**2,',',10*x • file.close() • You can open data.csv in excel now the csv extension is required
Same thing in Python • import matplotlib.pyplot as plt • x=-10 • x_values=[] • file=open('data.csv','w') • while(x<=10): • x_values.append(x) • x=x+.5 • y1_value=[] • y2_value=[] • for x in x_values: • y1_value.append(x**2) • y2_value.append(10*x) • plt.plot(x_values,y1_value) • plt.plot(x_values,y2_value) • plt.show() • file.close()
Reading EEG data • First lets look at eegdata as generated by a 14 channel emotive headset. This device creates a csv file that has quite a few values per row. We are only interested in the first 16 values. You can look at the .csv file using either excel or a pure text editor or both. Excel will automatically load the .csv file into columns using the , as a separator. • Let do so in class today.
Here is the code to read one column only and graph. • from pylab import plot, show, title, xlabel, figure • defreadData(fp,col): • values=[] • fp.readline() #skip the first line • for line in fp: • #extract column col. Col 2 is the first real one • row=line.split(",") • values.append( float(row[col])) • return values
Continued • filename='eegdata.csv' • fp = open(filename) • ch = raw_input('Enter channel you want to display') • ch = int(ch) • fig=figure(1) • fig.suptitle("EEG Channel Display\n",fontsize=24) • y_values=[] • fp.readline() #skip the first line • for line in fp: • #extract column col. Col 2 is the first real one • row=line.split(",") • y_values.append( float(row[ch])) • title('Channel '+str(ch)) • xlabel('Time(1/128 of a sec)') • plot(y_values[0:1100]) • show()