import matplotlib.pyplot as plt;
plt.bar( x, # 元组或列表,数据的x坐标序列
height, # 元组或列表,数据序列
width, # 数值或列表,条形宽度,默认0.8
bottom, # 数值或列表,y轴的基准值,默认0
align, # 字符串,两种值可供选择{'center','edge'}
color, # 元组或列表,条形颜色
edgecolor, # 元组或列表,条边颜色
linewidth, # 元组或列表,可选条形边缘宽度,如为0则无边
tick_label, # 元组或列表或字符串,条形数据的标签
xerr,yerr, # 元组或列表或数值,误差线
ecolor, # 元组或列表,误差线颜色,默认为黑色
capsize, # 误差线长度
error_kw, # 字典,错误条
log, # 布尔,是否以log对象做刻度,默认为假
orientation # 字符串,两种值供选择{'vertical'垂直,'horizontal'水平}
)
示例1:普通柱状图
import matplotlib.pyplot as plt;
x = [0.7,0.3];
y = [60,40];
color = ['blue','red'];
lable = ['man','woman'];
def th1():
plt.bar(x,y,width=0.2,
color=color,align='center',
tick_label=lable);
plt.xlim(0,1);
plt.ylim(0,100);
plt.show();
th1();
示例2:并列式
import matplotlib.pyplot as plt;
import numpy as np;
def axis(xi,xm,yi,ym): # 设置坐标轴范围
plt.xlim(xi,xm);
plt.ylim(yi,ym);
def th2():
num1=np.array([10,15,16,28]);
num2=np.array([10,12,18,26]);
width=0.3;
x = np.array([x for x in range(1,5)]); # 第一个数据序列x轴
x1 = x-width; # 为使其并列,使得第一个x轴序列全部减去条宽度
plt.bar(x1,num1,width=width,color='blue',label='first');
plt.bar(x,num2,width=width,color='green',label='second');
plt.legend(loc='upper left'); # 设置标签显示在左上角
axis(0,5,0,30);
plt.show();
th2();
示例3:堆叠式
def th3():
num1=np.array([10,15,16,28]);
num2=num1+np.array([10,12,18,26]);
ymax = max(num2);
width=0.3;
x = np.array([x for x in range(1,5)]);
plt.bar(x,num2,width=width,color='green',label='second',align='center');
# 避免下面的数据颜色被掩盖,需要先画第二个数据条
plt.bar(x,num1,width=width,color='blue',label='first',align='center');
plt.legend(loc='upper left');
axis(0,5,0,ymax+5);
plt.show();
th3();