Skip to content

add itemboxalign option to legend constructor #29197

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

skkestrel
Copy link

PR summary

This pull request adds the option "itemboxalign" to the Legend constructor. From the proposed announcement .rst:

Legend previously always aligns items using the "baseline" option, which results in
the appearance of vertical centering of the artist and label for multi-line labels.
This is sometimes hard to read. The introduction of the itemboxalign parameter to the Legend constructor
allows the user to change this behavior and choose a different desired vertical alignment.

import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, 2)

ax[0].plot([5, 2, 8], label='long\nlabel')
ax[0].plot([4, 9, 1], label='another\nlong\nlabel')
ax[0].legend(title="align=baseline (default)")

ax[1].plot([5, 2, 8], label='long\nlabel')
ax[1].plot([4, 9, 1], label='another\nlong\nlabel')
ax[1].legend(title="align=top", itemboxalign='top')

plt.show()

image

I chose the name "itemboxalign" on a whim based on the existing rendering code in Legend. I think the name could be better, such as:

itemboxalignment
itemboxverticalalign
itemboxverticalalign(ment)
labelverticalalign(ment)

PR checklist

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@timhoffm
Copy link
Member

timhoffm commented Nov 28, 2024

Thanks for the contribution. This looks fundamentally reasonable. I'm still thinking about proper naming.

Note: We unfortunately have an alignment parameter that only covers the horizontal alignment of title and content and is thus too generic and semantically overlapping the alignment here. But for backward-compatibility we cannot change that.

The new naming needs to be more specific, but itemboxalign feels very technical and it may be hard to guess what it is doing. Maybe handlealignment? If one wanted to be very precise handle_vertical_alignment, but that's quite long. Suggestions welcome.

@skkestrel
Copy link
Author

I'll also note that in Legend documentation, the term "entry" is also used for the marker+label combination:

alignment{'center', 'left', 'right'}, default: 'center'
The alignment of the legend title and the box of entries. The entries are aligned as a single block, so that markers always lined up.

So, perhaps also entryalignment or entry_vertical_alignment - but I would argue that there's not much sense in talking about horizontal alignment of individual entries, so I slightly prefer the shorter entryalignment or handlealignment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Needs review
Development

Successfully merging this pull request may close these issues.

2 participants