On Sun, Feb 27, 2011 at 4:49 PM, David Andrews <irbdavid@gmail.com> wrote:
Hi All,

I'm looking for some suggestions about two problems:

1) I'm converting some figure generating code from IDL into
Python/matplotlib.  Image attached showing this figure.
IDL being a functional programming language for the most part,
creating wrappers around various subroutines is trivial and generally
the simplest way to modify their behavior.
For example, in dealing with phase data (which can take values between
0º and 360º, and are 'wrapped' around this interval, such that 270º +
180º = 90º and so on), I have some stuff in IDL that instead over
simply 'overplotting' some (x,y) data, it will do a quick loop and
instead overplot (x, y + n * 360º) for n = -1, ..., 1 (or some other
number of repetitions, you get the idea).

Now, in matplotlib, while I can do this pretty easily, I suspect there
are better ways?  I suppose I could write a subclass of
matplotlib.axes.Axes for example, that does the 360º repetition itself
across not just the plot() method but for others also?  But
implementing a whole new class for this may be complicated, and I am
sort of lost as to how I would then get that working with the pylab
stateful interface?

I'm reasonably new to OO programming, and I'm still getting my head
round the 'best' way to do things like this.

Alternatively, having a class that describes individual data points, I
could define a plot() method for them?

class MyData():
   plot(self, axes):
       axes.plot(self.x, self.y + n * 360)

But then, that seems to 'break' some rules, as I don't see much
matplotlib code in which you do 'data.plot()' as opposed to
'axes.plot()' - the order seems wrong?

2) Somewhat similar to the first question.  The figure includes (at
the top) some ancillary data (showing lengths of orbit and year
numbers).  In IDL its done simply by filling polygons in normal / page
coordinates - but again, I think it could be better done using OO
somehow?  Effectively, that top row could be thought of as a separate
subplot.  What would be the efficient / sensible / pythonic way to go
about reproducing this. Another subclass of Axes?

Many thanks,



Generally speaking, if your first thought is "Should I subclass the Axes class?" then you might need to take a second look at what matplotlib has to offer out of the box.  Granted, the graph you wish to duplicate is very complex, but let us break it down into various components.

First, you want multiple subplots to appear vertically "stacked" and share the same x-axis.  Here is an example of how to do that:


(Note that I personally advocate against the "from pylab import *" code style, and this example could easily be redone from the pyplot interface instead.)

Here is another example where the person used LineCollections with defined offsets.  This has the advantage of using a single axes object, but might be difficult to handle the y-axis.


To have multiple x-axis tick labels for a common y-axis is a concept called twiny.  The following is an example of doing twinx (multiple y-axis tick labels for a common x-axis), but the concept is the same:


This is another example showing a different way of doing that:


As for some of the markings around the graph, I am not entirely certain how to implement that.  I will leave that for others to suggest ideas for.

I hope this is helpful!
Ben Root