urschrei
urschrei

Reputation: 26859

Align matplotlib Text with colorbar

I'd like to align the bottom edge of a Text() instance with the bottom edge of a colorbar instance, but it's not working the way I expected:

I'm obtaining the colorbar y position using cb.ax.get_position().ymin, and then setting my text object's y position like so:

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)

I've tried altering the va, but the two are never aligned: using va=bottom the middle of the text appears to be aligned with the middle of the colorbar; using va=center, the text box ymin is below the (apparent) cb ymin. What's the best way to get and set the coordinates?

Upvotes: 3

Views: 1139

Answers (1)

CT Zhu
CT Zhu

Reputation: 54340

How does this look, just directly plot to 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

Upvotes: 5

Related Questions