python - matplotlib: share x-axis for two barcharts, each with 4 groups -
i trying create figure 2 barcharts using matplotlib. each barchart has 4 groups of bars. current python code using follows:
fig,ax=plt.subplots() bar_width=0.15 rects1 = plt.bar(index, group0, bar_width, alpha=opacity, color='b', label='1') rects2 = plt.bar(index + bar_width, group1, bar_width, alpha=opacity, color='r', label='2') rects3 = plt.bar(index + bar_width+bar_width, group2, bar_width, alpha=opacity, color='c', label='3') rects4 = plt.bar(index + bar_width+bar_width+bar_width, group3, bar_width, alpha=opacity, color='m', label='4')
after formatting, plot obtain follows:
now, have 2 such barcharts , want them share x-axis.
i can't figure out way achieve this. appreciated.
thanks
as suggested mel, calling bar() multiple times on same axes worked in case. working code this:
fig,ax=plt.subplots(2,1,sharex=true) ax1 = plt.subplot(2, 1, 1) a=ax1.bar(index, rating0,width=0.15,alpha=opacity, color='c' ) index=map(lambda x:x+0.15, index) b=ax1.bar(index,rating1,width=0.15,alpha=opacity, color='r') index=map(lambda x:x+0.15, index) c=ax1.bar(index,rating2,width=0.15,alpha=opacity, color='y') index=map(lambda x:x+0.15, index) d=ax1.bar(index,rating3,width=0.15,alpha=opacity, color='m') index=map(lambda x:x-0.45, index) ax2 = plt.subplot(2, 1, 2, sharex=ax1) ax2.bar(index, rating0paris,width=0.15,alpha=opacity, color='c') index=map(lambda x:x+0.15, index) ax2.bar(index,rating1paris,width=0.15,alpha=opacity, color='r') index=map(lambda x:x+0.15, index) ax2.bar(index,rating2paris,width=0.15,alpha=opacity, color='y') index=map(lambda x:x+0.15, index) ax2.bar(index,rating3paris,width=0.15,alpha=opacity, color='m') axes in ax: format_axes(axes)
Comments
Post a Comment