将 matplotlib 文本与颜色条对齐

urs*_*rei 3 python matplotlib

我想将实例的底部边缘Text()与实例的底部边缘对齐colorbar,但它没有按我预期的方式工作:

我使用 获取颜色条 y 位置cb.ax.get_position().ymin,然后设置文本对象的 y 位置,如下所示:

cb = plt.colorbar(mappable, shrink=0.5)

details = plt.text(
    1., 1.,
    "Text",
    ha='right', va='bottom',
    size=5,
    color='#555555',
    transform=ax.transAxes,
    fontdict={
        'family': 'Helvetica',
        'size': 6})
details.set_y(cb.ax.get_position().ymin)
Run Code Online (Sandbox Code Playgroud)

我尝试过更改va,但两者从未对齐:使用va=bottom文本的中间似乎与颜色条的中间对齐;使用va=center,文本框 ymin 位于(明显的)cb ymin 下方。获取和设置坐标的最佳方法是什么?

CT *_*Zhu 5

看起来怎么样,直接绘制到cb.ax

>>> x=linspace(-4,4)
>>> y=linspace(-4,4)
>>> g=meshgrid(x,y)
>>> z=g[0]**2+5*g[1]
>>> ctf=plt.contourf(x, y, z)
>>> cb=plt.colorbar(ctf, shrink=0.5)
>>> cb.ax.text(0.5, 0, 'text', va='top', ha='center')
<matplotlib.text.Text object at 0x9173670>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述