将matplotlib文本与colorbar对齐

时间:2013-09-24 15:30:25

标签: python matplotlib

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

我正在使用cb.ax.get_position().ymin获取colorbar y位置,然后像这样设置我的文本对象的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)

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

1 个答案:

答案 0 :(得分:4)

这看起来如何,只是直接绘制到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>

enter image description here