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

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

# -*- coding:utf-8 -*-
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
matplotlib.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()

显示结果:

 

全部评论

相关推荐

昨天 14:09
已编辑
北京交通大学 算法工程师
字节跳动 训练框架研发 (N+2) * (12 + 3) 硕士211
Crinton:训练框架遥遥领先
点赞 评论 收藏
分享
rbjjj:太杂了吧,同学,项目似乎都没深度,都是api调度耶,分层架构思想没有体现出来了,前端没有前端优化前端工程化体现,后端微服务以及分层架构没体现以及数据安全也没体现,核心再改改,注重于计算机网络,工程化,底层原理吧
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务