PythonMatplotlibCh10_6

Download Report

Transcript PythonMatplotlibCh10_6

PYTHON
PLOTTING CURVES
CHAPTER 10_5
FROM
THINK PYTHON
HOW TO THINK LIKE A COMPUTER SCIENTIST
MATPLOTLIB
matplotlib.pyplot is 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.pyplot import *
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()
Text Properties
2d Line 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.
the csv extension is required
x=-10
x_values=[]
file=open('data.csv','w')
while(x<=10):
150
x_values.append(x)
100
x=x+.5
for x in x_values:
50
print >>file,x,’,’, x**2,',',10*x
file.close()
0
-15
-10
-5
0
-50
You can open data.csv in excel now
-100
-150
5
10
15
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
def readData(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()