-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[ENH]: Legend entries for boxplot #27792
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
Comments
I'm unconvinced we would need to support passing multiple legend labels. If you are already looping through your artists to set the colours, you can set the labels at the same time. This works with v3.8.2: import matplotlib.pyplot as plt
import numpy as np
np.random.seed(19680801)
fruit_weights = [
np.random.normal(130, 10, size=100),
np.random.normal(125, 20, size=100),
np.random.normal(120, 30, size=100),
]
labels = ['peaches', 'oranges', 'tomatoes']
colors = ['peachpuff', 'orange', 'tomato']
fig, ax = plt.subplots()
ax.set_ylabel('fruit weight (g)')
bplot = ax.boxplot(fruit_weights,
patch_artist=True) # fill with color
# fill with colors and add legend labels
for patch, color, label in zip(bplot['boxes'], colors, labels):
patch.set_facecolor(color)
patch.set_label(label)
ax.legend()
ax.get_xaxis().set_visible(False)
plt.show() |
True. OTOH it doesn't cost us much. And it leaves the theoretical option open to add per-box coloring later (note that we've recently done this for What I actually wanted to say with this is that extensions in that directions are possible in a consistent way. |
How bad would it be to do a shuffle |
Note: There But
|
Yeah, sorry missed that |
Disregarding the existing API and how we would migrate, would
|
Could we introduce |
I lean towards |
Note to self/ to whoever is interested (I haven't made up my mind on this yet): Should |
I have a solution almost ready for this using variant 2 where the labels are set this way: labels = ['peaches', 'oranges', 'tomatoes']
ticklabels = ['a', 'b', 'c']
bplot = ax.boxplot(fruit_weights,
tick_labels=ticklabels,
label=labels) # legend labels
ax.legend() I can send in a PR as it is, unless you want to discuss this more (I don't want to create a bias for this variant.) |
This is a very good point. Let's go with that.
Go for it! Please note that |
Problem
Currently, boxplots do not get legend entries. #27711 was an attempt to introduce them but only solved one very particular usecase and did not generalize well. See #27780.
There is a
labels
parameter, but that only sets x-tick labels. And it has one entry per box. For the legend we should have only one entry perboxplot()
call no matter how many boxes that has, becauseboxplot
draws N identically styled boxes and it does not make sense to have N identical handles with different labels.Because of the x-tick relation of
labels
and it's relation to individual boxes, this parameter is not suited for the legend labels.Proposed solution
We need a separate parameter. There are basically two options:
Variant 1)
legend_label: str
pro: simple and clear, no interference with the existing API
con: inconsistent with the rest of the library: all other functions use
label
for the legend entry.Variant 2)
label: str
pro: consistent with the rest of the library
con: Having
label
alongsidelabels
is quite confusing and easily leads to errors. (We could runtime-check for the type str vs list of str, to give helpful error messages but still ...)If we want to go this way, we should rename
labels
totick_labels
or similar.The text was updated successfully, but these errors were encountered: