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

Boxplot: Median line too long after changing linewidth #19409

Closed
ghost opened this issue Jan 31, 2021 · 9 comments · Fixed by #26462
Closed

Boxplot: Median line too long after changing linewidth #19409

ghost opened this issue Jan 31, 2021 · 9 comments · Fixed by #26462
Labels
Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones!
Milestone

Comments

@ghost
Copy link

ghost commented Jan 31, 2021

When changing the linewidth property of the median line in boxplots, the line will extend beyond the box width.

For example,

import matplotlib.pyplot as plt

data = [[1, 2, 3, 4, 5]]
fig, ax = plt.subplots()
ax.boxplot(data,
           medianprops={"linewidth": 8})

gives (on Matplotlib 3.2.2):

By simply chaning the solid_capstyle propery to "butt" in

ax.boxplot(data,
           medianprops={"linewidth": 8,
                        "solid_capstyle": "butt"})

I was able to improve this a bit (I stole that trick from here):

Ideally, one could now reduce the median line length such that it no longer overlaps with the black bounding line of the box.
Since the box line width is only accessible in "points" units, it would need to be transformed to data units.
I was unable to get this to work, though.

Would it be possible/desirable to change the default setting for solid_capstyle?
Could this be handled in a different way?

@anntzer
Copy link
Contributor

anntzer commented Jan 31, 2021

I agree with changing the capstyle. As for the overlap at the edges, likely it's a zorder issue? #18216/#17247 likely matter too.

@ghost
Copy link
Author

ghost commented Jan 31, 2021

Just another note: The mean (as opposed to median lines) do not seem to have the same issue:

data = [[1, 2, 3, 4, 7]]
fig, ax = plt.subplots()
ax.boxplot(data,
           showmeans=True,
           meanline=True,
           meanprops={"linewidth": 8})

gives me

where the length of the green mean line is fine. (The line is broken because that is the default mean line style.)

Edit: The mean line still overlaps the bounding box, but at least does not extend beyond it.

@ghost
Copy link
Author

ghost commented Jan 31, 2021

A symptomatic workaround for the overlapping issue would be to change

zdelta = 0.1

in this line to

zdelta = - 0.1

That resolves it for me.

As far as I can see, zdelta is only used for drawing mean and median lines, so changing that should be safe.

@tacaswell
Copy link
Member

There are different cap styles for solid lines and dashed lines.

This is also a case where the re-scaling of the dash pattern with linewidth is not an improvement...

@oscargus
Copy link
Contributor

oscargus commented Jun 4, 2022

Zooming in gives that the zorder for the mean line indeed should be changed as well (as it is clear that there are different cap styles).

image

@oscargus oscargus added Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! labels Jun 4, 2022
@oscargus
Copy link
Contributor

oscargus commented Jun 4, 2022

Should be a first good issue: change the default cap style and the zorder. Problems may involve testing.

@tacaswell tacaswell added this to the v3.7.0 milestone Jun 4, 2022
@wmandla
Copy link

wmandla commented Jun 23, 2022

Problems may involve testing.

I would like to make a small contribution in order to fix this issue. The solution has already been hinted above. This would be my first contribution and I am not quite clear on what is expected when testing the fix.

@kidkoder432
Copy link
Contributor

There are different cap styles for solid lines and dashed lines.

Where are these defined? Maybe changing the default cap style would help.

@kidkoder432
Copy link
Contributor

There are different cap styles for solid lines and dashed lines.

Where are these defined? Maybe changing the default cap style would help.

Nevermind, found it in lines.py:299

timhoffm pushed a commit that referenced this issue Aug 21, 2023
Closes #19409.

- Set the default capstyles (solid and dashed) for median and mean lines in boxplots to "butt".
  I also modieifed the behaviror for setting the capstyle for median lines to obey the user's preference
  instead of overriding it.
- The boxplot PDFs were using the wrong capstyle, so I modified the references to use the butt captyle.
- Fixed a small typo in `bxp()`
- Added a new test for the median line (from PR #23335)

Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this issue Aug 21, 2023
@QuLogic QuLogic modified the milestones: v3.7.3, v3.8.0 Aug 21, 2023
ksunden added a commit that referenced this issue Aug 21, 2023
…462-on-v3.8.x

Backport PR #26462 on branch v3.8.x (Boxplot fix median line extending past box boundaries #19409)
@ksunden ksunden mentioned this issue Sep 15, 2023
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones!
Projects
None yet
7 participants