Python 中 plt 画柱状图和折线图

1. 背景

Python在一些数据可视化的过程中需要使用 plt 函数画柱状图和折线图。

2. 导入

import matplotlib.pyplot as plt

3. 柱状图

array= np.array(array)

plt.hist(array, bins=50,facecolor="red", edgecolor="red" ,linewidth=5,alpha=0.7)

    plt.xlabel("")
    plt.ylabel("")
    plt.title("")

4.折线图

plt.figure(figsize=(num_group, 6))
X=[1,2,3,4,5,6,7,8,9]
Y=[1,2,3,4,5,6,7,8,9]#定义折线图的X,Y坐标

plt.plot(X, Y, label=str(model_name)) #折线图

for a, b in zip(X, Y):
    plt.text(a, b, '%.2f' % b, ha='center', va='bottom', fontsize=7)#每个点的数值
plt.legend(loc=2)#显示每根折线的label
plt.title("{}".format(eval_key))#显示图名

5.保存

print("=> saving {}".format(image_name))
plt.savefig(image_name)

  

猜你喜欢

转载自www.cnblogs.com/siyuan1998/p/11373359.html