[英]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()
但这是我不想要的输出。
我想要一个正好位于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.