How do I adjust (offset) colorbar title in matplot

2020-05-23 03:47发布

Given the following code:

imshow(np.arange(16*16).reshape(16,16))
cb = colorbar()
cb.set_label("Foo")
cb.set_ticks([0,255])

Which produces:

enter image description here

How do I adjust the colorbar text "Foo" so that it is offset to the left, betwen the 0 and 255, closer to the colorbar, reducing the un-needed whitespace?

2条回答
我欲成王,谁敢阻挡
2楼-- · 2020-05-23 03:55
cb.set_label("Foo",horizontalalignment='right')

The label control with this function is very poor...


You could do:

cb = colorbar()
cb.set_ticks([0,255])
ax = cb.ax
ax.text(1.3,0.5,'Foo',rotation=90)
查看更多
老娘就宠你
3楼-- · 2020-05-23 04:19
cb.set_label("Foo", labelpad=-1)

Negative labelpad values will move closer to the bar, positive away.

查看更多
登录 后发表回答