Can't place text nest to another one

Hello everybody,

Please, take a look at the code below. I want to place the text 'xyz'
just next to 'abc'. The reusult is that 'xyz' goes much further right to
'abc' and there is a gap between the texts.

The width of the bbox happens to be 40, as the printout shows. The 'xyz'
text is indeed at 40. However, one can see on the canvas that 'abc'
width is only about 6.5. Thus, there is a mismatch between what
get_window_extent calculates (40) and what the text size is on the
canvas (6.5).

What am I doing wrong?

Best Regards,
Ryszard

import matplotlib.pyplot as plt

fig = plt.figure(frameon=False)
ax = plt.Axes(fig, [0.0, 0.0, 1.0, 1.0])
ax.set_axis_off()
ax.set_xlim(0, 100)
ax.set_ylim(0, 100)
fig.add_axes(ax)
renderer = fig.canvas.get_renderer()
abc = plt.text(0, 0, 'abc', fontsize=20, bbox=dict(facecolor='red',
alpha=0.2, pad=0))
bbox = abc.get_window_extent(renderer=renderer)
print('width:', bbox.width, 'height:', bbox.height)
xyz = plt.text(bbox.width, 0, 'xyz', fontsize=20,
bbox=dict(facecolor='blue', alpha=0.2, pad=0))
plt.show()

The problem is that we do not know how big the text will be until we
actually render it so `abc.get_window_extent` is lying.

I suggest looking at
https://matplotlib.org/tutorials/text/annotations.html#using-complex-coordinates-with-annotations
which
shows how to defer the calling of `get_window_extent` until as late as
possible.

Tom

···

On Mon, Apr 9, 2018 at 1:35 PM Ryszard Kubiak <rhkubiak at gmail.com> wrote:

Hello everybody,

Please, take a look at the code below. I want to place the text 'xyz'
just next to 'abc'. The reusult is that 'xyz' goes much further right to
'abc' and there is a gap between the texts.

The width of the bbox happens to be 40, as the printout shows. The 'xyz'
text is indeed at 40. However, one can see on the canvas that 'abc'
width is only about 6.5. Thus, there is a mismatch between what
get_window_extent calculates (40) and what the text size is on the
canvas (6.5).

What am I doing wrong?

Best Regards,
Ryszard

import matplotlib.pyplot as plt

fig = plt.figure(frameon=False)
ax = plt.Axes(fig, [0.0, 0.0, 1.0, 1.0])
ax.set_axis_off()
ax.set_xlim(0, 100)
ax.set_ylim(0, 100)
fig.add_axes(ax)
renderer = fig.canvas.get_renderer()
abc = plt.text(0, 0, 'abc', fontsize=20, bbox=dict(facecolor='red',
alpha=0.2, pad=0))
bbox = abc.get_window_extent(renderer=renderer)
print('width:', bbox.width, 'height:', bbox.height)
xyz = plt.text(bbox.width, 0, 'xyz', fontsize=20,
bbox=dict(facecolor='blue', alpha=0.2, pad=0))
plt.show()

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
Matplotlib-users Info Page

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20180413/5a911e15/attachment.html&gt;

Thank you, Thomas, for your reply. You are right that I have to use an
appropriate transofrm to turn canvas points yielded by
get_window_extent() to the actual Axes's coordinates. The
transforms.offset_copy(...) function came to my rescue.

Thanks again,
Ryszard

The problem is that we do not know how big the text will be until we
actually render it so `abc.get_window_extent` is lying.

I suggest looking at
https://matplotlib.org/tutorials/text/annotations.html#using-complex-coordinates-with-annotations?which
shows how to defer the calling of `get_window_extent` until as late as
possible.

Tom

    Hello everybody,

    Please, take a look at the code below. I want to place the text 'xyz'
    just next to 'abc'. The reusult is that 'xyz' goes much further
    right to
    'abc' and there is a gap between the texts.

    The width of the bbox happens to be 40, as the printout shows. The
    'xyz'
    text is indeed at 40. However, one can see on the canvas that 'abc'
    width is only about 6.5. Thus, there is a mismatch between what
    get_window_extent calculates (40) and what the text size is on the
    canvas (6.5).

    What am I doing wrong?

    Best Regards,
    Ryszard

    import matplotlib.pyplot as plt

    fig = plt.figure(frameon=False)
    ax = plt.Axes(fig, [0.0, 0.0, 1.0, 1.0])
    ax.set_axis_off()
    ax.set_xlim(0, 100)
    ax.set_ylim(0, 100)
    fig.add_axes(ax)
    renderer = fig.canvas.get_renderer()
    abc = plt.text(0, 0, 'abc', fontsize=20, bbox=dict(facecolor='red',
    alpha=0.2, pad=0))
    bbox = abc.get_window_extent(renderer=renderer)
    print('width:', bbox.width, 'height:', bbox.height)
    xyz = plt.text(bbox.width, 0, 'xyz', fontsize=20,
    bbox=dict(facecolor='blue', alpha=0.2, pad=0))
    plt.show()

    _______________________________________________
    Matplotlib-users mailing list
    Matplotlib-users at python.org <mailto:Matplotlib-users at python.org>
    Matplotlib-users Info Page

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20180415/f7b42ee3/attachment.html&gt;

···

On Mon, Apr 9, 2018 at 1:35 PM Ryszard Kubiak <rhkubiak at gmail.com > <mailto:rhkubiak at gmail.com>> wrote: