-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix boxplot legend entries #27568
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
Fix boxplot legend entries #27568
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree w/ @anntzer about the full solution being to create a BoxplotLegendHandler, but would be great if this is good enough to fix things. Can you please add a test that it does?
lib/matplotlib/axes/_axes.py
Outdated
@@ -4319,14 +4319,14 @@ def do_patch(xs, ys, **kwargs): | |||
do_box = do_patch if patch_artist else do_plot | |||
boxes.append(do_box(box_x, box_y, **box_kw)) | |||
# draw the whiskers | |||
whiskers.append(do_plot(whis_x, whislo_y, **whisker_kw)) | |||
whiskers.append(do_plot(whis_x, whishi_y, **whisker_kw)) | |||
whiskers.append(do_plot(whis_x, whislo_y, **whisker_kw, label="_nolegend_")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be done as
whisker_kw.setdefault('label', '_nolegend_)
or similar where we generate/accept these dictionaries. As proposed this will fail with a TypeError for any user who is currently passing label though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might also want to confirm that the patch properties are as expected, eg. color.
What happens if you set a label on those elements?
Setting a label to an element like whiskers or caps will revert the next 2 handles into lines. In this case, |
We have a The fundamental problem is that calling The brittleness comes from the logic of "I have been given N strings, let me find the first N artists in the axes that I know how to make legend entries for and use those!". By setting The other ways of making legends (either passing both artists and strings ("use these labels for those artists"), passing a list of artists ("ask these artists what their label should be and then put in legend"2 or calling with no arguements ("find anything that has a suitable label and put it in the legend") are all significantly better APIs (easier to understand and not brittle). If the user set a label on the whiskers, it is presumably because they are doing |
I've implemented this idea so now we can call |
Talked about this on the call. We like the implementation, but it needs a rebase. |
@saranti Did you mean to close this? |
PR summary
Work on this is continued in #27711
Closes #20512. Legend entries in boxplots are now shown as patches instead of thin lines. As explained in #20512 (comment), legend entries were being generated for the box and also for the whiskers, caps, and outliers. This patch prevents entries being created for every artist except the box itself by applying the
label="_nolegend_"
argument for those artists.PR checklist