## Why (not) choose matplotlib?

After presenting the difference between the nice jquery.flot charts and the ugly matplotlib charts, and how I lost a contract as a freelance with matplotlib, I stated that once you need something a little more complicated than histograms, pie charts and plots, you are stuck. Plus javascript code is bigger and uglier. And if a last argument was needed, you can do more with less, once you know the «

*coup du berger*» (a safe opening in chess).

Plus -

*for*

*former Perl developer as I*- matplotlib as a tremendous advantage: a gallery with actual examples you can copy paste and it works. But in order to re use them, you need to know the standard opening of a plot.

## Forewords

As a former matplot programmer I can ensure, matplotlib is functionnal port of matplot, an old software used in university for playing with discrete series in signal processing.I pointed people to that it was safer to install matplotlib the «packaged» way on your OS. See here : http://matplotlib.sourceforge.net/users/installing.html

Then, one should use

**ipython**that gives a nice feeling of immediateness by rendering on the fly your plots. (ipython has nice features such as completion, syntax highlighting, help)

## Le coup du berger

There are «historical? coding convention» in matplolib, making examples easier to read and modify once you know there is nothing to understand :

from matplotlib import pyplot as plt ### importing pylot is all about initializing the GUI/canvas fig = plt.figure() ### now you have a canvas ax = fig.add_subplot(111) ### at line 1, col 1 , in a canvas made of 1 subplot ax.plot(range(100), label ="optional legend for the plot") ### yes ploting is done here ax.set_xlabel("x is there") ax.set_ylabel("and ofc y is there") plt.title("titles are nice") ### These are optional but so easy plt.show() ### show the result plt.savefig("example.png") ## this is the result saved for this post.Can't say, it is not easy ?

## What is the use of the coup du berger ?

Now you can, not only cut and paste examples, but you can now recognize the pattern and understand what is specific here : http://matplotlib.sourceforge.net/examples/ This part of matplotlib is the pypi or the CPAN or the CTAN of matplotlib. The place where you gather working examples to enhance your experience of matplotlib. At this point you have the starter kit for using matplotlib.

## Extra fun.

### Numpy is great

Numpy are bindings basically to fortran libraries (as a former fortran77 developer I luv it :) ) that gives a little bit of «array langage» paradigm to python. (During the presentation I said it contained numerical recipies, but I was wrong, I said it was a high performance high library for homogeneous arrays, I was right). An «array langage» is simply a language where a variable is an array and for instance*2 * a*will multiply all members of an array by 2.

### Plotting a cardioid, add_subplot

from matplotlib import pyplot as plt import numpy as np from numpy import cos, sin ### sin & cos of numpy works in radian (sin & cos of math in degrees) fig = plt.figure() ax=fig.add_subplot(2,1,1) ## I declare there will be a stack of 2 subplots and I want to play with line 1, col1 a=cos(2.0 * np.array(range(2000))/200.0) ## array of float ax.plot(a, '.-') ## '.-' is about telling the shape of the line I wanna draw ax=fig.add_subplot(2,1,2) ## I tell matplotlib in the stack of the subplot I want another one under the first on the 2nd line b=sin(5.0 * np.array(range(2000))/200.0) ### gruiky implicit cast to float by 5.0 * ax.plot(a, b, label="nice cardioid, ne?") ax.legend() plt.show()

### Playing with dates

**Question**: how do the hell do I play with dates ?

**Answer**: Coup du berger + manual :)

- convert your dates with dates.date2num
- use ax.plot_date
- if lazy use fig.autofmt_xdate

or look the gallery : http://matplotlib.sourceforge.net/examples/pylab_examples/date_demo1.html

### Even more fun

This is

**fun**http://scipy-lectures.github.com/advanced/image_processing/index.html

Et voilĂ : all is said and done, and now you can fly with your own wings :)

The manual is great, the gallery is great, this is all you need to know to have fun.

## No comments:

Post a Comment