Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Boxplot: Median line is now bound within the box for all linewidths #23335

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

Closed

Conversation

wmandla
Copy link

@wmandla wmandla commented Jun 23, 2022

PR Summary

Fixes #19409

When changing the linewidth property of the median line in boxplots, the line will extend beyond the box width.
I added a default capstyle preventing this from happening. I also changed the zdelta, so that the median and mean lines no longer overlap the box's border.

I would like to get further feedback, since this is my first contribution. I am also not sure if I need to document this bugfix somewhere.

PR Checklist

Tests and Styling

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

Documentation

  • New features are documented, with examples if plot related.
  • [n/a] New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • [n/a] API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).
  • [n/a] Documentation is sphinx and numpydoc compliant (the docs should build without error).

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a while, please feel free to ping @matplotlib/developers or anyone who has commented on the PR. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

medianprops['solid_capstyle'] = 'butt'

# put the mean and median line below the box for visual improvement
zdelta = -0.1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this lead to that the lines disappear if the box is colored?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. The lines disappear due to the change of the zdelta. I am not sure what the intended result should be. As you described (#19409 (comment)), the mean and median lines have a different cap style. How should the desired result look?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My comment there did clearly not take filled boxes into account...

I think that is really the main problem here: what should it look like? I've googled a bit and there seems to be no real consensus if the lines should extend in a certain way.

Anyway, I guess that there are two options here:

  1. simply change the caps as you currently do and do not change the zorder.
  2. use something like clip_on=True, clip_path=box to the line (where box is the actual box object). If that is the right one is hard to say though. This should at least work for the median line. (The mean line may disappear if it is not in the box though.) However, I think that this will give exactly the same result as it is clipped using the "theoretical zero width line" rather than the actual line.

So probably just reverting the zorder change is enough.

If you can provide an image of the results (e..g using any of the examples in #19409, possibly with exaggerated line widths), that would simplify the review. (Just paste it in a comment.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would actually strongly consider an image test here so we can codify what this should look like.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the detailed answer. I will go with option 1 then.
Also I will read further into the image tests and provide a test for it.

zdelta = 0.1
# Use 'butt' as the default capstyle to avoid a visual overlap of
# the median line and the boxplot
if medianprops.get('solid_capstyle') is None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if medianprops.get('solid_capstyle') is None:
if medianprops is None or medianprops.get('solid_capstyle', None) is None:

This is causing the test failure. Also, providing a default option for get avoid an error in case the element is not there.

@oscargus
Copy link
Member

Regarding the test, I think it makes sense.

@rcomer
Copy link
Member

rcomer commented Jan 29, 2023

Hi @wmandla, are you still interested in working on this? If not, there is another proposal at #24708, but I think you get priority since you were first.

kidkoder432 added a commit to kidkoder432/matplotlib that referenced this pull request Aug 7, 2023
Other changes:
- Update `.gitignore` to ignore `venv/` in commit
- Fixed a small typo in `bxp()`
- Added a new test for the median line (from PR matplotlib#23335)
@rcomer
Copy link
Member

rcomer commented Aug 19, 2023

Closing in favour of #26462

@rcomer rcomer closed this Aug 19, 2023
timhoffm pushed a commit that referenced this pull request 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 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

Boxplot: Median line too long after changing linewidth
5 participants