Skip to content

Test layout with big descenders and multiple lines inconsistent. #11498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jklymak opened this issue Jun 24, 2018 · 1 comment · Fixed by #11499
Closed

Test layout with big descenders and multiple lines inconsistent. #11498

jklymak opened this issue Jun 24, 2018 · 1 comment · Fixed by #11499
Milestone

Comments

@jklymak
Copy link
Member

jklymak commented Jun 24, 2018

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.

  1. The layout is fine for single lines even large descenders. (first three text objects on each line)
  2. For two lines with the same descend the layout is fine (last text object on each line)
  3. 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.

old

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()
@jklymak
Copy link
Member Author

jklymak commented Jun 24, 2018

Ooops, of course writing the bug report helped me realize what most of the issue is... #11499

@QuLogic QuLogic added this to the v3.1 milestone Sep 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants