-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Add legend support for boxplots #27840
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
Conversation
3cfd4ab
to
68edfcb
Compare
Other things that need to be done at some stage:
- Add an example. 3 possible options are:
- Adding a legend to https://matplotlib.org/devdocs/gallery/statistics/boxplot_color.html
- Replacing the basic legend in the second example in https://matplotlib.org/devdocs/gallery/statistics/boxplot_demo.html
- Add a whole new example plot
b03b849
to
9c033d2
Compare
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.
Thanks for your work on this @saranti. It looks like you've done some good work pulling a variety of strands together.
One thing I'm a bit confused about is how the legend label is passed from boxplot
to bxp
. It is passed both as part of the boxprops
dictionary and by itself. Why not just pass with the label parameter?
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.
When you use backticks, Sphinx tries to turn the word into a link. We have a convention to use emphasis for parameters.
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.
You're right, the boxprops
label is redundant. When i first implemented this, I used it as the only check to see if labels
has been set but then I realized that it should've been set at the bxp function. Everything in the 'if boxprops' condition is no longer needed.
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.
Double backtick gets you code formatting.
lib/matplotlib/axes/_axes.py
Outdated
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.
Since the parameter now has a more specific name, I think we no longer need the note about them not being for the legend.
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.
IIRC the data parameter is special and should always come last.
lib/matplotlib/axes/_axes.py
Outdated
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 think is_scalar_or_string may be useful here.
lib/matplotlib/tests/test_legend.py
Outdated
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.
Maybe use tick_labels here rather than the deprecated name?
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.
If I don't use the deprecated name then this pytest will fail.
lib/matplotlib/tests/test_legend.py
Outdated
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 think this might be a stray debugging line?
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.
Yes it is. I'm having trouble trying to get pytest to catch the UserWarning when legend
is called without the labels being set. The list of emitted warnings is always null even though the warning is clearly raised in
matplotlib/lib/matplotlib/legend.py
Lines 1364 to 1370 in 013757e
lib/matplotlib/tests/test_legend.py
Outdated
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.
It might be good for this test to get hold of the legend handles and labels and assert that these lists are empty.
lib/matplotlib/axes/_axes.py
Outdated
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 think this could be a single string or a sequence?
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 might actually need to be str or list of str
The tests failed because of a problem with a new version of pytest which has now been yanked. I’m going to close and re-open to restart the CI.
AFAICT, this needs a rebase to pick up the pytest pinning.
008c0a5
to
dd84a56
Compare
dd84a56
to
0b5d5d6
Compare
@melissawm Thanks for the help yesterday, the rebase seems to have gone fine. The only problem I see now is that all the code having to do with the violin plot shouldn't be there and belongs to #27815
@saranti your branch is still 60 commits behind main
. I think you just need
git fetch upstream
git rebase upstream/main
to get it up to date. That should also get rid of the violinplot changes.
0b5d5d6
to
e1bb638
Compare
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 feel uncomfortable with the scope of the PR - which has prevented me from looking more closely. It mixes the two independent topics legend support and parameter rename. I would I encourage separate PRs or at least separate commits to make it clear which feature is responsible for which code changes, and to ease review.
9fcf0c0
to
73f2933
Compare
73f2933
to
cae8a5d
Compare
9fcf0c0
to
c17906f
Compare
The old tick label test was still there after doing git pull upstream
. I removed it as part of the conflict resolution but it's showing up in the diff 🤔
I’ve never used git pull upstream
so not really sure what that does. To get up to date with changes that have been merged to main
, it is recommended to use rebase
.
https://matplotlib.org/devdocs/devel/development_workflow.html#rebase-onto-upstream-main
1902592
to
9fcf0c0
Compare
9fcf0c0
to
14ddd7e
Compare
14ddd7e
to
95890c2
Compare
5170c2d
to
7b39755
Compare
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.
To fix the doc build.
Milestoning as 3.9 because it would be nice to get this in and IMO it’s ready. Just needs a second review.
lib/matplotlib/axes/_axes.py
Outdated
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 check should probably be in the "input validation" section, and reuse datashape_message
.
6004931
to
c96334d
Compare
Uh oh!
There was an error while loading. Please reload this page.
PR summary
Closes #27792. Boxplots now get legend entries.
This was previously attempted on #27711 and then reverted #27780 because it used the same parameter for tick labels and didn't generalize well. These 2 issues have been addressed in this PR.
A new
label
parameter has been added toAxes.boxplot
andAxes.bxp
. If called viaax.boxplot()
, label is passed to the bxp function, otherwise bxp can be directly called with the label parameter.The label is passed to the
boxes
object ifshowbox=True
, otherwise it will be passed to themedians
object isshowbox=False
. Attaching the label to the boxes makes sure that the legend handles will get a patch which will be displayed on the legend.If the box is turned off, the legend handles will get a Line2D object from the median line which will be displayed as a line in the legend. This has 2 advantages:
box
,caps
,flyers
andmeans
, themedians
cannot be turned off so they're a good vehicle for the legend label and will ensure that the labels always get set.Click to old test cases
new_test
PR checklist