[Matplotlib-users] Can't place text nest to another one
Ryszard Kubiak
rhkubiak at gmail.com
Mon Apr 9 13:34:38 EDT 2018
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()
More information about the Matplotlib-users
mailing list