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

Python 如何将水平matplotlib colorbar对象的标签精确定位在颜色栏中心的上方或下方?,python,image,matplotlib,axis-labels,colorbar,Python,Image,Matplotlib,Axis Labels,Colorbar,下面是我从中借用的代码,稍作修改,为带有正确放置标签的图像生成水平色条: 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 ne

下面是我从中借用的代码,稍作修改,为带有正确放置标签的图像生成水平色条:

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()
但这是我不想要的输出

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

轴标签(
ylabel
)设计为沿相应的轴放置。另一方面,根据设计,标题位于轴对象的中心。因此,与其使用
set\u ylabel
,不如使用
set\u title

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

有关地物不同部分的详细信息,请参见

轴标签(
ylabel
)设计为沿相应的轴放置。另一方面,根据设计,标题位于轴对象的中心。因此,与其使用
set\u ylabel
,不如使用
set\u title

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

有关地物不同部分的详细信息,请参见


请提交一个最小的可复制示例:一小段代码可以复制您的问题,但完全是独立的。也要考虑指南。请提交一个最小的可重复的例子:一个代码复制您的问题,但完全自给自足。同时考虑指导方针。