#coding: utf-8 import matplotlib.pyplot as plt import numpy as np import itertools def flip(items, ncol): return itertools.chain(*[items[i::ncol] for i in range(ncol)]) h = 0.5 # case-3, Case-2, case-1, dummy x1 = np.array([54,46,46]) #Headrace x2 = np.array([8,8,8]) #Headrace Surge tank x3 = np.array([16,33,14]) #Penstock x4 = np.array([43,43,37]) #Steel liner x5 = np.array([16,16,25]) #Tailrace x6 = np.array([0,0,4]) #Tailrace Surge Tank x7 = np.array([86,86,100]) #PowerStation x8 = np.array([11,13,14]) #Otheres y = np.array([1,2,3]) sx1=x1 sx2=sx1+x2 sx3=sx2+x3 sx4=sx3+x4 sx5=sx4+x5 sx6=sx5+x6 sx7=sx6+x7 ax=plt.subplot(111) ax.barh(y,x1,color='red', alpha=0.5,height=h, label='Headrace', align="center") ax.barh(y,x2,color='red', hatch=2*'x',alpha=0.5,height=h,left=sx1,label='Headrace S/T', align="center") ax.barh(y,x3,color='yellow', alpha=0.5,height=h,left=sx2,label='Penstock', align="center") ax.barh(y,x4,color='yellow',hatch=2*'/',alpha=0.5,height=h,left=sx3,label='Steel Pipe', align="center") ax.barh(y,x5,color='cyan', alpha=0.5,height=h,left=sx4,label='Tailrace', align="center") ax.barh(y,x6,color='cyan', hatch=2*'x',alpha=0.5,height=h,left=sx5,label='Tailrace S/T', align="center") ax.barh(y,x7,color='green', alpha=0.3,height=h,left=sx6,label='Power Station',align="center") ax.barh(y,x8,color='pink', alpha=0.5,height=h,left=sx7,label='Access etc.', align="center") handles, labels = ax.get_legend_handles_labels() plt.legend(flip(handles,3),flip(labels,3),ncol=3,loc='center',bbox_to_anchor=(0.5, -0.4),shadow=True,prop={'size' : 15},handlelength=2,handleheight=0.8) plt.subplots_adjust(bottom=0.40) plt.xticks(range(0,300,50),fontsize=15) plt.yticks(y, ['Case-3','Case-2','Case-1',''],fontsize=15) plt.xlabel('Related Construction Cost (million US$)',fontsize=15) plt.title('Comparison of Construction Cost',fontsize=20) fnameF = "fig_bar3.png" plt.savefig(fnameF) plt.show()