Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
Closed
@jklymak

Description

@jklymak
Issue body actions

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()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.