import numpy as np
import matplotlib.pyplot as plt
N = 5
y = [20, 10, 30, 25, 15]
index = np.arange(N)
plt.bar(left=index, height=y,color='red', width=0.5) #left横坐标,height纵坐标, color颜色 width
plt.show()
########################
plt.bar(left=0, bottom=index,color='green', width=y,height=0.5,orientation='horizontal') #left横坐标,height纵坐标, color颜色 width
plt.show()
########################
plt.barh(left=0, bottom=index,width=y)
plt.show()
######################
#并列图
idx = np.arange(4)
sales_bj=[12,24,16,34]
sales_sh=[13,22,19,50]
x_w=0.3
plt.bar(idx, sales_bj, x_w, color='red')
plt.bar(idx+x_w, sales_sh, x_w, color='green')
plt.show()
########################
#层叠图
idx = np.arange(4)
sales_bj=[12,24,16,34]
sales_sh=[13,22,19,50]
x_w=0.3
plt.bar(idx, sales_bj, x_w, color='red')
plt.bar(idx, sales_sh, x_w, color='green',bottom=sales_bj)
plt.show()
######################
#上下对比图
n = 12
X = np.arange(n)
Y1 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)
Y2 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)
plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')
for x, y in zip(X, Y1):
plt.text(x + 0.4, y + 0.05, '%.2f' % y, ha='center', va= 'bottom')
for x, y in zip(X, Y2):
plt.text(x + 0.4, -y - 0.05, '%.2f' % y, ha='center', va= 'top')
plt.xlim(-.5, n)
plt.xticks(())
plt.ylim(-1.25, 1.25)
plt.yticks(())