<div dir="ltr">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.<div><br></div><div>I suggest looking at <a href="https://matplotlib.org/tutorials/text/annotations.html#using-complex-coordinates-with-annotations">https://matplotlib.org/tutorials/text/annotations.html#using-complex-coordinates-with-annotations</a> which shows how to defer the calling of `get_window_extent` until as late as possible.</div><div><br></div><div>Tom</div></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Apr 9, 2018 at 1:35 PM Ryszard Kubiak <<a href="mailto:rhkubiak@gmail.com">rhkubiak@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello everybody,<br>
<br>
Please, take a look at the code below. I want to place the text 'xyz'<br>
just next to 'abc'. The reusult is that 'xyz' goes much further right to<br>
'abc' and there is a gap between the texts.<br>
<br>
The width of the bbox happens to be 40, as the printout shows. The 'xyz'<br>
text is indeed at 40. However, one can see on the canvas that 'abc'<br>
width is only about 6.5. Thus, there is a mismatch between what<br>
get_window_extent calculates (40) and what the text size is on the<br>
canvas (6.5).<br>
<br>
What am I doing wrong?<br>
<br>
Best Regards,<br>
Ryszard<br>
<br>
import matplotlib.pyplot as plt<br>
<br>
fig = plt.figure(frameon=False)<br>
ax = plt.Axes(fig, [0.0, 0.0, 1.0, 1.0])<br>
ax.set_axis_off()<br>
ax.set_xlim(0, 100)<br>
ax.set_ylim(0, 100)<br>
fig.add_axes(ax)<br>
renderer = fig.canvas.get_renderer()<br>
abc = plt.text(0, 0, 'abc', fontsize=20, bbox=dict(facecolor='red',<br>
alpha=0.2, pad=0))<br>
bbox = abc.get_window_extent(renderer=renderer)<br>
print('width:', bbox.width, 'height:', bbox.height)<br>
xyz = plt.text(bbox.width, 0, 'xyz', fontsize=20,<br>
bbox=dict(facecolor='blue', alpha=0.2, pad=0))<br>
plt.show()<br>
<br>
_______________________________________________<br>
Matplotlib-users mailing list<br>
<a href="mailto:Matplotlib-users@python.org" target="_blank">Matplotlib-users@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/matplotlib-users" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/matplotlib-users</a><br>
</blockquote></div>