Skip to content

Boxplot: Median line too long after changing linewidth #19409

Closed

Description

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?

Activity

  1. anntzer commented on Jan 31, 2021

    @anntzer
    Contributor

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

  2. tacaswell commented on Jan 31, 2021

    @tacaswell
    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...

  3. oscargus commented on Jun 4, 2022

    @oscargus
    Member

    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

  4. added
    Difficulty: Easyhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    on Jun 4, 2022
  5. oscargus commented on Jun 4, 2022

    @oscargus
    Member

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

  6. added this to the v3.7.0 milestone on Jun 4, 2022
  7. wmandla commented on Jun 23, 2022

    @wmandla

    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.

  8. modified the milestones: v3.7.0, v3.7.1 on Feb 14, 2023
  9. modified the milestones: v3.7.1, v3.7.2 on Mar 4, 2023
  10. modified the milestones: v3.7.2, v3.7.3 on Jul 5, 2023
  11. kidkoder432 commented on Aug 7, 2023

    @kidkoder432
    Contributor

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

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

  12. kidkoder432 commented on Aug 7, 2023

    @kidkoder432
    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

  13. added a commit that references this issue on Aug 21, 2023
    813e41a
  14. added a commit that references this issue on Aug 21, 2023
  15. modified the milestones: v3.7.3, v3.8.0 on Aug 21, 2023
  16. added a commit that references this issue on Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Easyhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues🌱 Good first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions