您的位置:首頁 > 軟件教程 > 教程 > 使用Python的matplotlib庫繪制圖表

使用Python的matplotlib庫繪制圖表

來源:好特整理 | 時(shí)間:2024-04-25 11:59:11 | 閱讀:92 |  標(biāo)簽: T 畫圖 Python   | 分享到:

本文將介紹如何使用 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)

參考資料:

  • 【GitHub】Implement Font-Fallback in Matplotlib
  • 【matplotlib】axes.Axes.bar_label
  • 【matplotlib】Broken Axis

本文作者: ywang_wnlo
本文鏈接: https://ywang-wnlo.github.io/posts/731b80f7/
版權(quán)聲明: 本博客所有文章除特別聲明外,均采用BY-NC-SA許可協(xié)議。轉(zhuǎn)載請(qǐng)注明

小編推薦閱讀

好特網(wǎng)發(fā)布此文僅為傳遞信息,不代表好特網(wǎng)認(rèn)同期限觀點(diǎn)或證實(shí)其描述。

相關(guān)視頻攻略

更多

掃二維碼進(jìn)入好特網(wǎng)手機(jī)版本!

掃二維碼進(jìn)入好特網(wǎng)微信公眾號(hào)!

本站所有軟件,都由網(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)