On Mon, Oct 18, 2010 at 12:54 AM, Benjamin Root <ben.r...@ou.edu> wrote: > On Sun, Oct 17, 2010 at 5:35 PM, braingateway <braingate...@gmail.com> > wrote: >> >> Hi Everyone, >> >> I am trying the matplotlib. I have to say this is a powerful package for >> scientific 2-D plotting. However, I encountered some problems when try >> to generate several colormaps. >> >> for example: >> a=cm.get_cmap('gist_rainbow',256)(range(256)) >> will give a error: >> Traceback (most recent call last): >> File "<pyshell#82>", line 1, in <module> >> a=cm.get_cmap('gist_rainbow',256)(range(256)) >> File "...\site-packages\matplotlib\colors.py", line 498, in __call__ >> if not self._isinit: self._init() >> File "...\site-packages\matplotlib\colors.py", line 649, in _init >> self._segmentdata['red'], self._gamma) >> TypeError: tuple indices must be integers, not str >> >> but other colormaps can actually work: >> a=cm.get_cmap('gist_stern',256)(range(256)) >> >>> a >> array([[ 0. , 0. , 0. , 1. ], >> [ 0.0716923 , 0.00392157, 0.00784314, 1. ], >> [ 0.14338459, 0.00784314, 0.01568627, 1. ], >> ..., >> [ 0.99215686, 0.99215686, 0.97040326, 1. ], >> [ 0.99607843, 0.99607843, 0.98520163, 1. ], >> [ 1. , 1. , 1. , 1. ]]) >> >> I tried all possible colormaps and found out: gist_rainbow, terrain, >> bwr, brg, and seismic will generate the same error, all other colormaps >> are OK. I wonder is this a bug or expected behavior? >> ############################### >> ##matplotlib verison : '1.0.svn' >> >>> maps=[m for m in cm.datad if not m.endswith("_r")] >> >>> for i in maps: >> try: >> a=cm.get_cmap(i,256)(range(256)) >> except: >> (type, value, traceback) = sys.exc_info() >> print "Problems to create %s" % (i,) >> print "The error was --> %s: %s" % (type, value) >> >> >> Problems to create gist_rainbow >> The error was --> <type 'exceptions.TypeError'>: tuple indices must be >> integers, not str >> Problems to create terrain >> The error was --> <type 'exceptions.TypeError'>: tuple indices must be >> integers, not str >> Problems to create bwr >> The error was --> <type 'exceptions.TypeError'>: tuple indices must be >> integers, not str >> Problems to create brg >> The error was --> <type 'exceptions.TypeError'>: tuple indices must be >> integers, not str >> Problems to create seismic >> The error was --> <type 'exceptions.TypeError'>: tuple indices must be >> integers, not str >> ################################################################## >> > > Is there any particular reason why you are doing the "(range(256))"? Keep > in mind that a colormap in matplotlib works differently than a colormap in > Matlab. In Matlab, the colormap is a 2-D array of rgb values, while in > matplotlib, it is an object that is used by the backends for > color-rendering. > > Often times, you will not need to do anything more than specify which > colormap you want by name e.g., pcolor(X, Y, Z, cmap='gist_rainbow'), or by > passing in a customized or self-made colormap object to the 'cmap' keyword > argument. > > What is happening in your code when you call '(range(256))' is that the > colormap is being called for an array of values ranging from 0 to 255 and > is determining what the color will be for each of those values. What seems > to be happening with those few colormaps is that the call is being made > before those maps are properly self-initialized. So, there might be some > sort of flaw here that you have exposed, but I would suggest taking another > look at what you are trying to accomplish to see if there is a better way. > > Thanks for giving matplotlib a try and I hope you continue to use it for > your work! > > Ben Root > >
Hi Ben, Thanks a lot for answering my question! I am a newbie to matplotlib, so please forgive me, if the question is stupid ;p In my program I wanna know the exact RGBA value of a data point in the figure, in order to plot a corresponding line with the same color in another figure. That is why I need to call a(z) to get RGBA value of a point at (x,y,z) (z is represented by color). If I understood it correctly, you said I do not need to specify the 'lut' in cm.get_cmap(name,lut)? I thought the colormap object is actually a lookup table with a length specified by lut. It turns out I do not need to specify anything here. But if I do not specify anything the colormap.N is always 256. What will happen then, if I need more color steps? I do notice a(2) and a(2.2) returns different values. So I am very confused about the principle how the RGBA value is generated by the colormaps. LittleBigBrain ------------------------------------------------------------------------------ Download new Adobe(R) Flash(R) Builder(TM) 4 The new Adobe(R) Flex(R) 4 and Flash(R) Builder(TM) 4 (formerly Flex(R) Builder(TM)) enable the development of rich applications that run across multiple browsers and platforms. Download your free trials today! http://p.sf.net/sfu/adobe-dev2dev _______________________________________________ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users