From: Ariel Rokem <arokem@be...>  20100327 05:14:32
Attachments:
Message as HTML

Hi everyone, I am trying to make a colormap 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 127127*abs(x1)/(x2x1) through 127+127*x2/(x2x1). 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). First of all  is this a good idea? Or in other words  is there any reason I am not thinking of why this idea is a really bad idea? Second  the technical questions. I think that I can make this happen by using matplotlib.colors.LinearSegmentedColormap, after fiddling with the values of the colormap a bit (as described above), but in order to do that, I need to know what segmentdata was used in order to generate the original colormap (for example, how many lines did each of the entries in the cdict have? Looking at a plot of the cmap it looks like there must have been 8 or 9 for RdYlBu_r, but I can't be sure). I could analyze it in more detail to get that out empirically, but I am guessing that someone around here might be able to spare me that lunacy (if not others...). Thanks in advance, Ariel  Ariel Rokem Helen Wills Neuroscience Institute University of California, Berkeley http://argentum.ucbso.berkeley.edu/ariel 
From: Brian Blais <bblais@br...>  20100327 12:17:20
Attachments:
Message as HTML

On Mar 27, 2010, at 1:13 , Ariel Rokem wrote: > 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. not sure if this is what you want (I'd never heard of RdYlBu_r...I need to go read up!), but I've used a similar colormap with the code posted below. You might be able to modify it for your case. hope this helps! bb from pylab import * def bluewhitered(a,N=256): bottom = [0, 0, 0.5] botmiddle = [0, 0.5, 1] middle = [1, 1, 1] topmiddle = [1, 0, 0] top = [0.5, 0, 0] lims=[a.min(),a.max()] if lims[0]<0 and lims[1]>0: ratio=abs(lims[0])/(abs(lims[0])+lims[1]) cdict={} cdict['red']=[] cdict['green']=[] cdict['blue']=[] # negative part red=[(0.0, 0.0, 0.0), (ratio/2, 0.0, 0.0), (ratio, 1.0, 1.0)] green=[(0.0, 0.0, 0.0), (ratio/2, 0.5, 0.5), (ratio, 1.0, 1.0)] blue=[(0.0, 0.5, 0.5), (ratio/2, 1, 1), (ratio, 1.0, 1.0)] cdict['red'].extend(red) cdict['green'].extend(green) cdict['blue'].extend(blue) nratio=1(1ratio)/2.0 # positive part red=[(ratio, 1.0, 1.0), (nratio, 1.0, 1.0), (1, 0.5, 0.5)] green=[(ratio, 1.0, 1.0), (nratio, 0., 0.), (1, 0.0, 0.0)] blue=[(ratio, 1., 1.), (nratio, 0, 0), (1, 0, 0)] cdict['red'].extend(red) cdict['green'].extend(green) cdict['blue'].extend(blue) elif lims[0]>=0: # all positive cdict={} cdict['red']=[] cdict['green']=[] cdict['blue']=[] ratio=0.0 nratio=0.5 # positive part red=[(ratio, 1.0, 1.0), (nratio, 1.0, 1.0), (1, 0.5, 0.5)] green=[(ratio, 1.0, 1.0), (nratio, 0., 0.), (1, 0.0, 0.0)] blue=[(ratio, 1., 1.), (nratio, 0, 0), (1, 0, 0)] cdict['red'].extend(red) cdict['green'].extend(green) cdict['blue'].extend(blue) else: # all negative cdict={} cdict['red']=[] cdict['green']=[] cdict['blue']=[] ratio=1.0 # negative part red=[(0.0, 0.0, 0.0), (ratio/2, 0.0, 0.0), (ratio, 1.0, 1.0)] green=[(0.0, 0.0, 0.0), (ratio/2, 0.5, 0.5), (ratio, 1.0, 1.0)] blue=[(0.0, 0.5, 0.5), (ratio/2, 1, 1), (ratio, 1.0, 1.0)] cdict['red'].extend(red) cdict['green'].extend(green) cdict['blue'].extend(blue) my_cmap = matplotlib.colors.LinearSegmentedColormap ('my_colormap',cdict,N) return my_cmap if __name__=="__main__": a=randn(20,20) my_cmap=bluewhitered(a,256) clf() pcolor(a,cmap=my_cmap) colorbar()  Brian Blais bblais@... http://web.bryant.edu/~bblais http://bblais.blogspot.com/ 
From: Ariel Rokem <arokem@be...>  20100327 19:28:25
Attachments:
Message as HTML

Hi Brian, Thanks for the code  this is definitely in the direction of what I want to make! The RdYlBu_r colormap is one of the builtin colormaps available in matplotlib.pyplot.cm (you can see all of them here: http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps). I think that using the builtin colormaps might give nicer transitions between the colors, so instead of transitioning linearily between red and white and white and blue, it transitions in a slightly nonlinear way, along several segments. Compare: plot(plt.cm.RdYlBu_r(arange(256))) with plot(my_cmap(arange(256))) I think that the more nonlinear one might look a little bit nicer (and might be less perceptually misleading in interpreting color differences in the result). But I need to figure out how many segments there are in there. Thanks  Ariel On Sat, Mar 27, 2010 at 4:14 AM, Brian Blais <bblais@...> wrote: > On Mar 27, 2010, at 1:13 , Ariel Rokem wrote: > > 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. > > > not sure if this is what you want (I'd never heard of RdYlBu_r...I need to > go read up!), but I've used a similar colormap with the code posted below. > You might be able to modify it for your case. > > > hope this helps! > > bb > > from pylab import * > > def bluewhitered(a,N=256): > bottom = [0, 0, 0.5] > botmiddle = [0, 0.5, 1] > middle = [1, 1, 1] > topmiddle = [1, 0, 0] > top = [0.5, 0, 0] > > lims=[a.min(),a.max()] > > if lims[0]<0 and lims[1]>0: > ratio=abs(lims[0])/(abs(lims[0])+lims[1]) > > cdict={} > cdict['red']=[] > cdict['green']=[] > cdict['blue']=[] > > # negative part > red=[(0.0, 0.0, 0.0), > (ratio/2, 0.0, 0.0), > (ratio, 1.0, 1.0)] > green=[(0.0, 0.0, 0.0), > (ratio/2, 0.5, 0.5), > (ratio, 1.0, 1.0)] > blue=[(0.0, 0.5, 0.5), > (ratio/2, 1, 1), > (ratio, 1.0, 1.0)] > > cdict['red'].extend(red) > cdict['green'].extend(green) > cdict['blue'].extend(blue) > > nratio=1(1ratio)/2.0 > # positive part > red=[(ratio, 1.0, 1.0), > (nratio, 1.0, 1.0), > (1, 0.5, 0.5)] > green=[(ratio, 1.0, 1.0), > (nratio, 0., 0.), > (1, 0.0, 0.0)] > blue=[(ratio, 1., 1.), > (nratio, 0, 0), > (1, 0, 0)] > > cdict['red'].extend(red) > cdict['green'].extend(green) > cdict['blue'].extend(blue) > > > > > elif lims[0]>=0: # all positive > cdict={} > cdict['red']=[] > cdict['green']=[] > cdict['blue']=[] > > ratio=0.0 > nratio=0.5 > > # positive part > red=[(ratio, 1.0, 1.0), > (nratio, 1.0, 1.0), > (1, 0.5, 0.5)] > green=[(ratio, 1.0, 1.0), > (nratio, 0., 0.), > (1, 0.0, 0.0)] > blue=[(ratio, 1., 1.), > (nratio, 0, 0), > (1, 0, 0)] > > cdict['red'].extend(red) > cdict['green'].extend(green) > cdict['blue'].extend(blue) > > else: # all negative > cdict={} > cdict['red']=[] > cdict['green']=[] > cdict['blue']=[] > > ratio=1.0 > > # negative part > red=[(0.0, 0.0, 0.0), > (ratio/2, 0.0, 0.0), > (ratio, 1.0, 1.0)] > green=[(0.0, 0.0, 0.0), > (ratio/2, 0.5, 0.5), > (ratio, 1.0, 1.0)] > blue=[(0.0, 0.5, 0.5), > (ratio/2, 1, 1), > (ratio, 1.0, 1.0)] > > cdict['red'].extend(red) > cdict['green'].extend(green) > cdict['blue'].extend(blue) > > my_cmap = > matplotlib.colors.LinearSegmentedColormap('my_colormap',cdict,N) > > > return my_cmap > > if __name__=="__main__": > > a=randn(20,20) > my_cmap=bluewhitered(a,256) > > > > clf() > pcolor(a,cmap=my_cmap) > colorbar() > > > > > > > >  > Brian Blais > bblais@... > http://web.bryant.edu/~bblais <http://web.bryant.edu/%7Ebblais>; > http://bblais.blogspot.com/ > > > >  Ariel Rokem Helen Wills Neuroscience Institute University of California, Berkeley http://argentum.ucbso.berkeley.edu/ariel 
From: Friedrich Romstedt <friedrichromstedt@gm...>  20100327 22:29:07

2010/3/27 Ariel Rokem <arokem@...>: > I am trying to make a colormap 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 > 127127*abs(x1)/(x2x1) through 127+127*x2/(x2x1). 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 
From: Ariel Rokem <arokem@be...>  20100328 04:24:43
Attachments:
Message as HTML
colorbar.png

From: Chloe Lewis <chlewis@be...>  20100328 06:52:41

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 passingaround 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 <friedrichromstedt@... > > wrote: > 2010/3/27 Ariel Rokem <arokem@...>: > > I am trying to make a colormap 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 > > 127127*abs(x1)/(x2x1) through 127+127*x2/(x2x1). 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 finetune applications for parallel performance. > See why Intel Parallel Studio got high marks during beta. > http://p.sf.net/sfu/intelswdev_______________________________________________ > Matplotlibusers mailing list > Matplotlibusers@... > https://lists.sourceforge.net/lists/listinfo/matplotlibusers 
From: Chloe Lewis <chlewis@be...>  20100328 08:53:58

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)/(xmaxxmin) 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 passingaround 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 <friedrichromstedt@... >>> wrote: >> 2010/3/27 Ariel Rokem <arokem@...>: >>> I am trying to make a colormap 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 >>> 127127*abs(x1)/(x2x1) through 127+127*x2/(x2x1). 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 finetune applications for parallel performance. >> See why Intel Parallel Studio got high marks during beta. >> http://p.sf.net/sfu/intelswdev_______________________________________________ >> Matplotlibusers mailing list >> Matplotlibusers@... >> https://lists.sourceforge.net/lists/listinfo/matplotlibusers > > >  > Download Intel® Parallel Studio Eval > Try the new software tools for yourself. Speed compiling, find bugs > proactively, and finetune applications for parallel performance. > See why Intel Parallel Studio got high marks during beta. > http://p.sf.net/sfu/intelswdev > _______________________________________________ > Matplotlibusers mailing list > Matplotlibusers@... > https://lists.sourceforge.net/lists/listinfo/matplotlibusers 
From: Ariel Rokem <arokem@be...>  20100328 19:44:50
Attachments:
Message as HTML

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 <chlewis@...> 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)/(xmaxxmin) 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 passingaround 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 < >>> friedrichromstedt@... >>> >>>> wrote: >>>> >>> 2010/3/27 Ariel Rokem <arokem@...>: >>> >>>> I am trying to make a colormap 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 >>>> 127127*abs(x1)/(x2x1) through 127+127*x2/(x2x1). 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 finetune applications for parallel performance. >>> See why Intel Parallel Studio got high marks during beta. >>> >>> http://p.sf.net/sfu/intelswdev_______________________________________________ >>> Matplotlibusers mailing list >>> Matplotlibusers@... >>> https://lists.sourceforge.net/lists/listinfo/matplotlibusers >>> >> >> >> >>  >> Download Intel® Parallel Studio Eval >> Try the new software tools for yourself. Speed compiling, find bugs >> proactively, and finetune applications for parallel performance. >> See why Intel Parallel Studio got high marks during beta. >> http://p.sf.net/sfu/intelswdev >> _______________________________________________ >> Matplotlibusers mailing list >> Matplotlibusers@... >> https://lists.sourceforge.net/lists/listinfo/matplotlibusers >> > >  Ariel Rokem Helen Wills Neuroscience Institute University of California, Berkeley http://argentum.ucbso.berkeley.edu/ariel 
From: Friedrich Romstedt <friedrichromstedt@gm...>  20100328 19:59:21

2010/3/28 Ariel Rokem <arokem@...>: > Hi Chloe, > > _segmentdata  that's what I was looking for! Hmm, much easier would maybe be: colorbar = figure.colorbar(...) colorbar.ax.set_xlim((C.min(), C.max()) # Or .set_ylim() for vertical cbars. I just did a dive into the matplotlib code and docu: http://matplotlib.sourceforge.net/api/colorbar_api.html#matplotlib.colorbar.ColorbarBase It has also the advantage that the xticks represent the correct values. For small C ranges one should consider using a cmap subdivided finer? Friedrich 
From: Chloe Lewis <chlewis@be...>  20100328 20:52:57

That would be a lot nicer, Friedrich; could you share demo code? I can't make the set_ylim work, but I think I'm being clumsy with the object model. E.g., from this: 
From: Friedrich Romstedt <friedrichromstedt@gm...>  20100329 00:34:52
Attachments:
norm.py

2010/3/28 Chloe Lewis <chlewis@...>: > That would be a lot nicer, Friedrich; could you share demo code? I can't > make the set_ylim work, but I think I'm being clumsy with the object model. It seems that I cannot read the sections following after the "From this:" and "I get this:"? But anyway, I solved it for you :) See attached script. colorbar() takes the argument *boundaries*, defining the region to draw. It's code from Alan G Isaac slightly modified and also posted there back, too. You will want to run the code via python i norm.py. For the vmin and vmax, if you like it more you can also pass in norm = matplotlib.colors.Normalize(vmin, vmax) instead. Note that the ticking is a bit weird, there is also a bug in matplotlib I will report on right after this email, whose bugfix you will maybe want to apply to get ticking properly working. When you have insane values for C.min() and C.max() anyway, I'm afraid you have to retick manually with *ticks* to colorbar(). The ticker.MaxNLocator is only used when not using the *boundaries* arg to colorbar(), unfortunately. Otherwise it tries to create maximal many and less than 11 ticks by using the lowest value and an appropriate step in *boundaries*. I think the implementation of ticking is cumbersome and never optimal. Friedrich 
From: Friedrich Romstedt <friedrichromstedt@gm...>  20100329 13:41:39

2010/3/29 Friedrich Romstedt <friedrichromstedt@...>: > Note that the ticking is a bit weird, there is also a bug in > matplotlib I will report on right after this email, whose bugfix you > will maybe want to apply to get ticking properly working. When you > have insane values for C.min() and C.max() anyway, I'm afraid you have > to retick manually with *ticks* to colorbar(). The ticker.MaxNLocator > is only used when not using the *boundaries* arg to colorbar(), > unfortunately. Otherwise it tries to create maximal many and less > than 11 ticks by using the lowest value and an appropriate step in > *boundaries*. I think the implementation of ticking is cumbersome and > never optimal. You can get rid of this night mare by giving the kwarg "ticks = matplotlib.ticker.MaxNLocator()" to fig.colorbar(). Then the *boundaries* aren't used for ticking (but still for plotting). Friedrich 
From: Ariel Rokem <arokem@be...>  20100330 05:00:15
Attachments:
Message as HTML

Hi  I ended up with the code below, using Chloe's previously posted 'subcolormap' and, in order to make the colorbar nicely attached to the main imshow plot, I use make_axes_locatable in order to generate the colorbar axes. I tried it out with a couple of usecases and it seems to do what it is supposed to, (with ticks only for the edges of the range of the data and 0, if that is within that range), but I am not entirely sure. Do you think it works? Cheers, Ariel from mpl_toolkits.axes_grid import make_axes_locatable fig=plt.figure() ax_im = fig.add_subplot(1,1,1) divider = make_axes_locatable(ax_im) ax_cb = divider.new_vertical(size="20%", pad=0.2, pack_start=True) fig.add_axes(ax_cb) #Extract the minimum and maximum values for scaling of the colormap/colorbar: max_val = np.max(m[np.where(m<1)]) min_val = np.min(m) #This makes sure that 0 is always the center of the colormap: if min_val<max_val: ax_max = min_val ax_min = min_val else: ax_max = max_val ax_min = max_val #Keyword args to imshow: kw = {'origin': 'upper', 'interpolation': 'nearest', 'cmap':cmap, 'vmin':ax_min, 'vmax':ax_max} im=ax_im.imshow(m,**kw) #The following produces the colorbar and sets the ticks if colorbar: delta = ax_maxax_min #The size of the entire interval of data min_p = (min_valax_min)/delta max_p = (max_valax_min)/delta print min_p print max_p cnorm = mpl.colors.Normalize(vmin=min_val,vmax=max_val) subcmap = subcolormap(min_p,max_p,cmap) cb = mpl.colorbar.ColorbarBase(ax_cb, cmap=subcmap, orientation='horizontal',norm=cnorm) #Set the ticks  if 0 is in the interval of values, set that, as well #as the maximal and minimal values: if min_val<0: cb.set_ticks([min_val,0,max_val]) cb.set_ticklabels(['%.2f'%min_val,'0','%.2f'%max_val]) #Otherwise  only set the minimal and maximal value: else: cb.set_ticks([min_val,max_val]) cb.set_ticklabels(['%.2f'%min_val,'%.2f'%max_val]) On Mon, Mar 29, 2010 at 6:41 AM, Friedrich Romstedt < friedrichromstedt@...> wrote: > 2010/3/29 Friedrich Romstedt <friedrichromstedt@...>: > > Note that the ticking is a bit weird, there is also a bug in > > matplotlib I will report on right after this email, whose bugfix you > > will maybe want to apply to get ticking properly working. When you > > have insane values for C.min() and C.max() anyway, I'm afraid you have > > to retick manually with *ticks* to colorbar(). The ticker.MaxNLocator > > is only used when not using the *boundaries* arg to colorbar(), > > unfortunately. Otherwise it tries to create maximal many and less > > than 11 ticks by using the lowest value and an appropriate step in > > *boundaries*. I think the implementation of ticking is cumbersome and > > never optimal. > > You can get rid of this night mare by giving the kwarg "ticks = > matplotlib.ticker.MaxNLocator()" to fig.colorbar(). Then the > *boundaries* aren't used for ticking (but still for plotting). > > Friedrich >  Ariel Rokem Helen Wills Neuroscience Institute University of California, Berkeley http://argentum.ucbso.berkeley.edu/ariel 
From: Friedrich Romstedt <friedrichromstedt@gm...>  20100330 13:52:18

2010/3/30 Ariel Rokem <arokem@...>: > I ended up with the code below, using Chloe's previously posted > 'subcolormap' and, in order to make the colorbar nicely attached to the main > imshow plot, I use make_axes_locatable in order to generate the colorbar > axes. I tried it out with a couple of usecases and it seems to do what it > is supposed to, (with ticks only for the edges of the range of the data and > 0, if that is within that range), but I am not entirely sure. Do you think > it works? I think even Chloe would agree that you should avoid the subcolormap() if you can. I tried to create an as minimalistic as possible but working selfcontained example, please find the code also attached as .py file: from matplotlib import pyplot as plt import matplotlib as mpl from mpl_toolkits.axes_grid import make_axes_locatable import numpy as np fig = plt.figure() ax_im = fig.add_subplot(1, 1, 1) divider = make_axes_locatable(ax_im) ax_cb = divider.new_vertical(size = '20%', pad = 0.2, pack_start = True) fig.add_axes(ax_cb) x = np.linspace(5, 5, 101) y = x Z = np.sin(x*y[:,None]).clip(1,10.1) # Leave out if you want: Z += 2 min_val = Z.min() max_val = Z.max() bound = max(np.abs(Z.max()), np.abs(Z.min())) patch = ax_im.imshow(Z, origin = 'upper', interpolation = 'nearest', vmin = bound, vmax = bound) cb = fig.colorbar(patch, cax = ax_cb, orientation = 'horizontal', norm = patch.norm, boundaries = np.linspace(bound, bound, 256), ticks = [min_val, 0, max_val], format = '%.2f') plt.show() Friedrich 
From: Ariel Rokem <arokem@be...>  20100330 14:38:28
Attachments:
Message as HTML

Hi Friedrich, Thanks a lot  very nice! Cheers  Ariel On Tue, Mar 30, 2010 at 6:52 AM, Friedrich Romstedt < friedrichromstedt@...> wrote: > 2010/3/30 Ariel Rokem <arokem@...>: > > I ended up with the code below, using Chloe's previously posted > > 'subcolormap' and, in order to make the colorbar nicely attached to the > main > > imshow plot, I use make_axes_locatable in order to generate the colorbar > > axes. I tried it out with a couple of usecases and it seems to do what > it > > is supposed to, (with ticks only for the edges of the range of the data > and > > 0, if that is within that range), but I am not entirely sure. Do you > think > > it works? > > I think even Chloe would agree that you should avoid the subcolormap() > if you can. I tried to create an as minimalistic as possible but > working selfcontained example, please find the code also attached as > .py file: > > from matplotlib import pyplot as plt > import matplotlib as mpl > from mpl_toolkits.axes_grid import make_axes_locatable > import numpy as np > > fig = plt.figure() > ax_im = fig.add_subplot(1, 1, 1) > divider = make_axes_locatable(ax_im) > ax_cb = divider.new_vertical(size = '20%', pad = 0.2, pack_start = True) > fig.add_axes(ax_cb) > > x = np.linspace(5, 5, 101) > y = x > Z = np.sin(x*y[:,None]).clip(1,10.1) > > # Leave out if you want: > Z += 2 > > min_val = Z.min() > max_val = Z.max() > bound = max(np.abs(Z.max()), np.abs(Z.min())) > > patch = ax_im.imshow(Z, origin = 'upper', interpolation = 'nearest', > vmin = bound, vmax = bound) > > cb = fig.colorbar(patch, cax = ax_cb, orientation = 'horizontal', > norm = patch.norm, > boundaries = np.linspace(bound, bound, 256), > ticks = [min_val, 0, max_val], > format = '%.2f') > > plt.show() > > Friedrich >  Ariel Rokem Helen Wills Neuroscience Institute University of California, Berkeley http://argentum.ucbso.berkeley.edu/ariel 
From: Chloe Lewis <chlewis@be...>  20100330 17:15:57

But this example doesn't solve the problem I was thinking of: it shows lots of colors in the colorbar that aren't used in the plot. &C On Mar 30, 2010, at 6:52 AM, Friedrich Romstedt wrote: > 2010/3/30 Ariel Rokem <arokem@...>: >> I ended up with the code below, using Chloe's previously posted >> 'subcolormap' and, in order to make the colorbar nicely attached to >> the main >> imshow plot, I use make_axes_locatable in order to generate the >> colorbar >> axes. I tried it out with a couple of usecases and it seems to do >> what it >> is supposed to, (with ticks only for the edges of the range of the >> data and >> 0, if that is within that range), but I am not entirely sure. Do >> you think >> it works? > > I think even Chloe would agree that you should avoid the subcolormap() > if you can. I tried to create an as minimalistic as possible but > working selfcontained example, please find the code also attached as > .py file: > > from matplotlib import pyplot as plt > import matplotlib as mpl > from mpl_toolkits.axes_grid import make_axes_locatable > import numpy as np > > fig = plt.figure() > ax_im = fig.add_subplot(1, 1, 1) > divider = make_axes_locatable(ax_im) > ax_cb = divider.new_vertical(size = '20%', pad = 0.2, pack_start = > True) > fig.add_axes(ax_cb) > > x = np.linspace(5, 5, 101) > y = x > Z = np.sin(x*y[:,None]).clip(1,10.1) > > # Leave out if you want: > Z += 2 > > min_val = Z.min() > max_val = Z.max() > bound = max(np.abs(Z.max()), np.abs(Z.min())) > > patch = ax_im.imshow(Z, origin = 'upper', interpolation = 'nearest', > vmin = bound, vmax = bound) > > cb = fig.colorbar(patch, cax = ax_cb, orientation = 'horizontal', > norm = patch.norm, > boundaries = np.linspace(bound, bound, 256), > ticks = [min_val, 0, max_val], > format = '%.2f') > > plt.show() > > Friedrich > <cbar.py><cbar.png> 
From: Ryan May <rmay31@gm...>  20100330 19:32:36
Attachments:
add_colorbar_limits.diff
test_cbar.py

On Tue, Mar 30, 2010 at 11:15 AM, Chloe Lewis <chlewis@...> wrote: > But this example doesn't solve the problem I was thinking of: it shows > lots of colors in the colorbar that aren't used in the plot. Here's a patch (and example) that I've cooked up that adds a colorbar.set_limits() method. It works pretty well, but there's still one issue to be sorted out: Setting the data limits distorts the aspect ratio. I haven't quite figured out how best to make it so that the aspect ratio is handled based on the size in axes coordinates and not based on the data range (or rather, data aspect ratio). (I'd love to hear any ideas the other devs have.) Ryan  Ryan May Graduate Research Assistant School of Meteorology University of Oklahoma 
From: Friedrich Romstedt <friedrichromstedt@gm...>  20100330 21:24:14

2010/3/30 Chloe Lewis <chlewis@...>: > But this example doesn't solve the problem I was thinking of: it shows lots > of colors in the colorbar that aren't used in the plot. I'm so stupid! Here is the correct code. I just interchanged "bounds, bound" with "min_val, max_val" on line 28. The only thing I didn't fix was to exclude the 0.00 from the ticks, but this Ariel already did, so I leave it now like it is. Friedrich 