12 python数据可视化(精讲柱状图&条形图)
00 载入扩展库
import numpy as np
import matplotlib.pyplot as plt
01 柱状图
x=[1,2,3,4,5]
y=[6,10,4,5,1]
colors=['r','b','g','m','c']
plt.bar(x,y,align='center',color=colors)
02 条形图
x=[1,2,3,4,5]
y=[6,10,4,5,1]
colors=['r','b','g','m','c']
plt.barh(x,y,align='center',color=colors)
03 改变X轴和Y轴的坐标标识
x=[1,2,3,4,5]
y=[6,10,4,5,1]
colors=['r','b','g','m','c']
plt.bar(x,y,align='center',color=colors)
plt.xticks(x,['a','b','c','d','e'])
x=[1,2,3,4,5]
y=[6,10,4,5,1]
colors=['r','b','g','m','c']
plt.barh(x,y,align='center',color=colors)
plt.yticks(x,['a','b','c','d','e'])
04 堆积(叠加)
x=[1,2,3,4,5]
y=[6,10,4,5,1]
y1=[2,6,3,8,5]
colors=['r','b','g','m','c']
plt.bar(x,y,align='center',color=colors)
plt.bar(x,y1,align='center',bottom=y,color='k')
plt.xticks(x,['a','b','c','d','e'])
x=[1,2,3,4,5]
y=[6,10,4,5,1]
y1=[2,6,3,8,5]
colors=['r','b','g','m','c']
plt.barh(x,y,align='center',color=colors)
plt.barh(x,y1,align='center',left=y,color='k')
plt.yticks(x,['a','b','c','d','e'])
05 并列
x=np.arange(1,6,1)
y=[6,10,4,5,1]
y1=[2,6,3,8,5]
colors=['r','b','g','m','c']
plt.bar(x,y,align='center',width=0.4,color=colors)
plt.bar(x+0.4,y1,align='center',width=0.4,color='k')
plt.xticks(x+0.2,['a','b','c','d','e'])
x=np.arange(1,6,1)
y=[6,10,4,5,1]
y1=[2,6,3,8,5]
colors=['r','b','g','m','c']
plt.barh(x,y,align='center',height=0.4,color=colors)
plt.barh(x+0.4,y1,align='center',height=0.4,color='k')
plt.yticks(x+0.2,['a','b','c','d','e'])
06 hatch的使用
x=[1,2,3,4,5]
y=[6,10,4,5,1]
colors=['r','b','g','m','c']
plt.bar(x,y,align='center',color=colors,hatch='//')
x=[1,2,3,4,5]
y=[6,10,4,5,1]
colors=['r','b','g','m','c']
plt.barh(x,y,align='center',color=colors,hatch='//')