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! :) 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
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 https://lists.sourceforge.net/lists/listinfo/matplotlib-users
#!/usr/bin/env python """ This now uses the imshow command instead of pcolor which *is much faster* """ from __future__ import division import numpy as np from matplotlib.pyplot import * from matplotlib.collections import LineCollection # I use if 1 to break up the different regions of code visually if 1: # load the data # data are 256x256 16 bit integers dfile = '../data/s1045.ima' im = np.fromstring(file(dfile, 'rb').read(), np.uint16).astype(float) im.shape = 256, 256 if 1: # plot the MRI in pcolor subplot(221) imshow(im, cmap=cm.jet) axis('off') if 1: # plot the histogram of MRI intensity subplot(222) im = np.ravel(im) im = im[np.nonzero(im)] # ignore the background im = im/(2.0**15) # normalize hist(im, 100) xticks([-1, -.5, 0, .5, 1]) yticks([]) xlabel('intensity') ylabel('MRI density') if 1: # plot the EEG # load the data numSamples, numRows = 800,4 data = np.fromstring(file('../data/eeg.dat', 'rb').read(), float) data.shape = numSamples, numRows t = 10.0 * np.arange(numSamples, dtype=float)/numSamples ticklocs = [] ax = subplot(212) xlim(0,10) xticks(np.arange(10)) dmin = data.min() dmax = data.max() dr = (dmax - dmin)*0.7 # Crowd them a bit. y0 = dmin y1 = (numRows-1) * dr + dmax ylim(y0, y1) segs = [] for i in range(numRows): segs.append(np.hstack((t[:,np.newaxis], data[:,i,np.newaxis]))) ticklocs.append(i*dr) offsets = np.zeros((numRows,2), dtype=float) offsets[:,1] = ticklocs lines = LineCollection(segs, offsets=offsets, transOffset=None, ) ax.add_collection(lines) # set the yticks to use axes coords on the y axis ax.set_yticks(ticklocs) ax.set_yticklabels(['PG3', 'PG5', 'PG7', 'PG9']) xlabel('time (s)') show()
------------------------------------------------------------------------- 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 https://lists.sourceforge.net/lists/listinfo/matplotlib-users