繁体   English   中英

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

[英]How to position the label of a horizontal matplotlib colorbar object exactly at the center of the colorbar below or above?

这是我从matplotlib示例中借用的代码,稍作修改,为具有正确放置标签的图像生成水平颜色条:

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()

但这是我不想要的输出。

图1

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

轴标签( ylabel )设计为沿相应的轴放置。 另一方面, 标题通过设计定位在轴对象的中心。 因此,而不是使用set_ylabel ,你应该使用set_title

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

在此输入图像描述

有关图形不同部分的更多信息,请参阅此示例“图的剖析”

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM