Stacking multiple plots the efficient way

Hi folks,

I have a few plots which I would like to stack in a single figure. When
I first came with this problem (back to matplotlib version 0.93 or
older), the most efficient way (at least in my case) of doing this
consisted in playing with transformations as you can see in the cookbook
(see:
http://www.scipy.org/Cookbook/Matplotlib/MultilinePlots#head-b5f2ad87ab7ec637a0fc63ec85281469d9aeeb46).
Unfortunately, this example (and, of course, the script I wrote back
then) doesn't work anymore with with the newest version of Matplotlib
(0.98).
So I started to hack the "mri_with_eeg.py" example to achieve what I
want (since the way EEGs are stacked!)... with no luck!
I probably don't understand well how the transformation works... That's
why I decided to beg for a friendly help! :slight_smile:

Here is the code from the example (I focused on the lines I haven't
completely figured):

    boxin = Bbox.from_extents(ax.viewLim.x0, -20, ax.viewLim.x1, 20)

    height = ax.bbox.height
    boxout = Bbox.from_extents(ax.bbox.x0, -1.0 * height,
                               ax.bbox.x1, 1.0 * height)

    transOffset = BboxTransformTo(
        Bbox.from_extents(0.0, ax.bbox.y0, 1.0, ax.bbox.y1))

    for i in range(numRows):
        # effectively a copy of transData
        trans = BboxTransform(boxin, boxout)
        offset = (i+1)/(numRows+1)

        trans += Affine2D().translate(*transOffset.transform_point((0,
offset)))

        thisLine = Line2D(
            t, data[:,i]-data[0,i],
            )

        thisLine.set_transform(trans)

        ax.add_line(thisLine)
        ticklocs.append(offset)

ax being a AxesSubplot instance
as far as I understood, 20 and -20 (see line where boxin is defined)
refer to min and max values (on Y axis) to be plotted within the space
allocated to each plot (thanks to the tranformation named trans).
In my case Y values can be much higher (roughly from -1e3 to 5e7), so I
should change those values according to mine in order to get the
tranformation working for me. Obviously something is wrong in my
understanding since it doesn't work!
Hence my first question: Why is it not working?
As a bonus, I also have another question: Is there a "new" (i.e.
provided by the new API) way that can be used to stack plots composed by
many (>5k) points?

Thanks

S�bastien

Sébastien / Seb-bubuntu wrote:

Hi folks,

I have a few plots which I would like to stack in a single figure. When
I first came with this problem (back to matplotlib version 0.93 or
older), the most efficient way (at least in my case) of doing this
consisted in playing with transformations as you can see in the cookbook
(see:
http://www.scipy.org/Cookbook/Matplotlib/MultilinePlots#head-b5f2ad87ab7ec637a0fc63ec85281469d9aeeb46).
Unfortunately, this example (and, of course, the script I wrote back
then) doesn't work anymore with with the newest version of Matplotlib
(0.98).
So I started to hack the "mri_with_eeg.py" example to achieve what I
want (since the way EEGs are stacked!)... with no luck!
I probably don't understand well how the transformation works... That's
why I decided to beg for a friendly help! :slight_smile:

Here is the code from the example (I focused on the lines I haven't
completely figured):

    boxin = Bbox.from_extents(ax.viewLim.x0, -20, ax.viewLim.x1, 20)

    height = ax.bbox.height
    boxout = Bbox.from_extents(ax.bbox.x0, -1.0 * height,
                               ax.bbox.x1, 1.0 * height)

    transOffset = BboxTransformTo(
        Bbox.from_extents(0.0, ax.bbox.y0, 1.0, ax.bbox.y1))

    for i in range(numRows):
        # effectively a copy of transData
        trans = BboxTransform(boxin, boxout)
        offset = (i+1)/(numRows+1)

        trans += Affine2D().translate(*transOffset.transform_point((0,
offset)))

        thisLine = Line2D(
            t, data[:,i]-data[0,i],
            )

        thisLine.set_transform(trans)

        ax.add_line(thisLine)
        ticklocs.append(offset)

ax being a AxesSubplot instance
as far as I understood, 20 and -20 (see line where boxin is defined)
refer to min and max values (on Y axis) to be plotted within the space
allocated to each plot (thanks to the tranformation named trans).
In my case Y values can be much higher (roughly from -1e3 to 5e7), so I
should change those values according to mine in order to get the
tranformation working for me. Obviously something is wrong in my
understanding since it doesn't work!
Hence my first question: Why is it not working?
As a bonus, I also have another question: Is there a "new" (i.e.
provided by the new API) way that can be used to stack plots composed by
many (>5k) points?

I think a LineCollection may provide a very easy way to do what you want, if I understand correctly. I recently changed mri_with_eeg.py to use a LineCollection; maybe you are looking at an older version? Attached is the current version from svn.

Eric

mri_with_eeg.py (1.94 KB)

···

Thanks

Sébastien

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Eric Firing wrote:

S�bastien / Seb-bubuntu wrote:

[snip]

I think a LineCollection may provide a very easy way to do what you
want, if I understand correctly. I recently changed mri_with_eeg.py to
use a LineCollection; maybe you are looking at an older version?
Attached is the current version from svn.

Eric

[snip]

Indeed my Matplotlib version is 0.98.1 (version from Ubuntu repos) so examples may be outdated.
I'll try with your version (which seems much more simple).

Thank you

S�bastien