Adding a second label to colorbar

1.5k views Asked by At

I have an imshow plot with a colorbar. I want two labels in the colorbar, one on the left side and the other one on the right side.

This is the 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")

This produces:

enter image description here

I want a second label on the right side of the colorbar. If I use the commented line a second colorbar is added to my plot, and that is not what I want. How can I do this?

1

There are 1 answers

0
tmdavison On

You can't have two label objects, but you could add a second label using clb.ax.text.

Also, note that to move the first label to the left hand side, you could use clb.ax.yaxis.set_label_position('left') rather than labelpad=-40

So, using lines:

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

Produces this figure:

enter image description here