## Re: [Matplotlib-users] Legend labels - interaction with functions

 Re: [Matplotlib-users] Legend labels - interaction with functions From: Jouni K. Seppänen - 2008-05-22 14:46:16 ```David Simpson writes: > This is probably my lack of knowledge of python, but how do I set up > legend labels for some bar-plots that have been produced inside a > function. For example, the following will nicely plot my bar-plots, but > then legend doesn't know about the colours used, so here just uses black > for both labels. Legends for multiple histograms don't behave the way people expect, because the colors are taken bar by bar, first from one histogram, then from the second histogram, etc. You need to take just one bar from each histogram and pass them as the first argument to legend: ------------------------------------------------------------------------ from pylab import * x=arange(0,5) y=array([ 1.2, 3.4, 5.4, 2.3, 1.0]) z=array([ 2.2, 0.7, 0.4, 1.3, 1.2]) def plotb(x,y,col): p=bar(x,y,color=col) return p[0] p1 = plotb(x,y,'k') p2 = plotb(x+0.4,z,'y') legend((p1, p2), ('YYY','ZZZ')) show() ------------------------------------------------------------------------ I think this behavior of legend is suboptimal, but there is a logic to it: by default you label objects in the order you draw them, and the histogram plot happens to consist of several similarly-colored objects. -- Jouni K. Seppänen http://www.iki.fi/jks ```

 [Matplotlib-users] Legend labels - interaction with functions From: David Simpson - 2008-05-22 12:53:31 ```This is probably my lack of knowledge of python, but how do I set up legend labels for some bar-plots that have been produced inside a function. For example, the following will nicely plot my bar-plots, but then legend doesn't know about the colours used, so here just uses black for both labels. I'd like the labels to have the same colour as the bars generated inside plotb. (I am using a function here as my real code has extra stuff to calculate error-bars and suchlike for each data set.) x=arange(0,5) y=array([ 1.2, 3.4, 5.4, 2.3, 1.0]) z=array([ 2.2, 0.7, 0.4, 1.3, 1.2]) def plotb(x,y,col): p=bar(x,y,color=col) plotb(x,y,'k') plotb(x+0.4,z,'y') legend(('YYY,'ZZZ')) I tried passing the object "p" through the plotb argument list, but python didn't like that. (I am just learning python, and so far haven't seen how to pass such objects around. Thanks for any tips, Dave ```
 [Matplotlib-users] changing ticklabels color From: Chiara Caronna - 2008-05-22 14:30:30 Attachments: Message as HTML ```Hello, I would like to change the color of the yticklabels. I tried to use this command: ax1=p.subplot(212) ax1.set_xlabel('t [sec]') ax1.set_ylabel('g^2(q,t)') ax1.set_yticklabels(color='r') but it gives an error: TypeError: set_yticklabels() takes at least 2 non-keyword arguments (1 given) if I write instead ax1.set_yticklabels(['1','2'],color='r') it works, but it puts clearly labels 1 and 2... I don't want to change the labels, only the color! is there anyway of doing it? Hope you can help me Chiara > Date: Thu, 22 May 2008 14:52:13 +0200 > From: david.simpson@... > To: matplotlib-users@... > Subject: [Matplotlib-users] Legend labels - interaction with functions > > This is probably my lack of knowledge of python, but how do I set up > legend labels for some bar-plots that have been produced inside a > function. For example, the following will nicely plot my bar-plots, but > then legend doesn't know about the colours used, so here just uses black > for both labels. I'd like the labels to have the same colour as the bars > generated inside plotb. (I am using a function here as my real code has > extra stuff to calculate error-bars and suchlike for each data set.) > > x=arange(0,5) > y=array([ 1.2, 3.4, 5.4, 2.3, 1.0]) > z=array([ 2.2, 0.7, 0.4, 1.3, 1.2]) > > def plotb(x,y,col): > p=bar(x,y,color=col) > > plotb(x,y,'k') > plotb(x+0.4,z,'y') > > legend(('YYY,'ZZZ')) > > > I tried passing the object "p" through the plotb argument list, but > python didn't like that. (I am just learning python, and so far haven't > seen how to pass such objects around. > > Thanks for any tips, > > Dave > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users@... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users _________________________________________________________________ Invite your mail contacts to join your friends list with Windows Live Spaces. It's easy! http://spaces.live.com/spacesapi.aspx?wx_action=create&wx_url=/friends.aspx&mkt=en-us```
 Re: [Matplotlib-users] changing ticklabels color From: Friedrich Hagedorn - 2008-05-22 15:04:13 ```On Thu, May 22, 2008 at 02:30:24PM +0000, Chiara Caronna wrote: > Hello, > I would like to change the color of the yticklabels. I tried to use this > command: > > ax1=p.subplot(212) [...] > ax1.set_yticklabels(color='r') * Solution 1: In [1]: ax1=subplot(111) In [2]: setp(ax1.get_yaxis().get_major_ticklabels(), color='r') * Solution 2: In [1]: ax1=subplot(111) In [2]: for label in ax1.get_yaxis().get_majorticklabels(): ...: label.set_color('r') ...: ...: In [3]: draw() May be there is a shorter solution but I dont know of it :-) By, Friedrich ```
 Re: [Matplotlib-users] Legend labels - interaction with functions From: Friedrich Hagedorn - 2008-05-22 14:40:22 ```On Thu, May 22, 2008 at 02:52:13PM +0200, David Simpson wrote: > This is probably my lack of knowledge of python, but how do I set up > legend labels for some bar-plots that have been produced inside a > function. For example, the following will nicely plot my bar-plots, but > then legend doesn't know about the colours used, so here just uses black > for both labels. I'd like the labels to have the same colour as the bars > generated inside plotb. (I am using a function here as my real code has > extra stuff to calculate error-bars and suchlike for each data set.) > > x=arange(0,5) > y=array([ 1.2, 3.4, 5.4, 2.3, 1.0]) > z=array([ 2.2, 0.7, 0.4, 1.3, 1.2]) > > def plotb(x,y,col): > p=bar(x,y,color=col) > > plotb(x,y,'k') > plotb(x+0.4,z,'y') > > legend(('YYY,'ZZZ')) > > I tried passing the object "p" through the plotb argument list, but > python didn't like that. (I am just learning python, and so far haven't > seen how to pass such objects around. You could return the plotted lines from the function plotb. Here is my attempt: In [1]: x=arange(0,5) In [2]: y=array([ 1.2, 3.4, 5.4, 2.3, 1.0]) In [3]: z=array([ 2.2, 0.7, 0.4, 1.3, 1.2]) In [4]: def plotb(x,y,col): ...: lines = bar(x,y,color=col) ...: return lines ...: In [5]: l1 = plotb(x,y,'k') In [6]: l2 = plotb(x+0.4,z,'y') In [7]: legend((l1[0], l2[0]), ('YYY','ZZZ')) Out[7]: The legend() function could label any line object. So every single bar-line could be listed in the legend. But I think you would only have one from each color, so I have choosen the first: l1[0] and l2[0]. Is this what you what? By, Friedrich ```
 Re: [Matplotlib-users] Legend labels - interaction with functions From: Jouni K. Seppänen - 2008-05-22 14:46:16 ```David Simpson writes: > This is probably my lack of knowledge of python, but how do I set up > legend labels for some bar-plots that have been produced inside a > function. For example, the following will nicely plot my bar-plots, but > then legend doesn't know about the colours used, so here just uses black > for both labels. Legends for multiple histograms don't behave the way people expect, because the colors are taken bar by bar, first from one histogram, then from the second histogram, etc. You need to take just one bar from each histogram and pass them as the first argument to legend: ------------------------------------------------------------------------ from pylab import * x=arange(0,5) y=array([ 1.2, 3.4, 5.4, 2.3, 1.0]) z=array([ 2.2, 0.7, 0.4, 1.3, 1.2]) def plotb(x,y,col): p=bar(x,y,color=col) return p[0] p1 = plotb(x,y,'k') p2 = plotb(x+0.4,z,'y') legend((p1, p2), ('YYY','ZZZ')) show() ------------------------------------------------------------------------ I think this behavior of legend is suboptimal, but there is a logic to it: by default you label objects in the order you draw them, and the histogram plot happens to consist of several similarly-colored objects. -- Jouni K. Seppänen http://www.iki.fi/jks ```
 Re: [Matplotlib-users] Legend labels - interaction with functions From: John Hunter - 2008-05-22 16:39:11 ```On Thu, May 22, 2008 at 9:45 AM, Jouni K. Seppänen wrote: > I think this behavior of legend is suboptimal, but there is a logic to > it: by default you label objects in the order you draw them, and the > histogram plot happens to consist of several similarly-colored objects. The best solution would be to rewrite the hist to use a collection. This would make legend behave properly, would be a lot faster, but would break a fair amount of legend code. Probably a good thing to do for 0.98 JDH ```
 Re: [Matplotlib-users] changing ticklabels color From: Jouni K. Seppänen - 2008-05-22 15:08:03 ```Chiara Caronna writes: > ax1=p.subplot(212) > ax1.set_xlabel('t [sec]') > ax1.set_ylabel('g^2(q,t)') > ax1.set_yticklabels(color='r') You could do for label in ax1.get_yticklabels(): label.set_color('r') or use the setp shortcut: p.setp(ax1.get_yticklabels(), color='r') -- Jouni K. Seppänen http://www.iki.fi/jks ```
 Re: [Matplotlib-users] changing ticklabels color From: Chiara Caronna - 2008-05-22 15:39:23 Attachments: Message as HTML ```> p.setp(ax1.get_yticklabels(), color='r') This worked! Thanks a lot! Chiara > To: matplotlib-users@... > From: jks@... > Date: Thu, 22 May 2008 17:52:09 +0300 > Subject: Re: [Matplotlib-users] changing ticklabels color > > Chiara Caronna writes: > > > ax1=p.subplot(212) > > ax1.set_xlabel('t [sec]') > > ax1.set_ylabel('g^2(q,t)') > > ax1.set_yticklabels(color='r') > > You could do > > for label in ax1.get_yticklabels(): label.set_color('r') > > or use the setp shortcut: > > p.setp(ax1.get_yticklabels(), color='r') > > -- > Jouni K. Seppänen > http://www.iki.fi/jks > > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users@... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users _________________________________________________________________ Invite your mail contacts to join your friends list with Windows Live Spaces. It's easy! http://spaces.live.com/spacesapi.aspx?wx_action=create&wx_url=/friends.aspx&mkt=en-us```