網站首頁 編程語言 正文
問題描述
像這樣的圖,我想把右邊的colorbar設置成和主圖一樣高度
方法
0. plt.colorbar參數介紹
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.colorbar.html?highlight=colorbar#matplotlib.pyplot.colorbar
1. plt.colorbar(im, fraction=0.046, pad=0.04, shrink=1.0)
fraction可以從0.035-0.046調節以找到合適大小
但當圖像的長、高對比過大時,可能不起作用
2. make_axes_locatable
這是matplotlib包里的一個函數
from mpl_toolkits.axes_grid1 import make_axes_locatable
im = plt.imshow(data)
divider = make_axes_locatable(plt.gca())
cax = divider.append_axes("right", size="5%", pad="3%")
plt.colorbar(im, cax=cax)
不適用于有投影參數的axe(會報錯),如cartopy的GeoAxes
3. make_axes_locatables 升級版
https://stackoverflow.com/questions/18195758/set-matplotlib-colorbar-size-to-match-graph @Matthias
import matplotlib.pyplot as plt
from mpl_toolkits import axes_grid1
def add_colorbar(im, aspect=20, pad_fraction=0.5, **kwargs):
"""Add a vertical color bar to an image plot."""
divider = axes_grid1.make_axes_locatable(im.axes)
width = axes_grid1.axes_size.AxesY(im.axes, aspect=1./aspect)
pad = axes_grid1.axes_size.Fraction(pad_fraction, width)
current_ax = plt.gca()
cax = divider.append_axes("right", size=width, pad=pad)
plt.sca(current_ax)
return im.axes.figure.colorbar(im, cax=cax, **kwargs)
用法示例
im = plt.imshow(np.arange(200).reshape((20, 10)))
add_colorbar(im)
4. 手動給colorbar添加一個axe
https://stackoverflow.com/questions/18195758/set-matplotlib-colorbar-size-to-match-graph @Fei Yao
import matplotlib.pyplot as plt
import numpy as np
fig=plt.figure()
ax = plt.axes()
im = ax.imshow(np.arange(100).reshape((10,10)))
# Create an axes for colorbar. The position of the axes is calculated based on the position of ax.
# You can change 0.01 to adjust the distance between the main image and the colorbar.
# You can change 0.02 to adjust the width of the colorbar.
# This practice is universal for both subplots and GeoAxes.
cax = fig.add_axes([ax.get_position().x1+0.01,ax.get_position().y0,0.02,ax.get_position().height])
plt.colorbar(im, cax=cax) # Similar to fig.colorbar(im, cax = cax)
效果展示
總結?
原文鏈接:https://blog.csdn.net/qq_42471423/article/details/116133697
相關推薦
- 2022-06-15 golang中net的tcp服務使用_Golang
- 2022-06-19 教你用vbs實現微信自動發送消息功能_vbs
- 2022-09-22 vrrp協議與keepalived淺析
- 2023-07-16 uniapp 中 uni.navigateTo跳轉其他頁面 并且帶參數
- 2022-09-17 詳解redis集群的三種方式_Redis
- 2022-04-20 基于PyQT5制作一個桌面摸魚工具_python
- 2022-12-12 python字符串中兩個大括號{{}}的使用及說明_python
- 2022-07-14 C++深入淺出講解希爾排序算法的實現_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同步修改后的遠程分支