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

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@…2017…
To: matplotlib-users@lists.sourceforge.net
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® Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/


Matplotlib-users mailing list
Matplotlib-users@…2018…rceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users


Invite your mail contacts to join your friends list with Windows Live Spaces. It’s easy! Try it!

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]: <matplotlib.legend.Legend object at 0x908dc6c>

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

···

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.

   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 :slight_smile:

By, Friedrich

···

On Thu, May 22, 2008 at 02:30:24PM +0000, Chiara Caronna wrote: