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

这是我从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()

但这是我不想要的输出。

http://img4.mukewang.com/60c862aa0001629f05980463.jpg

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


摇曳的蔷薇
浏览 336回答 1
1回答

四季花海

轴标签 ( ylabel) 设计为沿相应轴放置。甲标题,而另一方面,是由设计中,位于上居中的轴对象。因此set_ylabel,您应该使用 ,而不是使用set_title。cbar.ax.set_title('$[M_\u2609 kpc^{{-2}}]$', fontsize=20)有关图形不同部分的更多信息,请参阅此示例“图形剖析”
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python