Skip to content
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

Bad boxplot legend entries #20512

Closed
RSKothari opened this issue Jun 25, 2021 · 5 comments · Fixed by #27711
Closed

Bad boxplot legend entries #20512

RSKothari opened this issue Jun 25, 2021 · 5 comments · Fixed by #27711

Comments

@RSKothari
Copy link

Describe the issue

Legend for entries in boxplot are displayed as thin lines as opposed to patches

When we have multiple box plots in the same axis, legend shows thin lines as opposed to boxes and it is difficult to tell entries apart. This issue has been documented at this link with a reproducible example.

Proposed fix

I don't have a solution :(

Link: https://stackoverflow.com/questions/68124486/improve-matplotlib-legend-for-boxplots-by-showing-patches-instead-of-lines

@story645
Copy link
Member

Do you think the legend should use boxes as illustrated in the answer to that question? fGWNP.png

@RSKothari
Copy link
Author

@story645 Yes, I think that should be the default behavior of legend when used on boxplot.

@anntzer
Copy link
Contributor

anntzer commented Jun 25, 2021

I think this is mostly a matter of setting label="_nolegend_" on all artists generated by boxplot except the box itself (the black lines in the SO example come from the fact that we currently try to generate legend entries for the whiskers, caps, and outliers).
Even better would be to have a custom handler for boxplots, but that seems to be much more work.

@saranti
Copy link
Contributor

saranti commented Jun 14, 2023

Ever since v3.5, the first entry in the legend is shown as a rectangular patch with the rest shown as lines:

box

It's the same regardless of the number of entries.

@story645
Copy link
Member

story645 commented Jun 14, 2023

code to reproduce:

fig, axs = plt.subplots()

A = 5*np.random.rand(100, 1)
B = 10*np.random.rand(100, 1) -5
C = 7*np.random.rand(100, 1) - 5
bp0 = axs.boxplot(A, positions=[0], patch_artist=True)
bp1 = axs.boxplot(B, positions=[1], patch_artist=True)
bp2 = axs.boxplot(C, positions=[2], patch_artist=True)
axs.legend(['A', 'B', 'C'])

ET2: I haven't done a bisect, but #20515 went in around 3.5 and refactored the boxplot code.

@QuLogic QuLogic added this to the v3.9.0 milestone Feb 1, 2024
timhoffm added a commit to timhoffm/matplotlib that referenced this issue Feb 12, 2024
This PR removes the propagation of `labels` to any artist legend labels.

Other than the rest of the plotting functions `labels` is not used for legend labels
but for xtick labels. This is only poorly documented via
https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.bxp.html and in an
[example](https://matplotlib.org/stable/gallery/statistics/boxplot_color.html).

Whatever our way forward regarding the use of `labels` is, we should by no means
propagate them simultaneously to xticks and legend entries. This coupling would cripple
users' configurability and limit our ability to migrate to a clear API where legend
labels and tick labels can be configured independently.

Until we have sorted out a better API, the recommended solution for the original issue
matplotlib#20512 is to grab the artists returned from `boxplot()` and either `set_label()` on
them or pass them to the legend call `ax.legend(handles, labels)`.
timhoffm added a commit to timhoffm/matplotlib that referenced this issue Feb 12, 2024
This PR removes the propagation of `labels` to any artist legend labels.

Other than the rest of the plotting functions `labels` is not used for legend labels
but for xtick labels. This is only poorly documented via
https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.bxp.html and in an
[example](https://matplotlib.org/stable/gallery/statistics/boxplot_color.html).

Whatever our way forward regarding the use of `labels` is, we should by no means
propagate them simultaneously to xticks and legend entries. This coupling would cripple
users' configurability and limit our ability to migrate to a clear API where legend
labels and tick labels can be configured independently.

Until we have sorted out a better API, the recommended solution for the original issue
matplotlib#20512 is to grab the artists returned from `boxplot()` and either `set_label()` on
them or pass them to the legend call `ax.legend(handles, labels)`.
timhoffm added a commit to timhoffm/matplotlib that referenced this issue Feb 14, 2024
This PR removes the propagation of `labels` to any artist legend labels.

Other than the rest of the plotting functions `labels` is not used for legend labels
but for xtick labels. This is only poorly documented via
https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.bxp.html and in an
[example](https://matplotlib.org/stable/gallery/statistics/boxplot_color.html).

Whatever our way forward regarding the use of `labels` is, we should by no means
propagate them simultaneously to xticks and legend entries. This coupling would cripple
users' configurability and limit our ability to migrate to a clear API where legend
labels and tick labels can be configured independently.

Until we have sorted out a better API, the recommended solution for the original issue
matplotlib#20512 is to grab the artists returned from `boxplot()` and either `set_label()` on
them or pass them to the legend call `ax.legend(handles, labels)`.
Impaler343 pushed a commit to Impaler343/matplotlib that referenced this issue Mar 8, 2024
This PR removes the propagation of `labels` to any artist legend labels.

Other than the rest of the plotting functions `labels` is not used for legend labels
but for xtick labels. This is only poorly documented via
https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.bxp.html and in an
[example](https://matplotlib.org/stable/gallery/statistics/boxplot_color.html).

Whatever our way forward regarding the use of `labels` is, we should by no means
propagate them simultaneously to xticks and legend entries. This coupling would cripple
users' configurability and limit our ability to migrate to a clear API where legend
labels and tick labels can be configured independently.

Until we have sorted out a better API, the recommended solution for the original issue
matplotlib#20512 is to grab the artists returned from `boxplot()` and either `set_label()` on
them or pass them to the legend call `ax.legend(handles, labels)`.
Impaler343 pushed a commit to Impaler343/matplotlib that referenced this issue Mar 14, 2024
This PR removes the propagation of `labels` to any artist legend labels.

Other than the rest of the plotting functions `labels` is not used for legend labels
but for xtick labels. This is only poorly documented via
https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.bxp.html and in an
[example](https://matplotlib.org/stable/gallery/statistics/boxplot_color.html).

Whatever our way forward regarding the use of `labels` is, we should by no means
propagate them simultaneously to xticks and legend entries. This coupling would cripple
users' configurability and limit our ability to migrate to a clear API where legend
labels and tick labels can be configured independently.

Until we have sorted out a better API, the recommended solution for the original issue
matplotlib#20512 is to grab the artists returned from `boxplot()` and either `set_label()` on
them or pass them to the legend call `ax.legend(handles, labels)`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants