osdir.com
mailing list archive F.A.Q. -since 2001!



Subject: Draft of 'Getting Started With Matplotlib's OO
Class Library' - msg#00011

List: python.matplotlib.general

Mail Archive Navigation:
by Date: Prev Next Date Index by Thread: Prev Next Thread Index

I took up John's suggestion to 'new users starting on the path to matplotlib OO API enlightenment to make notes and write a tutorial as you go'.

I'm posting it to the list to generate some feedback and hopefully help out any fellow newbies (although I would prefer it to be on a Wiki somewhere).

Feel free to comment....


Robert


==================================================
Getting Started With Matplotlib's OO Class Library
==================================================

Introduction
------------

For those people coming to Matplotlib without any prior experience
of MatLab and who are familiar with the basic concepts of
programming API's and classes, learning to use Matplotlib via the
class library is an excellent choice. The library is well put
together and works very intuitively once a few fundamentals are
grasped.

The advice from John Hunter, the original developer of the library
is 'don't be afraid to open up matplotlib/pylab.py to see how the
pylab interface forwards its calls to the OO layer.' That in
combination with the user's guide, the examples/embedding demos,
and the mailing lists, which are regularly read by many developers
are an excellent way to learn the class library.

Following is a brief introduction to using the class library,
developed as I came to grips with how to produce my first graphs.

Classes/Terms
-------------

FigureCanvas - is primarily a container class to hold the Figure
instance, an approach which enforces a rigid segregation between
the plot elements, and the drawing of those elements. It can be
loosely thought of as 'the place where the ink goes'.

Figure - a container for one or more Axes. It is possible to
create and manage an arbitrary number of figures using the Figure
class. Note also that a figure can have its own line, text and
patch elements, independent of any axes.

Axes - the rectangular area which holds the basic elements (Line2D,
Rectangle, Text, etc) that are generated by the Axes plotting
commands (e.g. the Axes plot, scatter, bar, hist methods). The Figure
methods add_axes and add_subplot are used to create and add an Axes
instance to the Figure. You should not instantiate an Axes instance
yourself, but rather use one of these helper methods.

Line - implemented in the Line2D class, can draw lines(!) with a
variety of styles (solid, dashed, dotted, etc), markers (location
indicators on the line - point, circle, triangle, diamond, etc) and
colours (k or #000000 - black, w or #FFFFFF - white, b or #000099 -
blue, etc)

Text - a class that handles storing and drawing of text in window
or data coordinates. The text can be coloured, rotated, aligned in
various ways relative to the origin point, and have font properties
(weight, style, etc) assigned to it.

Patch - a patch is a two dimensional shape with a separately
specifiable face and edge colour. Specialised patch classes include
circle, rectangle, regular polygon and more.

Ticks - the indicators of where on an axis a particular value
lies. Separate classes exist for the x and y axis ticks, (XTick,
YTick) and each are comprised of the primitive Line2D and Text
instances that make up the tick.

Artist - Everything that draws into a canvas derives from Artist
(Figure, Axes, Axis, Line2D, Rectangle, Text, and more). Some of
these are primitives (Line2D, Rectangle, Text, Circle, etc) in that
they do not contain any other Artists, some are simple composites,
e.g. XTick which is mad up of a couple of Line2D and Text instances
(upper and lower tick lines and labels), and some are complex, e.g.
and Axes or a Figure, which contain both composite and primitive
artists.


Techniques
----------

1. Setting up an agg backend canvas:

from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure

fig = Figure()
canvas = FigureCanvas(fig)

2. To set the size of the Figure, use the figsize keyword, which uses
inches as the units:

# this results in a 204x192 pixel png - if output at 100 dpi, using
# canvas.print_figure(.., dpi=100)
fig = Figure(figsize=(2.04,1.92))
canvas = FigureCanvas(fig)

3. To add a single subplot:

# The 111 specifies 1 row, 1 column on subplot #1
ax = fig.add_subplot(111)

4. To change the axes size and location on construction, e.g to fit
the labels in on a small graph:

ax = fig.add_axes([0.2,0.2,0.5,0.7])

An existing Axes position/location can be changed by calling
the set_position() method.

5. Adding a graph of some sort is as simple as calling the required
function on the axes instance:

p1 = ax.bar(...) or p1 = ax.plot(...)

6. Setting a label with extra small text:

ax.set_xlabel('Yrs', size='x-small')

7. Setting the graph title:

ax.set_title(A graph title', size='x-small')

8. To enable only the horizontal grid on the major ticks:

ax.grid(False)
ax.yaxis.grid(True, which='major')

9. To only have a left y-axis and a bottom x-axis:

# set the edgecolor and facecolor of the axes rectangle to be the same
frame = ax.axesPatch
frame.set_edgecolor(frame.get_facecolor())
# Specify a line in axes coords to represent the left and bottom axes.
bottom = Line2D([0, 1], [0, 0], transform=ax.transAxes)
left = Line2D([0, 0], [0, 1], transform=ax.transAxes)
ax.add_line(bottom)
ax.add_line(left)

10. To change the size of the tick labels :

labels = ax.get_xticklabels() + ax.get_yticklabels()
for label in labels:
label.set_size('x-small')

11. Removing the black rectangle around an individual bar graph
rectangle (by changing it to the bar colour) :

c = '#7FBFFF'
p1 = ax.bar(ind, values, width, color=c)
for r in p1:
r.set_edgecolor(c)


Putting it together
-------------------

Following is a simple example of how to use the class library
This is examples/agg_oo.py in the matplotlib src distribution, also
found (like all examples) at http://matplotlib.sf.net/examples

#!/usr/bin/env python
"""
A pure OO (look Ma, no pylab!) example using the agg backend
"""
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure

fig = Figure()
canvas = FigureCanvas(fig)
ax = fig.add_subplot(111)
ax.plot([1,2,3])
ax.set_title('hi mom')
ax.grid(True)
ax.set_xlabel('time')
ax.set_ylabel('volts')
canvas.print_figure('test')



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click


Thread at a glance:

Previous Message by Date:

Legend with multiple histograms

Hi everyone Does anyone know how to asign different patches in legend when plotting 2 or more histograms in the same figure. I've tried: legend((patches1,patches2),(hist1,hist2)) , but this gives the same patches in the legend inset. Kristen __________________________________ Do you Yahoo!? Take Yahoo! Mail with you! Get it on your mobile phone. http://mobile.yahoo.com/maildemo ------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click

Next Message by Date:

Re: Legend with multiple histograms

>>>>> "kristen" == kristen kaasbjerg >>>>> <cosbys-/E1597aS9LQAvxtiuMwx3w@xxxxxxxxxxxxxxxx> writes: kristen> Hi everyone Does anyone know how to asign different kristen> patches in legend when plotting 2 or more histograms in kristen> the same figure. I've tried: kristen> legend((patches1,patches2),(hist1,hist2)) , kristen> but this gives the same patches in the legend inset. I'm assuming patches1 and patches2 are the return values from hist, in which case they are each a *list* of patches. What you want to do is pass a *single* item from each of those lists as representative patches. legend( (patches1[0],patches2[0]), ('label1', 'label2') ) Next time if you post a more complete code snippet, I won't have to guess what patches1 and patches2 are! Hope this helps, JDH ------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click

Previous Message by Thread:

Legend with multiple histograms

Hi everyone Does anyone know how to asign different patches in legend when plotting 2 or more histograms in the same figure. I've tried: legend((patches1,patches2),(hist1,hist2)) , but this gives the same patches in the legend inset. Kristen __________________________________ Do you Yahoo!? Take Yahoo! Mail with you! Get it on your mobile phone. http://mobile.yahoo.com/maildemo ------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click

Next Message by Thread:

matplotlib Redhat 9.0 install problems

Hi, I'm new to using matplotlib and am having some problems installing the software package. Basically my setup is: * Redhat 9.0 * gcc 3.3 * Python 2.4 (installed in a non-standard location) * freetype 2.1.9 and numarray 1.2.2 also installed in a non-standard location. Following the instructions on the matplotlib homepage, I: 1. Substitute: 'linux2' : [ with: 'linux2' : [os.environ['ACSROOT'], os.environ['PYTHON_ROOT'] within matplotlib-0.72.1/setupext.py. The ACSROOT environment variable is where numarray is installed and the numarray headers can be found in $ACSROOT/include/numarray/. 2. Set BUILD_AGG=1 3. Run the command 'python setup.py build' which gives output that looks correct until gcc is executed: running build_ext building 'matplotlib._na_transforms' extension creating build/temp.linux-i686-2.4 creating build/temp.linux-i686-2.4/src creating build/temp.linux-i686-2.4/CXX gcc -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -pipe -D_POSIX_THREADS -D_POSIX_THREAD_SAFE_FUNCTIONS -D_REENTRANT -DACE_HAS_AIO_CALLS -fcheck-new -Wall -fPIC -g -DDEBUG -O -DCCS_LIGHT -fPIC -Isrc -I. -I/alma/ACS-4.0/Python/include/python2.4 -c src/_na_transforms.cpp -o build/temp.linux-i686-2.4/src/_na_transforms.o -DNUMARRAY=1 In file included from /alma/ACS-4.0/Python/include/python2.4/Python.h:8, from CXX/Objects.hxx:9, from CXX/Extensions.hxx:18, from src/_transforms.h:12, from src/_na_transforms.cpp:2: /alma/ACS-4.0/Python/include/python2.4/pyconfig.h:835:1: warning: "_POSIX_C_SOURCE" redefined In file included from /alma/ACS-4.0/gnu/include/c++/3.3/i386-redhat-linux/bits/os_defines.h:39, from /alma/ACS-4.0/gnu/include/c++/3.3/i386-redhat-linux/bits/c++config.h:35, from /alma/ACS-4.0/gnu/include/c++/3.3/functional:53, from src/_na_transforms.cpp:1: /usr/include/features.h:131:1: warning: this is the location of the previous definition src/_na_transforms.cpp:6:35: numarray/arrayobject.h: No such file or directory ... Just looking at the output it appears as if the changes made to basedir (i.e., 'linux2') in setupext.py are not having any sort of effect (hence the error message about numarray/arrayobject.h not existing). Is there something blatantly wrong I'm doing? Any help would be greatly appreciated. Thanks in advance, David Fugate ------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
blog comments powered by Disqus

Home | News | Sitemap | FAQ | advertise | OSDir is an Inevitable website. GBiz is too!