简体   繁体   中英

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?

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>

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM