網(wǎng)站首頁 編程語言 正文
1.問題情境
我們使用python的 matplotlib庫(kù)繪圖時(shí),可能會(huì)遇到圖片內(nèi)容顯示不全的情況,
以下邊代碼為例:
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False x = range(9) y = [5.12, 5.15, 5.13, 5.10, 5.2, 5.25, 5.19, 5.24, 5.31] c = 0.5 * (min(x) + max(x)) d = min(y) + 0.3 * (max(y)-min(y)) plt.plot(x, y, label='股票A收盤價(jià)', c='k', ls='-.', marker='D', lw=2) plt.xticks(x, [ '2022-03-27', '2022-03-28', '2022-03-29', '2022-03-30', '2022-03-31', '2022-04-01', '2022-04-04', '2022-04-05', '2022-04-06'], rotation=45) plt.title('某股票收盤價(jià)時(shí)序圖') plt.xlabel('日期') plt.ylabel('價(jià)格') plt.grid(True) plt.legend() # 標(biāo)出每天的收盤價(jià) for a, b in zip(x, y): plt.text(a, b+0.01, '%.1f'%b, ha='center', va='bottom', fontsize=9) plt.annotate('最低價(jià)', (x[y.index(min(y))], min(y)), (x[y.index(min(y))] + 2, min(y)+0.06), xycoords='data', arrowprops=dict(width=3,headwidth=10,headlength=20, facecolor='g',shrink=0.05), c='r',fontsize=20) plt.show()
圖像效果如圖所示,圖像底部x軸的表示日期的標(biāo)簽,沒有被顯示完全:
雖然,有的知道的同學(xué)可能會(huì)告訴我,只要把窗口放大,就可以顯示得完整了。確實(shí)如此。但是這僅僅只能滿足我們的一般需求。如果我們的程序需要自動(dòng)化生成圖表并保存,這個(gè)方法就失效了。使用plt.savefig()保存出的圖片文件如下圖所示,這并不是我們想要的:
這樣的場(chǎng)景下,subplots_adjust()方法的應(yīng)用則恰到好處。
2. plt.subplots_adjust()概述
plt.subplots_adjust()方法常用的參數(shù)有6個(gè)。
其語法如下:
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)
其中,left, bottom, right, top依次表示四個(gè)方向上的,圖表與畫布邊緣之間的距離。
這四個(gè)參數(shù)的每個(gè)參數(shù)的取值范圍通常都在0-1之間。與其說是“間距”,倒不如說是圖像邊緣的“坐標(biāo)”更確切。使用這四個(gè)參數(shù)時(shí),將畫布左下角視為坐標(biāo)原點(diǎn),畫布的寬和高都視為1。如果參數(shù)取值大于1,則可能會(huì)出現(xiàn)圖像的損失,圖像會(huì)移動(dòng)到畫布之外,而不會(huì)報(bào)錯(cuò)。
且left不能大于等于right,bottom不能大于等于top,如果違反這一點(diǎn)則會(huì)發(fā)生報(bào)錯(cuò)。
wspace和 hspace則分別表示水平方向上圖像間的距離和垂直方向上圖像間的距離。其的取值是可以取得大于1,具體的則具體情形自行調(diào)試選出合適的。這兩個(gè)參數(shù)用于畫布有多個(gè)子圖時(shí)。
3. 案例展示
3.1 單圖情形
依然以第一部分中的示例為例,將表示圖表與下邊緣的距離 的參數(shù) bottom設(shè)成0.2。
即在上邊代碼的基礎(chǔ)上加上一句:
plt.subplots_adjust(bottom=0.2)
則圖像效果發(fā)生以下改變:
3.2 多子圖情形
在畫布上繪制以下四幅圖像。并設(shè)定上下左右及圖像間的間距。
依次在左上繪制一幅折線圖,右上繪制一幅散點(diǎn)圖,
左下繪制一幅柱狀圖,右下繪制一幅箱線圖。
并設(shè)定間距:
plt.subplots_adjust(left=0.1, right=0.9, bottom=0.1, top=0.9, wspace=0.3, hspace=0.3)
代碼如下:
import matplotlib.pyplot as plt import numpy as np plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False # 位置221 畫一幅簡(jiǎn)單的折線圖 fig = plt.figure(1, facecolor='#33ff99', figsize=(10, 6)) ax1 = plt.subplot(221) ax1.set_title('ax1') ax1.set_facecolor("orange") ax1.plot([1, 1, 0, 0, -1, 0, 1, 1, -1], c='r') # 位置222 或一個(gè)橫軸為月份,的散點(diǎn)圖 ax2 = plt.subplot(222) ax2.set_title('ax2') ax2.set_facecolor("purple") ax2.plot(['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月'], [1, 0, 2, 5, 3, 5, 8, 7, 9], ls='', marker='*') # 位置223 繪制一份柱狀圖 ax3 = plt.subplot(223) ax3.set_title('ax3') ax3.set_facecolor("pink") ax3.bar(['A類', 'B類', 'C類', 'D類', 'E類'], height=[200, 350, 600, 540, 430], color='#9900ff') # 位置224 繪制一張箱線圖 ax4 = plt.subplot(224) ax4.set_title('ax4') np.random.seed(100) data = np.random.randint(0, 100, (4, 4)) ax4.set_facecolor("blue") ax4.boxplot(data, labels=('Open', 'High', 'Low', 'Close')) # 添加標(biāo)題 ax1.set_title('折線圖') ax2.set_title('散點(diǎn)圖') ax3.set_title('柱形圖') ax4.set_title('箱線圖') plt.subplots_adjust(left=0.1, right=0.9, bottom=0.1, top=0.9, wspace=0.3, hspace=0.3) plt.show()
生成圖像效果如下:
原文鏈接:https://blog.csdn.net/weixin_48964486/article/details/124083946
相關(guān)推薦
- 2022-11-04 python使用tqdm模塊處理文件閱讀進(jìn)度條顯示_python
- 2022-08-02 深入了解Golang的map增量擴(kuò)容_Golang
- 2022-06-26 python如何利用matplotlib繪制并列雙柱狀圖并標(biāo)注數(shù)值_python
- 2022-08-19 React組件通信
- 2022-10-04 golang中time包之時(shí)間間隔格式化和秒、毫秒、納秒等時(shí)間戳格式輸出的方法實(shí)例_Golang
- 2022-07-18 Linux文件系統(tǒng)和日志分析
- 2023-01-13 Pytorch如何加載自己的數(shù)據(jù)集(使用DataLoader讀取Dataset)_python
- 2023-01-26 Redis的數(shù)據(jù)復(fù)制過程詳解_Redis
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支