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]: <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
-------------------------------------------------------------------------
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
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-users