1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > matplotlib在一张图同时画折线图和柱状图

matplotlib在一张图同时画折线图和柱状图

时间:2023-04-12 14:32:42

相关推荐

matplotlib在一张图同时画折线图和柱状图

实现了在一张图片里用matplotlib的pyplot同时绘制折线图和柱状图。

# -*- coding:utf-8 -*-import matplotlibimport matplotlib.pyplot as pltimport numpy as npmatplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑体显示中文# 构建数据x = np.arange(1, 15)y = [0.8, 0.88, 0.825, 0.76, 0.91, 0.95, 0.74, 0.99, 0.80, 0.72, 0.9, 0.81, 0.74, 0.87]z = [37, 25, 17, 49, 27, 77, 34, 34, 34, 51, 39, 52, 47, 12]u = [37, 31, 19, 57, 29, 86, 36, 37, 45, 64, 42, 57, 50, 24]# 绘柱状图plt.bar(x=x, height=z, label='实际', color='Coral', alpha=0.8)plt.bar(x=x, height=u, label='总数', color='LemonChiffon', alpha=0.8)# 在左侧显示图例plt.legend(loc="upper left")# 设置标题plt.title("Detection results")# 为两条坐标轴设置名称plt.xlabel("Component number")plt.ylabel("Number of seam")# 画折线图ax2 = plt.twinx()ax2.set_ylabel("recall")# 设置坐标轴范围ax2.set_ylim([0.5, 1.05]);plt.plot(x, y, "r", marker='.', c='r', ms=5, linewidth='1', label="Recall")# 显示数字for a, b in zip(x, y):plt.text(a, b, b, ha='center', va='bottom', fontsize=8)# 在右侧显示图例plt.legend(loc="upper right")plt.savefig("recall.jpg")plt.show()

显示结果:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。