定位颜色条 [英] Positioning the colorbar

查看:23
本文介绍了定位颜色条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个附有颜色条的 matplotlib 图.我想将颜色条定位为水平的,并在我的绘图下方.

我几乎通过以下方式做到了这一点:

plt.colorbar(orientation="horizo​​ntal",fraction=0.07,anchor=(1.0,0.0))

但是颜色条仍然与绘图稍微重叠(以及 x 轴的标签).我想将颜色条进一步向下移动,但我不知道该怎么做.

解决方案

Edit:针对 matplotlib 版本更新 >= 3.

在此答案中

I have a matplotlib plot with a colorbar attached. I want to position the colorbar so that it is horizontal, and underneath my plot.

I have almost done this via the following:

plt.colorbar(orientation="horizontal",fraction=0.07,anchor=(1.0,0.0))

But the colorbar is still overlapping with the plot slightly (and the labels of the x axis). I want to move the colorbar further down, but I can't figure out how to do it.

解决方案

Edit: Updated for matplotlib version >= 3.

Three great ways to do this have already been shared in this answer.

The matplotlib documentation advises to use inset_locator. This would work as follows:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import numpy as np

rng = np.random.default_rng(1)

fig, ax = plt.subplots(figsize=(4,4))
im = ax.imshow(rng.random((11, 16)))
ax.set_xlabel("x label")

axins = inset_axes(ax,
                    width="100%",  
                    height="5%",
                    loc='lower center',
                    borderpad=-5
                   )
fig.colorbar(im, cax=axins, orientation="horizontal")

这篇关于定位颜色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆