本文將介紹如何使用 Python 的 matplotlib 庫畫圖,記錄一些常用的畫圖 demo 代碼
本文將介紹如何使用Python的matplotlib庫繪制圖表,并記錄一些常用的繪圖示例代碼。
安裝matplotlib庫:
# 建議先切換到虛擬環(huán)境中
pip install matplotlib
中文顯示設(shè)置:
新版的matplotlib已經(jīng)支持字體回退功能,因此可以直接設(shè)置字體為Times New Roman和SimSun(宋體),以實(shí)現(xiàn)英文以Times New Roman顯示,中文以宋體顯示。
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = ['Times New Roman','SimSun']
折線圖和點(diǎn)線圖:
使用plot函數(shù)可以繪制折線圖和點(diǎn)線圖,通過marker參數(shù)設(shè)置點(diǎn)的樣式,markersize參數(shù)設(shè)置點(diǎn)的大小。
import matplotlib.pyplot as plt
# 設(shè)置中文字體為Times New Roman和宋體
plt.rcParams['font.family'] = ['Times New Roman','SimSun']
# 生成數(shù)據(jù)
x = range(0, 6)
y = [i**3 for i in x]
# 繪制點(diǎn)線圖
plt.plot(x, y, marker='o', markersize=6, label='y=x^3')
# 添加坐標(biāo)軸標(biāo)簽
plt.xlabel('x')
plt.ylabel('y')
# 配置坐標(biāo)軸范圍
plt.xlim(0)
plt.ylim(0)
# 添加圖例
plt.legend()
# 配置緊湊布局
plt.tight_layout(pad=0.1)
# 保存圖片
plt.savefig('plot.png')
柱狀圖和堆積柱狀圖:
使用bar函數(shù)繪制柱狀圖,通過bottom參數(shù)可以繪制堆積柱狀圖。
# 生成數(shù)據(jù)
x = range(1, 6)
y1 = [1 for i in x]
y2 = [i for i in x]
# 繪制堆積柱狀圖
plt.bar(x, y1, color='tab:blue', edgecolor='black', label='y1=1', width=0.5)
plt.bar(x, y2, bottom=y1, color='tab:orange', edgecolor='black', label='y2=x', width=0.5)
坐標(biāo)軸斷點(diǎn):
有時(shí)需要在柱狀圖中添加y軸的斷點(diǎn),可以通過畫兩個(gè)相同的圖,并配置不同的y軸范圍,然后在兩個(gè)圖之間添加截?cái)嗑的方式來實(shí)現(xiàn)。
# 生成數(shù)據(jù)
x = range(1, 5)
y = [i**3 for i in x]
# 分別繪制上下兩個(gè)圖
b1 = ax1.bar(x, y, color='tab:blue', edgecolor='black', label='y1=1', width=0.5)
b2 = ax2.bar(x, y, color='tab:blue', edgecolor='black', label='y1=1', width=0.5)
# 顯示數(shù)據(jù)
ax1.bar_label(b1, fmt='%d', label_type='edge')
ax2.bar_label(b2, fmt='%d', label_type='edge')
# 配置上圖的坐標(biāo)軸范圍
ax1.set_ylim(15)
ax1.set_yticks([20, 40, 60])
# 刪掉上圖的下邊框
ax1.spines['bottom'].set_visible(False)
# 隱藏上圖的x軸
ax1.xaxis.set_visible(False)
# 配置下圖的坐標(biāo)軸范圍
ax2.set_ylim(0, 9)
ax2.set_yticks([0, 2, 4, 6, 8])
# 刪掉下圖的上邊框
ax2.spines['top'].set_visible(False)
# 添加截?cái)嗑,由于圖高度比例為1:2,所以截?cái)嗑的y坐標(biāo)也需要按比例設(shè)置
d = .015
kwargs = dict(transform=ax1.transAxes, color='k', clip_on=False)
ax1.plot((-d, +d), (-2*d, +2*d), **kwargs)
ax1.plot((1 - d, 1 + d), (-2*d, +2*d), **kwargs)
kwargs.update(transform=ax2.transAxes)
ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs)
ax2.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)
參考資料:
小編推薦閱讀本文作者: ywang_wnlo
本文鏈接: https://ywang-wnlo.github.io/posts/731b80f7/
版權(quán)聲明: 本博客所有文章除特別聲明外,均采用BY-NC-SA許可協(xié)議。轉(zhuǎn)載請(qǐng)注明
count(*)、count(1)哪個(gè)更快?面試必問:通宵整理的十道經(jīng)典MySQL必問面試題
閱讀從需求分析、產(chǎn)品設(shè)計(jì)到部署交付各階段說明
閱讀如何利用七牛云進(jìn)行數(shù)據(jù)備份和刪除
閱讀強(qiáng)化學(xué)習(xí)筆記之【ACE:Off-PolicyActor-CriticwithCausality-AwareEntropyRegularization】
閱讀使用MailKit在.NET Core中收發(fā)郵件的完整示例
閱讀WiFi基礎(chǔ)(六):天線基礎(chǔ)知識(shí)
閱讀OpenAI官方開源多智能體框架Swarm,社區(qū)反響熱烈
閱讀Vue-Vben-Admin:功能強(qiáng)大的Vue3后臺(tái)管理系統(tǒng)模板
閱讀深度解析Spring AI:請(qǐng)求與響應(yīng)機(jī)制的核心邏輯
閱讀.NET云原生應(yīng)用實(shí)踐(一):從搭建項(xiàng)目框架結(jié)構(gòu)開始
閱讀llama.cpp:一個(gè)適用于中小型研發(fā)企業(yè)的高性能CPU/GPU大語言模型推理框架
閱讀Windows應(yīng)急響應(yīng)-Auto病毒
閱讀本站所有軟件,都由網(wǎng)友上傳,如有侵犯你的版權(quán),請(qǐng)發(fā)郵件[email protected]
湘ICP備2022002427號(hào)-10 湘公網(wǎng)安備:43070202000427號(hào)© 2013~2024 haote.com 好特網(wǎng)