python - 如何将水平 matplotlib 颜色条对象的标签精确定位在颜色条下方或上方的中心?

标签 python image matplotlib axis-labels colorbar

这是我从matplotlib sample examples借用的代码稍加修改即可为带有正确放置标签的图像生成水平颜色条:

from matplotlib import colors
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19680801)
cmap = "cool"

fig, axs = plt.subplots()

# Generate data with a range that varies from one plot to the next.
data = (1 / 10) * np.random.rand(10, 20) * 1e-6
image = axs.imshow(data, cmap=cmap)
axs.label_outer()

# Find the min and max of all colors for use in setting the color scale.
vmin = image.get_array().min()
vmax = image.get_array().max()
norm = colors.Normalize(vmin=vmin, vmax=vmax)

cbar = fig.colorbar(image, ax=axs, orientation='horizontal', fraction=.1)
cbar.ax.set_ylabel('$[M_\u2609 kpc^{{-2}}]$', position=(30,1), fontsize=20, rotation=0)


plt.show()

但是这是我不想要的输出。

Figure1

我想要一个正好位于颜色条中心的标签(下方或上方)。更改上面代码中 position 的坐标后,在确定标签的位置方面似乎没有任何灵 active 。在 matplotlib 颜色条的上下文中是否有我不知道的事情?非常感谢您的帮助。

最佳答案

轴标签 (ylabel) 设计为沿着相应的轴放置。另一方面,根据设计,标题位于坐标区对象的中心。因此,您应该使用 set_title,而不是使用 set_ylabel

cbar.ax.set_title('$[M_\u2609 kpc^{{-2}}]$', fontsize=20)

enter image description here

有关图形不同部分的更多信息,请参阅 this example "Anatomy of a Figure"

enter image description here

关于python - 如何将水平 matplotlib 颜色条对象的标签精确定位在颜色条下方或上方的中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52269771/

相关文章:

python - 将 python 字符串日期转换为 mysql 日期时间

html - 悬停时图像宽度为 'malfunctioning'(webkit),如何解决?

ios - 在 ios 中上传后如何从 firebase 获取缩略图?

python - 当绘制在强度之上时,颤抖图会发生变化

python - 如何为按时间排序的 matplotlib 图序列制作动画

python - 使用数字作为标签名称来解析损坏的 XML

python - 拆分成 R 中的训练和测试集?

python - 创建类似于给定模板的虚假数据

silverlight - WP7/Silverlight 超链接图片

python - 使用 matplotlib 在数据帧的其他列的条件下针对相同的 x 轴绘制列值