網站首頁 編程語言 正文
問題
在使用matplotlib
作圖的時候,有的時候會遇到畫圖時坐標軸重疊,顯示不全和圖片保存時不完整的問題。如下:
解決方案
畫圖時重疊或者顯示不全的問題
- 畫圖時加上參數設置
tight_layout=True
- 畫完所有子圖后,設置
plt.tight_layout()
# 加上tight_layout=True 參數設置 fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(12,6), tight_layout=True) labels = ['2022-{}-01'.format(str(i).zfill(2)) for i in range(1,13)] y1 = np.random.randint(low=1, high=10, size=12) y2 = np.random.randint(low=1, high=10, size=12) axes[0].bar(labels, y1) axes[0].set_xticks(labels) axes[0].set_xticklabels(labels, rotation=75) axes[1].bar(labels, y2) axes[1].set_xticks(labels) axes[1].set_xticklabels(labels, rotation=75) plt.savefig('test.png', dpi=200)
fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(12,6)) labels = ['2022-{}-01'.format(str(i).zfill(2)) for i in range(1,13)] y1 = np.random.randint(low=1, high=10, size=12) y2 = np.random.randint(low=1, high=10, size=12) axes[0].bar(labels, y1) axes[0].set_xticks(labels) axes[0].set_xticklabels(labels, rotation=75) axes[1].bar(labels, y2) axes[1].set_xticks(labels) axes[1].set_xticklabels(labels, rotation=75) plt.tight_layout() # 這里加上plt.tight_layout(),記住要在畫完所有子圖后 plt.savefig('test.png', dpi=200)
效果如下:
保存時圖片不完整的問題
在plt.savefig中加入bbox_inches='tight’參數設置
fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(12,6), tight_layout=True) labels = ['2022-{}-01'.format(str(i).zfill(2)) for i in range(1,13)] y1 = np.random.randint(low=1, high=10, size=12) y2 = np.random.randint(low=1, high=10, size=12) axes[0].bar(labels, y1) axes[0].set_xticks(labels) axes[0].set_xticklabels(labels, rotation=75) axes[1].bar(labels, y2) axes[1].set_xticks(labels) axes[1].set_xticklabels(labels, rotation=75) plt.savefig('test.png', dpi=600, bbox_inches='tight') # savefig時加入bbox_inches='tight'參數設置
補充:使用matplotlib時如何讓坐標軸完整顯示所有的標簽刻度
在使用matplotlib時,由于數據量過大,在默認設置下,使用plot等時,會將x,y軸的刻度按一定比例縮減掉一部分,如:1,2,3,4,5,6,7,8,9,10會顯示成2,4,6,8,10等,解決方法是使用xticks()和yticks():
xticks(ticks=None, labels=None, **kwargs)
ticks:x軸顯示的數值(類型:list),若為空,則x軸上不會顯示任何數值;
labels:標簽項,當ticks不為空時,labels內容將覆蓋ticks的內容
**kwargs:控制標簽內容的排版
Return:
- locs:返回x軸坐標列表
- labels:返回x軸標簽列表
總結
原文鏈接:https://blog.csdn.net/weixin_39925939/article/details/122702591
相關推薦
- 2022-09-04 c++?求數組最大最小值函數的實現_C 語言
- 2023-01-19 Pycharm?2to3配置,python2轉python3方式_python
- 2022-10-18 Qt實現TCP客戶端和服務器通訊程序_C 語言
- 2022-07-18 Redis服務器連接本地Linux所踩的坑
- 2022-06-19 python繪制橫向水平柱狀條形圖_python
- 2022-10-19 使用docker安裝elk的詳細步驟_docker
- 2022-09-12 PyQt轉換路徑中的斜杠(斜杠(/)與反斜杠(\)轉換)_python
- 2022-07-06 C#winform中數據庫綁定DataGrid的實現_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支