Matplotlib可视化(五)--条形图

#导入模块
import numpy as np
import matplotlib.pyplot as plt

简单的条形图

N = 5
m = [20, 10, 30, 25, 15]

index = np.arange(N)
pl = plt.bar(x=index, height=m, color='blue', width=0.5)
plt.show()

将条形图横过来

p3 = plt.barh(y=index, width=m)
#将y设置为横坐标,宽度设置为数值,就表示横过来的条形图
plt.show()

两个条形图放在一起

index = np.arange(4)
sale_bj = [32, 45, 87, 89]
sale_sh = [67, 53, 34, 54]
bar_width = 0.3
plt.bar(index, sale_bj, bar_width, color='b')
plt.bar(index+bar_width, sale_sh, bar_width, color='r')
plt.show()
#两个条形图放在一起,紧挨着横坐标+一个bar_width

堆叠条形图

plt.bar(index, sale_bj, bar_width, color='b')
plt.bar(index, sale_sh, bar_width, color='r', bottom=sale_bj)
plt.show()
#将上海的底设置成北京

另一个堆叠条形图

index = np.arange(5)
bar_width = 0.3
data1 = np.random.randint(1, 100, 5)
data2 = np.random.randint(1, 100, 5)
plt.bar(index, data1, bar_width, color='y')
plt.bar(index, data2, bar_width, color='g', bottom=data1)
plt.show()

猜你喜欢

转载自blog.csdn.net/qq_42007339/article/details/104641657