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
What am I doing wrong?
import matplotlib.pyplot as plt
fig = plt.figure(frameon=False)
ax = plt.Axes(fig, [0.0, 0.0, 1.0, 1.0])
renderer = fig.canvas.get_renderer()
abc = plt.text(0, 0, 'abc', fontsize=20, bbox=dict(facecolor='red',
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))