Matplotlib/pylab
The following stuff are based on Matplotlib’s User’s Guide (pdf)
Using ipython to start: ipython -pylab
Superimpose/Modify(Replace) mode:
matplotlib (and matlab) have a hold state. When hold is on, subsequent plotting commands are superimposed over previous commands. When hold is off, the plot is cleared with every plotting command. This is controlled by the hold command, which can be called like: hold(True) or hold(False)
Setting line properties:
You can also call Line2D methods directly. The return value of plot is a sequence of matplotlib.lines.Line2D
instances. Note in the example below, I use tuple unpacking with the “,” to extract the first element of the
sequence as line: line, = plot(t, s1), line.setmarkersize(15) Note, however, that we haven’t issued any pylab commands after the initial plot command so the figure will not be redrawn even though interactive mode is set. To trigger a redraw, you can simply resize the figure window a little or call the draw method.
set( lines , markersize=15, marker=’d’ , markerfacecolor=’g’ , markeredgecolor=’r’ )set is wrong, it should be setp (at least in ipython shell)
Abbreviation Fullname
aa antialiased
c color
ls linestyle
lw linewidth
mec markeredgecolor
mew markeredgewidth
mfc markerfacecolor
ms markersize
Save and Load data:
Suppose you have an ASCII file of measured times and voltages like so
0.0000 0.4911
0.0500 0.5012
0.1000 0.7236
0.1500 1.1756
… and so on
You can load that data into an array X with the load command. The shape of X is numSamples rows by 2
columns, with the first column containing the time points and the second column containing the measured
voltages. You can use numerix array indexing to extract the two columns into the 1D arrays t and s
X = load( ’../ data / ascii_data . dat ’ )
t = X [ : , 0 ] # t h e f i r s t column
s = X [ : , 1 ] # t h e s e c o n d row
plot ( t , s , ’o ’ )t = arange(0.0 , 1.0 , 0.05 )
s = sin ( 2 ∗ p i ∗ t ) + 0.5 ∗ rand ( len ( t ) )
X = zeros(( len ( t ) ,2) , Float )
X[ : , 0 ] = t
X[ : , 1 ] = s
save( ’../ data / ascii_data . dat ’ , X)
Text & labels:
# subscripts , superscripts and groups with {} are supported
xlabel ( ’$\Delta_i ^ j$ ’ , fontsize=’x-large ’ )
ylabel ( ’$\Delta_ {i +1}^ j$ ’ , fontsize=’x-large ’ )setp(text_instance, position=(1.1995,1.7295))
Interactive mode:
The pylab interface provides 4 commands that are useful for interactive control. Note again that the interactive
setting primarily controls whether the figure is redrawn with each plotting command. isinteractive
returns the interactive setting, ion turns interactive on, ioff turns it off, and draw forces a redraw of the
entire figure. Thus when working with a big figure in which drawing is expensive, you may want to turn
matplotlib’s interactive setting off temporarily to avoid the performance hit
When interactive: True, the figure will be redrawn with each command. When interactive : False, the figure will be drawn only when there is a call to show or savefig.
Linear Regression:
m, b = p o l y f i t ( x , y , 1 )
p l o t ( x , y , ’bo ’ , x , m∗x+b , ’-k ’ , l i n e w i d t h =2)
y l a b e l ( ’ regression ’ )
g r i d ( True )
Post a Comment