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
CODE OF MULTIBARS
···
#-------------------------------------------------------------------------------
# Name: multibars
# Purpose:
#
# Author: alf
#
# Created: 05-01-2011
# Copyright: (c) alf 2010
# Licence: <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import matplotlib.pyplot as plt
from numpy import array
import random
strTitle = 'Bars plot - multi data and legend'
x = array(range(10))
y1 = []
y2 = [[],[],[]]
for i in range(len(x)):
y1.append(1.0 + random.random())
for j in range(len(y2)):
y2[j].append(0.5 + random.random())
lbl_y1 = []
lbl_y1.append('bar 1')
lbl_y2 = 'bar 2'
lbl_type = ['red', 'green', 'blue']
colors_y2 = ([1,0,0],[0,1,0],[0,0,1])
lbls_y2 = []
for i in range(len(lbl_type)):
lbls_y2.append(lbl_y2 + " " + lbl_type[i])
fig = plt.figure()
ax1=fig.add_subplot(2,1,1)
ax1.set_title("Single data")
plt.bar(x, y1, color='purple', linewidth = 0, align='edge', width = 0.8)
plt.legend(lbl_y1, loc= 0)
plt.xticks(x)
ax2=fig.add_subplot(2,1,2, sharex=ax1)
ax2.set_title("Multi data")
for i in range(len(y2)):
plt.bar(x+(0.8*float(i)/len(y2)),y2[i],color = colors_y2[i], linewidth=0, align='edge', width = (0.8/len(y2)))
plt.legend(lbls_y2, loc=0)
plt.xticks(x)
plt.subplots_adjust(left=0.05, bottom=0.1, right=0.95, top=0.95, wspace=0.1, hspace=0.15)
plt.show()
##############################
CODE OF MULTILINES
#-------------------------------------------------------------------------------
# Name: multilines
# Purpose:
#
# Author: alf
#
# Created: 05-01-2011
# Copyright: (c) alf 2010
# Licence: <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import matplotlib.pyplot as plt
from numpy import array
import random
strTitle = 'Lines plot - multi data and legend'
x = array(range(10))
y1 = []
y2 = [[],[],[]]
for i in range(len(x)):
y1.append(1.0 + random.random())
for j in range(len(y2)):
y2[j].append(0.5 + random.random())
lbl_y1 = []
lbl_y1.append('line 1')
lbl_y2 = 'line 2'
lbl_type = ['red', 'green', 'blue']
colors_y2 = ([1,0,0],[0,1,0],[0,0,1])
lbls_y2 = []
for i in range(len(lbl_type)):
lbls_y2.append(lbl_y2 + " " + lbl_type[i])
fig = plt.figure()
ax1=fig.add_subplot(2,1,1)
ax1.set_title("Single data")
plt.plot(x, y1, color='purple')
plt.legend(lbl_y1, loc= 0)
plt.xticks(x)
ax2=fig.add_subplot(2,1,2, sharex=ax1)
ax2.set_title("Multi data")
for i in range(len(y2)):
plt.plot(x,y2[i],color = colors_y2[i])
plt.legend(lbls_y2, loc=0)
plt.xticks(x)
plt.subplots_adjust(left=0.05, bottom=0.1, right=0.95, top=0.95, wspace=0.1, hspace=0.15)
plt.show()
##############################