On Wed, Jan 5, 2011 at 7:19 AM, Alain Pascal Frances <frances17...@itc.nl>wrote:

> Hi,
>
> I'm plotting two subplots using bar charts, the first one contains one
> dataset (one bar for each abscissa value), the second 3 dataset (3 bars for
> each abscissa value, each one with its own color). The legend of the
> multibars chart is not correct, it shows the color of the first dataset for
> all the bars legend.
> Note that it works correctly with matplotlib.pyplot.plot instead of
> matplotlib.pyplot.bar.
> Any idea to fix it?
> Thanks,
>
> A.Frances
>
>
I can confirm the problem, and I have a few suspects as to the cause.  Most
notably that the legend code probably assumes that it is looking for line
objects, not patch objects and starts using its own color cycler when it
can't get a list of colors to correspond with its list of labels.

As a work-around, you can add a label keyword to the call to bar() and not
bother giving legend() a list of labels.

Here is a cleaned-up version of your first code.  Note I also took a moment
to take advantage of python syntax and better numpy/matplotlib coding
styles. The only issue seems to be that there might be a bug with respect to
positioning the legend:

import matplotlib.pyplot as plt
import numpy as np

strTitle = 'Bars plot - multi data and legend'
x = np.arange(10)
y1 = 1.0 + np.random.random(x.shape)
y2 = 0.5 + np.random.random((3, len(x)))

lbl_y1 = 'bar 1'
lbl_type = ['red', 'green', 'blue']
colors_y2 = ([1,0,0],[0,1,0],[0,0,1])
lbls_y2 = ["bar 2 " + lbl for lbl in lbl_type]

fig = plt.figure()
ax1 = fig.add_subplot(2,1,1)
ax1.set_title("Single data")
ax1.bar(x, y1, color='purple', linewidth=0, align='edge', width=0.8,
label=lbl_y1)
ax1.legend(loc=0)
ax1.set_xticks(x)

ax2 = fig.add_subplot(2,1,2, sharex=ax1)
ax2.set_title("Multi data")
for i, (y, color, lbl) in enumerate(zip(y2, colors_y2, lbls_y2)) :
   ax2.bar(x+(0.8*float(i)/len(y2)), y, color=color, label=lbl, linewidth=0,
align='edge', width=(0.8/len(y2)))
ax2.legend(loc=0)

fig.subplots_adjust(left=0.05, bottom=0.1, right=0.95, top=0.95, wspace=0.1,
hspace=0.15)

plt.show()

I hope this helps!
Ben Root
------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and, 
should the need arise, upgrade to a full multi-node Oracle RAC database 
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to