Can't place text nest to another one

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Can't place text nest to another one

Ryszard Kubiak
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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Can't place text nest to another one

tcaswell
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 <[hidden email]> 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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Can't place text nest to another one

Ryszard Kubiak
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

On Mon, Apr 9, 2018 at 1:35 PM Ryszard Kubiak <[hidden email]> 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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users



_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users