網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
如何使用Python修改matplotlib.pyplot.colorbar的位置以對(duì)齊主圖_python
作者:r0nny ? 更新時(shí)間: 2022-08-25 編程語(yǔ)言問(wèn)題描述
像這樣的圖,我想把右邊的colorbar設(shè)置成和主圖一樣高度
方法
0. plt.colorbar參數(shù)介紹
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調(diào)節(jié)以找到合適大小
但當(dāng)圖像的長(zhǎng)、高對(duì)比過(guò)大時(shí),可能不起作用
2. make_axes_locatable
這是matplotlib包里的一個(gè)函數(shù)
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)
不適用于有投影參數(shù)的axe(會(huì)報(bào)錯(cuò)),如cartopy的GeoAxes
3. make_axes_locatables 升級(jí)版
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. 手動(dòng)給colorbar添加一個(gè)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)
效果展示
總結(jié)?
原文鏈接:https://blog.csdn.net/qq_42471423/article/details/116133697
相關(guān)推薦
- 2023-12-02 vscode無(wú)法連接寶塔ftp排雷
- 2022-07-24 搭建React?Native熱更新平臺(tái)的詳細(xì)過(guò)程_React
- 2023-10-28 如何給k8s集群里的資源打標(biāo)簽_云其它
- 2022-07-19 Linux手工配置靜態(tài)ip地址
- 2023-04-29 Linux?grep?-q用法示例詳解_linux shell
- 2022-08-20 如何利用python實(shí)現(xiàn)Simhash算法_python
- 2022-10-22 C#?設(shè)置Chart的X軸為時(shí)間軸???????詳情_(kāi)C#教程
- 2023-12-20 Mybatis中resultMap結(jié)果集的使用
- 最近更新
-
- 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)證過(guò)濾器
- Spring Security概述快速入門(mén)
- 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)程分支