日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

python matlibplot將不同數據的柱狀圖和折線圖畫在同一張圖中

作者:別出BUG求求了 更新時間: 2023-11-14 編程語言

實現了在一張圖片里用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()
 

結果:
在這里插入圖片描述

原文鏈接:https://blog.csdn.net/weixin_39589455/article/details/129160579

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新