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 fix median line extending past box boundaries #19409 #26462

Merged
merged 11 commits into from
Aug 21, 2023

Conversation

kidkoder432
Copy link
Contributor

@kidkoder432 kidkoder432 commented Aug 7, 2023

PR summary

Fixes #19409
The median line on a boxplot would extend past the box when the line width was increased. This PR aims to fix the visual annoyance by setting the solid line capstyle to be the same as all other capstyles. additionally, this change keeps the capstyle of the median lines consistent with other boxplot lines (i.e. mean lines).

This is my second ever PR so be nice please :)

Other changes:

PR checklist

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)
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 week or so, please leave a new comment below and that should bring it to our attention. 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.

@melissawm
Copy link
Contributor

Hi @kidkoder432 - while we wait for a proper review, would you mind taking a look at the Linting failure here? https://github.com/matplotlib/matplotlib/actions/runs/5780696475/job/15664707497?pr=26462

It looks like you are missing a blank line in your test.

Copy link
Member

@rcomer rcomer 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 your work on this @kidkoder432, but I do not think this is currently the right change.

lib/matplotlib/axes/_axes.py Show resolved Hide resolved
lib/matplotlib/lines.py Outdated Show resolved Hide resolved
@rcomer rcomer added status: needs revision PR: bugfix Pull requests that fix identified bugs labels Aug 8, 2023
@kidkoder432
Copy link
Contributor Author

kidkoder432 commented Aug 8, 2023 via email

@rcomer
Copy link
Member

rcomer commented Aug 11, 2023

This change has caused some test failures. @kidkoder432 are you confident to investigate those yourself? You can see the full output from the tests by clicking the "Details" link by one of the checks at the bottom of this page. You can also run the tests yourself locally:
https://matplotlib.org/devdocs/devel/testing.html

@kidkoder432
Copy link
Contributor Author

This change has caused some test failures. @kidkoder432 are you confident to investigate those yourself? You can see the full output from the tests by clicking the "Details" link by one of the checks at the bottom of this page. You can also run the tests yourself locally: https://matplotlib.org/devdocs/devel/testing.html

Sure, I'll take a look and let you know any concerns.

@kidkoder432
Copy link
Contributor Author

kidkoder432 commented Aug 12, 2023

Edit: I fixed the NoneType errors that were occurring when a medianprops dictionary was not provided, but those same tests now fail with ImageComparisonFailure:

Okay, so I looked at the failing tests and noticed that the expected images were all using a capstyle of projecting. However, because I modified the code to use a capstyle of butt. the original tests for median lines were failing because they were expecting a projecting capstyle. However, this capstyle is the reason why the median line was extending past the box. According to the docs:

'projecting'
the line is squared off as in butt, but the filled-in area extends beyond the endpoint a distance of linewidth/2.

I'm not sure how to move forward with this.

Sometimes the `medianprops` dict was not provided so the previous code was failing to assign a key to it. So a check was added to either modify an exsisting dict or create a new one.
@rcomer
Copy link
Member

rcomer commented Aug 12, 2023

OK, if the user specifies a capstyle then we should assume they are happy with their choice and leave it. We should only set it to “butt” if the user didn’t make a choice. I think the dictionary setdefault method might be useful here.

@kidkoder432
Copy link
Contributor Author

OK, if the user specifies a capstyle then we should assume they are happy with their choice and leave it. We should only set it to “butt” if the user didn’t make a choice. I think the dictionary setdefault method might be useful here.

Good idea.
Those tests don't seem to define a capstyle specifically though, so they still fail...

@rcomer
Copy link
Member

rcomer commented Aug 14, 2023

Ah OK, I thought you meant that some tests were specifying “projecting” as the capstyle. Instead they are just leaving it to default and this change switches the default from “projecting” to “butt”. So the tests are failing because of a genuine (desired) change in the output, which means you will need to replace the reference images. We have the triage_tests tool to help with that.

I used the triage tests tool to set the test benchmark images to those with a capstyle of `butt`. I also modieifed the behaviror for setting the capstyle for median lines to obey the user's preference instead of overriding it.
@rcomer
Copy link
Member

rcomer commented Aug 16, 2023

Thanks for the update @kidkoder432. It looks like you still have a couple of image test failures for pdf files. Do you see those failures locally? If not you might be missing a dependency - most likely ghostscript.
https://matplotlib.org/devdocs/devel/dependencies.html#dependencies-for-testing-matplotlib

@kidkoder432
Copy link
Contributor Author

kidkoder432 commented Aug 16, 2023 via email

@rcomer
Copy link
Member

rcomer commented Aug 16, 2023

I think the triage_tests script should handle pdf as well as png.

@kidkoder432
Copy link
Contributor Author

kidkoder432 commented Aug 16, 2023 via email

@kidkoder432
Copy link
Contributor Author

I'm having trouble getting pytest to recognize that I have Ghostscript installed. are there any resources for this on the matplotlib docs?

i used the installer to install Ghostscript 10.01.2.

@ksunden
Copy link
Member

ksunden commented Aug 18, 2023

Matplotlib only interacts with ghostscript via subprocess calls. Can you call gs as a CLI program (e.g. gs --help) (on windows it may also be called gswin64c, not really sure what is fully going on there...)

If it is not in your PATH, it will not be found, so make sure it is installed in a place that it can be executed.

@kidkoder432
Copy link
Contributor Author

kidkoder432 commented Aug 19, 2023 via email

@kidkoder432
Copy link
Contributor Author

kidkoder432 commented Aug 19, 2023 via email

The boxplot PDFs were using the wrong capstyle, so I modified the references to use the butt captyle.
@kidkoder432
Copy link
Contributor Author

Hi, for some reason it says that CodeCov tests failed in the workflow logs. However, when I visit the website, its says that codecov tests all passed. Can someone please take a look at this?

@rcomer
Copy link
Member

rcomer commented Aug 19, 2023

I wouldn’t worry about that codecov failure. We currently use codecov for information only. In this case, codecov says that 100% of your change is covered, which I think is the important thing. The coverage reductions are all “indirect changes” - I haven’t really got my head around how those are caused myself.

.gitignore Outdated Show resolved Hide resolved
kidkoder432 and others added 2 commits August 19, 2023 08:38
Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
@kidkoder432
Copy link
Contributor Author

kidkoder432 commented Aug 19, 2023 via email

Copy link
Member

@rcomer rcomer left a comment

Choose a reason for hiding this comment

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

This looks good to me now. For code changes, we require approvals from two reviewers so this can’t be merged just yet. Thank you for your work on it @kidkoder432!

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

Question: Does anybody understand why the box borders are sometimes shifted slightly?

Comment: The original issue also discussed bringing the line below the box using zorder. This is not discussed in this PR, While conceptually desireable, this is not so easy to fix: The box can be filled and then face and edge are drawn with the same zorder. This means, one cannot draw mean/median lines below the box edge but above the box face (at least not without complicating the draw logic).
I'm therefore inclinded to accept the PR without addressing zorder, but would like to have at least one second thought on this.

@@ -4147,6 +4147,11 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True,
--------
.. plot:: gallery/statistics/bxp.py
"""
# Clamp median line to edge of box by default.
medianprops = {
"solid_capstyle": "butt",
Copy link
Member

Choose a reason for hiding this comment

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

I think we should unversally "solid_capstyle" and "dashed_capstyle" to "butt" for both medianprops and meanprops. We want the lines to end within the box for both independent of the global capstyle settings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@kidkoder432
Copy link
Contributor Author

Question: Does anybody understand why the box borders are sometimes shifted slightly?

Comment: The original issue also discussed bringing the line below the box using zorder. This is not discussed in this PR, While conceptually desireable, this is not so easy to fix: The box can be filled and then face and edge are drawn with the same zorder. This means, one cannot draw mean/median lines below the box edge but above the box face (at least not without complicating the draw logic).
I'm therefore inclinded to accept the PR without addressing zorder, but would like to have at least one second thought on this.

Not sure why either... From what you explained about the draw logic it would likely be much easier to stay with the current implementation.

@rcomer
Copy link
Member

rcomer commented Aug 21, 2023

Question: Does anybody understand why the box borders are sometimes shifted slightly?

Do you have an example? When I looked at the test images that have changed here, all the diffs seemed to just show the median line getting shorter.

I'm therefore inclinded to accept the PR without addressing zorder, but would like to have at least one second thought on this.

I came to same conclusion based on @oscargus' comment on the previous PR: #23335 (comment)

@timhoffm
Copy link
Member

Do you have an example? When I looked at the test images that have changed here, all the diffs seemed to just show the median line getting shorter.

Can't find anymore. Either I was wrong or this was somehow fixed through the recent updates.

@timhoffm timhoffm added this to the v3.8.0 milestone Aug 21, 2023
@timhoffm
Copy link
Member

Since this is a bugfix, I recon that it can still go into 3.8.0 (on the basis that we do not have release candidates for any bugfix releases, there's no difference between deferring to 3.8.1 and pushing this into 3.8.0 even after 3.8.0rc1).

@timhoffm timhoffm merged commit 813e41a into matplotlib:main Aug 21, 2023
38 of 39 checks passed
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Aug 21, 2023
@timhoffm
Copy link
Member

Thanks @kidkoder432, and congratulations on your first contrbution to matplotlib! We'd happy to see you back!

ksunden added a commit that referenced this pull request 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)
@kidkoder432
Copy link
Contributor Author

kidkoder432 commented Aug 22, 2023 via email

@ksunden ksunden mentioned this pull request Sep 15, 2023
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: bugfix Pull requests that fix identified bugs
Projects
Development

Successfully merging this pull request may close these issues.

Boxplot: Median line too long after changing linewidth
6 participants