如何使用Python修改matplotlib.pyplot.colorbar的位置以对齐主图
作者:r0nny 发布时间:2021-09-28 18:01:30
标签:python,matplotlib.pyplot.colorbar,位置
问题描述
像这样的图,我想把右边的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
0
投稿
猜你喜欢
- 01. 应用场景及定义方式应用场景在实际开发中,对象 的 某些属性或方法 可能只希望 在对象的内部被使用,而 不希望在外部被访问到私有属性
- python语言最常见的括号有三种,分别是:小括号( )、中括号[ ]和大括号也叫做花括号{ }。其作用也各不相同,分别用来代表不同的pyt
- 阅读上一篇:W3C优质网页小贴士(三)明智地选择 URI没有什么比走到你最喜欢的商店门口,却发现店门紧闭,而且没有看见店面搬迁告示这种事情还
- 计算年、月、日需要安装组件包pip install python-dateutil当前日期时间import datetimeprint da
- 问题:想装tfx,但是提示不支持python3.9。解决方案:新建一个环境tfx专门用来运行流水线,这个环境安装python3.8。1.查看
- 前言Golang语言有诸多优点:静态编译、协程、堪比c语言的高性能。但是也有一些令人发指的地方 —— 经常被人调侃 五行代码,三行错误处理
- 本文实例讲述了Python 字符串、列表、元组的截取与切片操作。分享给大家供大家参考,具体如下:demo.py(字符串、列表、元组的截取):
- 新版Django在admin后台直接删除一条数据时,file = models.FileField(upload_to=‘file'
- 1 from multiprocessing import Pool,Queue。其中Queue在Pool中不起作用,具体原因未明。解决方案
- 苦逼的前夜昨晚很辛苦,搞到晚上快两点,最后还是没有把python3下的scrapy框架安装起来,后面还把yum这玩意给弄坏了,一直找不到命令
- 前言前几天去我姐家里蹭饭吃,发现我姐家里的小外甥女儿已经上小学了,正在疯狂赶作业,加减乘除还有很多题目都经常出错。我姐说老师每天给他们布置了
- 使用python + shell 编写,是一个简易solaris系统巡检程序#!/usr/bin/python -u#-*- coding:
- 一、了解字符编码的知识储备1. 文本编辑器存取文件的原理(nodepad++,pycharm,word) 打开编辑器就打开了启动了
- 使用cv2对视频进行切割import cv2def clip_video(source_video, target_video, start
- 最近各地中小学都在开展线上教学,有些不自觉的小朋友们用电脑在线学习的时候会趁家长不在的时候偷偷玩游戏、看漫画。本程序screenshot.p
- 1. 范数示例代码:import torcha = torch.full([8], 1)b = a.reshape([2, 4])c = a
- 本次的7个python爬虫小案例涉及到了re正则、xpath、beautiful soup、selenium等知识点,非常适合刚入门pyth
- 处理办法,删除该文件,或清空该文件内容;我的处理是清空后,再设置该文件权限为Everyone拒绝访问。
- 一、json_encode() 对变量进行JSON编码语法:json_encode($value[,$options=0])注意: 
- 手写数字识别(小白入门)今早刚刚上了节实验课,关于逻辑回归,所以手有点刺挠就想发个博客,作为刚刚入门的小白,看到代码运行成功就有点小激动,这