From: Ariel R. <ar...@be...> - 2010-03-28 19:44:50
|
Hi Chloe, _segmentdata - that's what I was looking for! Thanks a lot also for that bit of code! Cheers - Ariel On Sun, Mar 28, 2010 at 1:53 AM, Chloe Lewis <ch...@be...> wrote: > Like so, not that it couldn't be improved: > > import matplotlib.cm as cm > import matplotlib.colors as colors > import pylab as p > > def rgb_to_dict(value, cbar): > return dict(zip(('red','green','blue','alpha'), cbar(value))) > > def subcolorbar(xmin, xmax, cbar): > '''Returns the part of cbar between xmin, xmax, scaled to 0,1.''' > assert xmin < xmax > assert xmax <=1 > cd = cbar._segmentdata.copy() > colornames = ('red','green','blue') > rgbmin, rgbmax = rgb_to_dict(xmin, cbar), rgb_to_dict(xmax, cbar) > for k in cd: > tmp = [x for x in cd[k] if x[0] >= xmin and x[0] <= xmax] > if tmp == [] or tmp[0][0] > xmin: > tmp = [(xmin, rgbmin[k], rgbmin[k])] + tmp > if tmp == [] or tmp[-1][0] < xmax: > tmp = tmp + [ (xmax,rgbmax[k], rgbmax[k])] > #now scale all this to (0,1) > square = zip(*tmp) > xbreaks = [(x - xmin)/(xmax-xmin) for x in square[0]] > square[0] = xbreaks > tmp = zip(*square) > cd[k] = tmp > return colors.LinearSegmentedColormap('local', cd, N=256) > > if __name__=="__main__": > subset = [.1, .3, .6] > scb = subcolorbar(min(subset), max(subset), cm.jet) > print 'main segments', cm.jet._segmentdata > print 'smaller', scb._segmentdata > p.subplot(121) > p.scatter([1,2,3],[1,2,3],s=49, c = subset, cmap=scb) > p.colorbar() > p.subplot(122) > p.scatter([2,3,4],[2,3,4],s=49, c =[.001, .5, .99], cmap=cm.jet) > p.colorbar() > p.show() > > > > > On Mar 27, 2010, at 11:52 PM, Chloe Lewis wrote: > > To zoom in on the relevant section of a colorbar -- I convinced myself >> once that I'd need an auxiliary function to define a new cdict that >> covers only the current section of the original cdict. (and then >> define a new colorbar from the cdict, and maybe do a little norming of >> the data). >> >> _segmentdata will give you the original cdict for whichever colorbar >> you're using. >> >> Not that I got around to actually doing it! But it would be great for >> paper readability and passing-around of plots. >> >> &C >> >> >> >> On Mar 27, 2010, at 9:24 PM, Ariel Rokem wrote: >> >> Hi Friedrich, >>> >>> Thanks a lot for your response. I think that you are right - using >>> the vmin/vmax args into imshow (as well as into pcolor) does seem to >>> do what I want. Great! >>> >>> The only thing that remains now is to simultaneously stretch the >>> colormap in the image itself to this range, while also restricting >>> the range of the colorbar which is displayed, to only the part of >>> the colormap which actually has values (in the attached .png, I only >>> want values between 0 and ~0.33 to appear in the colorbar, not from >>> negative -0.33 to +0.33). >>> >>> Does anyone know how to do that? >>> >>> Thanks again - >>> >>> Ariel >>> >>> On Sat, Mar 27, 2010 at 3:29 PM, Friedrich Romstedt < >>> fri...@gm... >>> >>>> wrote: >>>> >>> 2010/3/27 Ariel Rokem <ar...@be...>: >>> >>>> I am trying to make a color-map which will respond to the range of >>>> >>> values in >>> >>>> the data itself. That is - I want to take one of the mpl colormaps >>>> >>> and use >>> >>>> parts of it, depending on the range of the data. >>>> >>>> In particular, I am interested in using the plt.cm.RdYlBu_r >>>> >>> colormap. If the >>> >>>> data has both negative and positive values, I want 0 to map to the >>>> >>> central >>> >>>> value of this colormap (a pale whitish yellow) and I want negative >>>> >>> values to >>> >>>> be in blue and positive numbers to be in red. Also - I would want >>>> >>> to use the >>> >>>> parts of the colormap that represent how far away the smallest and >>>> >>> largest >>> >>>> values in the data are from 0. So - if my data is in the range >>>> >>> [x1,x2] I >>> >>>> would want to use the part of the colormap in indices >>>> 127-127*abs(x1)/(x2-x1) through 127+127*x2/(x2-x1). If the data only >>>> includes positive numbers, I would want to only use the blue part >>>> >>> of the >>> >>>> colormap and if there are negative numbers, I would want to only >>>> >>> use the red >>> >>>> part of the colormap (in these cases, I would also want to take >>>> >>> only a >>> >>>> portion of the colormap which represents the size of the interval >>>> >>> [x1,x2] >>> >>>> relative to the interval [0,x1] or [x2,0], as the case may be). >>>> >>>> I think that this might be useful when comparing matrices >>>> >>> generated from >>> >>>> different data, but with the same computation, such as correlation >>>> >>> or >>> >>>> coherence (see http://nipy.sourceforge.net/nitime/examples/ >>>> >>> fmri.html to get >>> >>>> an idea of what I mean). >>>> >>> >>> I might miss something important, but why not use pcolor() with kwargs >>> vmin and vmax, >>> >>> http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.pcolor >>> , >>> e.g.: >>> >>> maxval = numpy.abs(C).max() >>> pcolor(C, vmin = -maxval, vmax = maxval) >>> >>> As far as I can judge, this should have the desired effect. >>> >>> Friedrich >>> >>> >>> >>> -- >>> Ariel Rokem >>> Helen Wills Neuroscience Institute >>> University of California, Berkeley >>> http://argentum.ucbso.berkeley.edu/ariel >>> < >>> colorbar >>> .png >>> >>>> >>>> ------------------------------------------------------------------------------ >>> Download Intel® Parallel Studio Eval >>> Try the new software tools for yourself. Speed compiling, find bugs >>> proactively, and fine-tune applications for parallel performance. >>> See why Intel Parallel Studio got high marks during beta. >>> >>> http://p.sf.net/sfu/intel-sw-dev_______________________________________________ >>> Matplotlib-users mailing list >>> Mat...@li... >>> https://lists.sourceforge.net/lists/listinfo/matplotlib-users >>> >> >> >> >> ------------------------------------------------------------------------------ >> Download Intel® Parallel Studio Eval >> Try the new software tools for yourself. Speed compiling, find bugs >> proactively, and fine-tune applications for parallel performance. >> See why Intel Parallel Studio got high marks during beta. >> http://p.sf.net/sfu/intel-sw-dev >> _______________________________________________ >> Matplotlib-users mailing list >> Mat...@li... >> https://lists.sourceforge.net/lists/listinfo/matplotlib-users >> > > -- Ariel Rokem Helen Wills Neuroscience Institute University of California, Berkeley http://argentum.ucbso.berkeley.edu/ariel |