Python 3.x 向colorbar添加第二个标签
我有一个带有色条的Python 3.x 向colorbar添加第二个标签,python-3.x,matplotlib,colorbar,imshow,Python 3.x,Matplotlib,Colorbar,Imshow,我有一个带有色条的imshow绘图。我希望在颜色栏中有两个标签,一个在左侧,另一个在右侧 这是mve: V = np.array([[1, 2, 3], [4, 5, 6]]) # Just a sample array plt.imshow(V, cmap = "hot", interpolation = 'none') clb = plt.colorbar() clb.set_label("Firstlabel", fontsize=10, labelpad=-40, y=0.5, ro
imshow
绘图。我希望在颜色栏中有两个标签,一个在左侧,另一个在右侧
这是mve:
V = np.array([[1, 2, 3], [4, 5, 6]]) # Just a sample array
plt.imshow(V, cmap = "hot", interpolation = 'none')
clb = plt.colorbar()
clb.set_label("Firstlabel", fontsize=10, labelpad=-40, y=0.5, rotation=90)
#clb.set_label("SECONDLABEL") # This is the label I want to add
plt.savefig("Example")
这将产生:
我想在颜色栏的右边再贴一个标签。如果我使用注释线,第二个颜色条将添加到绘图中,这不是我想要的。如何执行此操作?您不能有两个
标签
对象,但可以使用clb.ax.text
添加第二个标签
另外,请注意,要将第一个标签移到左侧,可以使用clb.ax.yaxis.set_label\u position('left')
而不是labelpad=-40
因此,使用线条:
clb = plt.colorbar()
clb.set_label("Firstlabel", fontsize=10, y=0.5, rotation=90)
clb.ax.yaxis.set_label_position('left')
clb.ax.text(2.5, 0.5, "SECONDLABEL", fontsize=10, rotation=90, va='center')
生成此图: