matplotlib中颜色条标签的对齐方式

yam*_*yam 2 python label matplotlib alignment colorbar

我有一个横向整个画布的水平颜色贴图,两端的标签显示最小值和最大值.问题是,对于不同的最小值和最大值,标签会向外伸出更多,从而改变图像的整体尺寸,这是我不想要的.

我想将左侧的标签(图像中的-40000)与左侧的标签对齐(图像中的40000),以解决此问题,但我无法弄清楚如何做到这一点.

所以我的问题很简单,如何更改颜色条中标签的对齐方式? 在此输入图像描述

hit*_*tzg 6

这应该让你开始:

import matplotlib.pyplot as plt
import numpy as np

# some random data to plot
x = np.random.rand(10,10)*80e3 - 40e3
plt.imshow(x, aspect='auto', vmin=-40e3, vmax=40e3)

# create the colorbar
cb = plt.colorbar(orientation='horizontal', ticks=[-40e3,0,40e3])

# get the xtick labels
tl = cb.ax.get_xticklabels()

# set the alignment for the first and the last
tl[0].set_horizontalalignment('left')
tl[-1].set_horizontalalignment('right')

plt.show()
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述