How To Pylab
HowToPylab ¶
* http://www.scipy.org/Cookbook/Matplotlib * http://matplotlib.sourceforge.net/users/pyplot_tutorial.html * http://matplotlib.sourceforge.net/users/artists.html
* http://matplotlib.sourceforge.net/contents.html * http://matplotlib.sourceforge.net/users/legend_guide.html
* http://matplotlib.sourceforge.net/users/screenshots.html * http://matplotlib.sourceforge.net/users/mathtext.html#mathtext-tutorial
Colors ¶
Commands which take color arguments can use several formats to specify the colors. For the basic builtin colors, you can use a single letter
- b : blue
- g : green
- r : red
- c : cyan
- m : magenta
- y : yellow
- k : black
- w : white
Gray shades can be given as a string encoding a float in the 0-1 range, e.g.:
color = '0.75'
For a greater range of colors, you have two options. You can specify the color using an html hex string, as in:
color = '#eeefff'
or you can pass an R , G , B tuple, where each of R , G , B are in the range [0,1].
Recipes ¶
1 #!/usr/bin/env python
2 """
3 Example: simple line plot.
4 Show how to make and save a simple line plot with labels, title and grid
5 """
6 import numpy
7 import pylab
8
9 t = numpy.arange(0.0, 1.0+0.01, 0.01)
10 s = numpy.cos(2*2*numpy.pi*t)
11 pylab.plot(t, s)
12
13 pylab.xlabel('time (s)')
14 pylab.ylabel('voltage (mV)')
15 pylab.title('About as simple as it gets, folks')
16 pylab.grid(True)
17 pylab.savefig('simple_plot')
18
19 pylab.show()
Passing curves to plot
This is understood as 1 curve with 4 points:
plt.plot([1,2,3,4], [1,4,9,16], 'ro')
And this is 2 curves of 3 points:
plot([(1, 4), (2, 5), (3, 6)]) ## No X axxis provided!
Enter an X axxis with texts:
pl = pylab.subplot(111) pl.set_xticks([0,1,2]) Out[45]: [<matplotlib.axis.XTick object at 0x94ce2ac>, <matplotlib.axis.XTick object at 0x94d586c>, <matplotlib.axis.XTick object at 0x94d58ac>] pl.set_xticklabels(['a','b','c']) Out[46]: [<matplotlib.text.Text object at 0x94ce62c>, <matplotlib.text.Text object at 0x94d5b6c>, <matplotlib.text.Text object at 0x94d5f4c>] pylab.plot([0,1,2],zip([1,2,3],[4,5,6])) Out[47]: [<matplotlib.lines.Line2D object at 0x94f1c6c>, <matplotlib.lines.Line2D object at 0x94f1eac>] pylab.show()


Plotting several curves
data = [list(zip(*sorted(zip(nwords[3*i:3*i+3],success[3*i:3*i+3]))))+[str(['r','g','b'][i])] for i in [0,1,2] ]
data = data[0]+data[1]+data[2]
pylab.plot(*data)