[GRASS-dev] Python MatPlotLib histogram example

I spent a little time playing around with the Python statistical graphing library MatPlotLib this evening. I’ve attached a screenshot of a demo histogram and, below, the code needed to create it. This example uses TkInter, but it isn’t much more complicated to put this into wxPython. This seems pretty cool. There are much nicer plots too.

Michael


C. Michael Barton, Professor of Anthropology
Director of Graduate Studies
School of Human Evolution & Social Change
Center for Social Dynamics & Complexity
Arizona State University

Phone: 480-965-6262
Fax: 480-965-7671
www: <www.public.asu.edu/~cmbarton>

======== example code =======

#!/usr/bin/env python
from pylab import *

mu, sigma = 100, 15
x = mu + sigma*randn(10000)

the histogram of the data

n, bins, patches = hist(x, 50, normed=1)
setp(patches, ‘facecolor’, ‘g’, ‘alpha’, 0.75)

add a ‘best fit’ line

y = normpdf( bins, mu, sigma)
l = plot(bins, y, ‘r–’)
setp(l, ‘linewidth’, 1)

xlabel(‘Smarts’)
ylabel(‘Probability’)
title(r’$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$')
axis([40, 160, 0, 0.03])
grid(True)

#savefig(‘histogram_demo’,dpi=72)
show()

===== graph generated by this code ===========

On 30/01/08 06:55, Michael Barton wrote:

I spent a little time playing around with the Python statistical graphing library MatPlotLib this evening. I've attached a screenshot of a demo histogram and, below, the code needed to create it. This example uses TkInter, but it isn't much more complicated to put this into wxPython. This seems pretty cool. There are much nicer plots too.

Great !

Could we try to make it as a general rule for such tasks (e.g. I'm also thinking about the profiler...) we isolate the code of the actual drawing function to make it callable from the GUI and the command line ?

Glynn, will what you have in mind for the new display system also allow this for things like more sophisticated scalebar and legend placement from the command line on the basis of the python code used in the GUI ?

Moritz

Moritz Lennert wrote:

> I spent a little time playing around with the Python statistical
> graphing library MatPlotLib this evening. I've attached a screenshot of
> a demo histogram and, below, the code needed to create it. This example
> uses TkInter, but it isn't much more complicated to put this into
> wxPython. This seems pretty cool. There are much nicer plots too.

Great !

Could we try to make it as a general rule for such tasks (e.g. I'm also
thinking about the profiler...) we isolate the code of the actual
drawing function to make it callable from the GUI and the command line ?

Definitely. There's no good reason why such functionality should only
be available on desktop systems.

Unfortunately, although MatPlotLib isn't limited to GUI use, it isn't
going to integrate well with the GRASS display system. E.g. there
doesn't appear to be any practical way to make it send its output to
"wherever d.* commands send their output". So, non-GUI tasks will need
to manually integrate anything done using MatPlotLib with the output
from d.* commands.

Glynn, will what you have in mind for the new display system also allow
this for things like more sophisticated scalebar and legend placement
from the command line on the basis of the python code used in the GUI ?

The display system operates at the level of lines, polygons, text,
etc. If you want an axis with labelled ticks, you draw a big line for
the axis, lots of little lines for the ticks, text for the labels,
etc. That's the job of display modules such as d.histogram; the
display system just provides the raw primitives.

Axes, scales, etc are probably useful to enough different tasks that
it may be worth having library functions for them, but that would be
above the level of the display system itself.

--
Glynn Clements <glynn@gclements.plus.com>