Closed
Description
Bug report
Follow up #11468
If we have multiline text with big descenders (i.e. $\sum_i x$
) then the layout mechanism in Text._get_layout()
doesn't handle the big descenders properly. Note that _get_layout()
gives us the bounding box for text.get_window_extent
so this causes problems for anything that needs to know where the text is and what its size is.
- The layout is fine for single lines even large descenders. (first three text objects on each line)
- For two lines with the same descend the layout is fine (last text object on each line)
- For mixed size descenders (
test
and$\sum_i x$
) the layout is erroneous. If the big descender comes first, the layout box is too big; conversely if it comes second.
I keep chasing around solutions, but the algorithm is a bit idiosyncratic so thought I'd post here if anyone had institutional knowledge.
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
fig, ax = plt.subplots(constrained_layout=False)
ax.set_xlim([0, 1.4])
ax.set_ylim([0, 2])
ax.axhline(0.5, color='C2', linewidth=0.3)
sts = ['Line', '2 Lines g\n 2nd Line g', '$\sum_i x $', 'hi $\sum_i x $\ntest', 'test\n $\sum_i x $', '$\sum_i x $\n $\sum_i x $']
for nn, st in enumerate(sts):
tt = ax.text(0.2 * nn + 0.1, 0.5, st, horizontalalignment='center',
verticalalignment='bottom')
bb = tt.get_window_extent(fig.canvas.get_renderer())
bbt = bb.inverse_transformed(ax.transAxes)
r = mpatches.Rectangle((0,0), 1, 1, clip_on=False, transform=ax.transAxes)
r.set_bounds(bbt.bounds)
ax.add_patch(r)
ax.text(1.2, 0.5, 'Bottom align', color='C2')
if 1:
ax.axhline(1.3, color='C2', linewidth=0.3)
for nn, st in enumerate(sts):
tt = ax.text(0.2 * nn + 0.1, 1.3, st, horizontalalignment='center',
verticalalignment='top')
bb = tt.get_window_extent(fig.canvas.get_renderer())
bbt = bb.inverse_transformed(ax.transAxes)
r = mpatches.Rectangle((0,0), 1, 1, clip_on=False, transform=ax.transAxes)
r.set_bounds(bbt.bounds)
ax.add_patch(r)
ax.text(1.2, 1.3, 'Top align', color='C2')
ax.axhline(1.8, color='C2', linewidth=0.3)
for nn, st in enumerate(sts):
tt = ax.text(0.2 * nn + 0.1, 1.8, st, horizontalalignment='center',
verticalalignment='baseline')
bb = tt.get_window_extent(fig.canvas.get_renderer())
bbt = bb.inverse_transformed(ax.transAxes)
r = mpatches.Rectangle((0,0), 1, 1, clip_on=False, transform=ax.transAxes)
r.set_bounds(bbt.bounds)
ax.add_patch(r)
ax.text(1.2, 1.8, 'Baseline align', color='C2')
ax.axhline(0.1, color='C2', linewidth=0.3)
for nn, st in enumerate(sts):
tt = ax.text(0.2 * nn + 0.1, 0.1, st, horizontalalignment='center',
verticalalignment='bottom', rotation=20)
bb = tt.get_window_extent(fig.canvas.get_renderer())
bbt = bb.inverse_transformed(ax.transAxes)
r = mpatches.Rectangle((0,0), 1, 1, clip_on=False, transform=ax.transAxes)
r.set_bounds(bbt.bounds)
ax.add_patch(r)
ax.text(1.2, 0.1, 'Bot align, rot20', color='C2')
bb = tt.get_window_extent(fig.canvas.get_renderer())
bbt = bb.inverse_transformed(ax.transAxes)
r = mpatches.Rectangle((0,0), 1, 1, clip_on=False, transform=ax.transAxes)
r.set_bounds(bbt.bounds)
ax.add_patch(r)
fig.savefig('Old.png')
plt.show()
Metadata
Metadata
Assignees
Labels
No labels