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

Add support for multiple hatches, edgecolors and linewidths in histograms #28073

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

Impaler343
Copy link
Contributor

@Impaler343 Impaler343 commented Apr 13, 2024

PR summary

Closes #26718 Distributes keyword args passed to each Patch using a cycler. Probably not the best way to do this?

PR checklist

@jklymak
Copy link
Member

jklymak commented Apr 13, 2024

I'd suggest showing what this does with an example either in the GitHub pr description, or ideally in the gallery

@github-actions github-actions bot added the Documentation: examples files in galleries/examples label Apr 14, 2024
@Impaler343
Copy link
Contributor Author

I'm not really sure if I need to add a new test or just modify an exisiting one(test_hist_stacked_bar) in test_axes.py

@oscargus
Copy link
Contributor

I guess there are two things here:

  1. Add/modify an example in the gallery to illustrate how it is used. (Will be easier for the reviewers to get an idea of how it is used etc.)
  2. Add a test to get the code coverage up and make sure no one breaks it later. Maybe there is some old example that one can "hi-jack", but otherwise create a new test with all the bells and whistles turned on.

@Impaler343
Copy link
Contributor Author

pinging @story645 for review. The failing tests are unrelated

Copy link
Member

@story645 story645 left a comment

Choose a reason for hiding this comment

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

also needs updated documentation that the patch properties are now vectorized (@timhoffm any concerns here?)

lib/matplotlib/axes/_axes.py Outdated Show resolved Hide resolved
lib/matplotlib/axes/_axes.py Outdated Show resolved Hide resolved
galleries/examples/statistics/histogram_multihist.py Outdated Show resolved Hide resolved
@timhoffm
Copy link
Member

timhoffm commented May 19, 2024

also needs updated documentation that the patch properties are now vectorized (@timhoffm any concerns here?)

What should we be concerned about? We already have vectorized label and color. It's only fair to vectorize these inside hist() as well. This should be documented with **kwargs (can't immediately suggest, because that part has not been touched in the PR). Something like:

        **kwargs
            `~matplotlib.patches.Patch` properties. The following properties additionally
            accept lists of property values, one element for each dataset:
            *edgecolors*, *linewidths*, *linestyles*, *hatches*.

This should also get a what's new entry.

@story645
Copy link
Member

What should we be concerned about? We already have vectorized label and color.

My bias is vectorize everything so I don't have concerns, but in the past for some vectorization discussions there have been concerns about the tradeoffs. But if there isn't opposition, awesome!

@timhoffm
Copy link
Member

I don't see any drawbacks for hist()

@Impaler343
Copy link
Contributor Author

So we are planning to vectorize all parameters of Patches? Like joinstyle, capstyle etc.

@story645
Copy link
Member

So we are planning to vectorize all parameters of Patches? Like joinstyle, capstyle etc

Not at this time w/ the current architecture, especially because nobody has asked for those.

Specify extensions for test

Added modified baseline images

Modified test for histogram with single parameters

Fixed test

Add modified baseline images
@Impaler343
Copy link
Contributor Author

Codecov is acting fishy, it passed once and failed again after squashing. Anything else to add/change?

lib/matplotlib/axes/_axes.py Outdated Show resolved Hide resolved
lib/matplotlib/tests/test_axes.py Outdated Show resolved Hide resolved
lib/matplotlib/axes/_axes.py Outdated Show resolved Hide resolved
galleries/examples/statistics/histogram_multihist.py Outdated Show resolved Hide resolved
galleries/examples/statistics/histogram_multihist.py Outdated Show resolved Hide resolved
galleries/examples/statistics/histogram_multihist.py Outdated Show resolved Hide resolved
galleries/examples/statistics/histogram_multihist.py Outdated Show resolved Hide resolved
@Impaler343
Copy link
Contributor Author

Impaler343 commented Jun 5, 2024

As of the last commit, this behavior is consistent for all histtypes unless mentioned otherwise:

  • If edgecolor, facecolor and color are mentioned, color is completely ignored.
  • If edgecolor and color are mentioned, color is applied to only the faces and edgecolor to the edges.
  • If facecolor and color are mentioned, color is applied to only the edges and facecolor to the faces.
  • If facecolor and edgecolor are mentioned, edgecolor is applied to edges, and facecolor is applied to faces.
  • If only color is mentioned, the face is colored for all filled histograms, and only the edge is colored for unfilled histograms with the values in color.
  • If only facecolor is mentioned, the face is colored for all filled histograms with the value in facecolor, and the edges are colored with the default color cycle for unfilled histograms.
  • If only edgecolor is mentioned, the face is colored by the default color cycle for all filled histograms, and the edge is colored with the values in edgecolor for all histograms.
    I feel this is the required behavior, but I am unsure if the color setting for the kwargs can be written more concisely.
    Let me know if I should start writing tests based on this structure.

@story645
Copy link
Member

story645 commented Jun 6, 2024

@Impaler343 I started trying to flow chart that and then realized it's probably clearer as a table. Let me know if this jives with what you're saying:

hist type color facecolor edgecolor patch face color patch edge color
all set set set facecolor edgecolor
all set None set color edgecolor
all set set None facecolor color
all None set set facecolor edgecolor
bar, barstacked, stepfilled set None None color 'none'
step set None None 'none' color
bar, barstacked, stepfilled None set None color 'none'
step None set None 'none' default color cycle
bar, barstacked, stepfilled None None set default color cycle 'none'
step None None set 'none' edgecolor
bar, barstacked, stepfilled None None None
step None None None

@Impaler343
Copy link
Contributor Author

hist type color facecolor edgecolor patch face color patch edge color
all set set set facecolor edgecolor
all set None set color edgecolor
all set set None facecolor color
all None set set facecolor edgecolor
bar, barstacked, stepfilled set None None color 'none'
step set None None 'none' color
bar, barstacked, stepfilled None set None facecolor 'none'
step None set None 'none' default color cycle
bar, barstacked, stepfilled None None set default color cycle edgecolor
step None None set 'none' edgecolor
bar, barstacked, stepfilled None None None default color cycle 'none'
step None None None 'none' default color cycle

Have bolded the corrected ones

@Impaler343
Copy link
Contributor Author

I'm unable to fix CircleCI errors for docs. Could someone help me out?

@story645
Copy link
Member

story645 commented Jun 7, 2024

Hi, so the error is in your what's new:

/home/circleci/project/doc/users/next_whats_new/histogram_vectorized_parameters.rst:47: WARNING: Explicit markup ends without a blank line; unexpected unindent.
/home/circleci/project/doc/users/next_whats_new/histogram_vectorized_parameters.rst:51: ERROR: Unexpected indentation.
/home/circleci/project/doc/users/next_whats_new/histogram_vectorized_parameters.rst:64: WARNING: Explicit markup ends without a blank line; unexpected unindent.
/home/circleci/project/doc/users/next_whats_new/histogram_vectorized_parameters.rst:68: ERROR: Unexpected indentation.

error: https://app.circleci.com/pipelines/github/matplotlib/matplotlib/31532/workflows/2533c013-7833-4d0c-ae72-dead7c7fbc76/jobs/83481?invite=true#step-113-207133_109
rendering: https://output.circle-artifacts.com/output/job/2c962d26-1c66-49dd-a987-e5bf25c8fcbe/artifacts/0/doc/build/html/users/next_whats_new/histogram_vectorized_parameters.html

lib/matplotlib/axes/_axes.py Outdated Show resolved Hide resolved
lib/matplotlib/tests/test_axes.py Outdated Show resolved Hide resolved
lib/matplotlib/axes/_axes.py Show resolved Hide resolved
@Impaler343
Copy link
Contributor Author

Impaler343 commented Jun 8, 2024

Ok it turns out the vectorization of colors is somewhat incomplete:

  • When we pass None, calls the color cycle, which is fine.
  • When we pass a list of valid colors, it uses them, which is also fine.
  • When we pass a list of Nones or even if one value in the list is None, it throws an error saying invalid RBGA argument. What can we do in this situation? Should we say that we use the colour cycle even if one value in the list is None and raise a warning? Or call the color cycle's next color whenever a None is encountered in a list? Or simply assume that the user will not pass a list like color= [None, "green", None] and ignore this check?

@story645
Copy link
Member

story645 commented Jun 9, 2024

or simply assume that the user will not pass a list like color= [None, "green", None]

I think this input is ambiguous cause it can mean either:

  1. opt those two lines into the color cycle such that it maps to ['C0', "green", 'C1']
  2. set both lines to the same default ['C0', "green", 'C0']

So I think it's ok to error out (unless/until someone comes around expecting 1 or 2) but we should maybe special case and have a better error message, like if not all(colors) (if any are NaN) "Ambiguous color specification: colors in the list may not be None"

Or something like that.

Added baseline images

Modified test to pass codecov, added plot in whats new entry

Fix test

Added baseline images
Removed figsize from test

Add multiple baseline image names

Fixed test?

Fixed test?

Removed parametrize usage

Add baseline images

Add baseline image

Fix docs

Fix docs

Deleted baseline images, changed test

Fix test

Fix test
Handled passing None list
@Impaler343
Copy link
Contributor Author

The added lines have all been covered, codecov seems to still fail

lib/matplotlib/axes/_axes.py Outdated Show resolved Hide resolved
lib/matplotlib/tests/test_axes.py Show resolved Hide resolved
@@ -4603,6 +4603,36 @@ def test_hist_stacked_bar():
ax.legend(loc='upper right', bbox_to_anchor=(1.0, 1.0), ncols=1)


@pytest.mark.parametrize("histtype", ["step", "stepfilled"])
@pytest.mark.parametrize("color", [["blue", "green", "brown"], [None]*3])
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
@pytest.mark.parametrize("color", [["blue", "green", "brown"], [None]*3])
@pytest.mark.parametrize("color", [["blue", "green", "brown"], None)

I'd check the [None]*3 case as part of the error message test for the "["blue", None, etc]" case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I figured we dont need an error message with the current modification

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The current modification handles [None, "blue", None] by assigned the rcparam color to None and uses the specified color wherever specified. Should be fine?

Copy link
Member

Choose a reason for hiding this comment

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

Should be fine?

I'd rather it error out b/c of the ambiguity -> do you want the default param or do you want the next entry in the color cycle?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Default param seems more logical to me, but no harm in adding the warning I guess. But i strongly feel people wont be expecting the color cycle after passing None explicitly. Isn't the color cycle called to fill up the absence of any colors explicitly mentioned?

Copy link
Member

@story645 story645 Jun 11, 2024

Choose a reason for hiding this comment

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

Isn't the color cycle called to fill up the absence of any colors explicitly mentioned?

Yeah, so here the question is with [None, 'blue', None] does the user intend that these are 3 distinct datasets and therefore that the color cycle should kick in, or that they're just using [None, 'blue', None] cause those two can be the same default color.

Is possibly a call for @timhoffm or something that should go on the project call (I won't be there this week, but you're welcome to come) thursday https://scientific-python.org/calendars/

lib/matplotlib/axes/_axes.py Outdated Show resolved Hide resolved
Copy link
Member

@story645 story645 left a comment

Choose a reason for hiding this comment

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

I think this is good +- some grammer nits, but I'm gonna put on the needs discussion label b/c I think the [none, paramater, none] case needs discussion.

rebase will fix the pr-cleanliness: https://matplotlib.org/devdocs/devel/development_workflow.html#rewrite-commit-history

Coverage is complaining that the no patch case is never tested:
https://app.codecov.io/gh/matplotlib/matplotlib/pull/28073?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=checks&utm_campaign=pr+comments&utm_term=matplotlib#7e79bc5b4cdd21de353697e9ada248b7-R7229

ETA: this can be a separate test!


.. plot::
:include-source: true
:alt: Four charts, each displaying stacked histograms of three Poisson distributions. Each chart differentiates the histograms using various parameters: ax1 uses different linewidths, ax2 uses different hatches, ax3 uses different edgecolors, and ax4 uses different facecolors. Edgecolors have ax1 and ax3 as well to accentuate the differences between the histograms.
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
:alt: Four charts, each displaying stacked histograms of three Poisson distributions. Each chart differentiates the histograms using various parameters: ax1 uses different linewidths, ax2 uses different hatches, ax3 uses different edgecolors, and ax4 uses different facecolors. Edgecolors have ax1 and ax3 as well to accentuate the differences between the histograms.
:alt: Four charts, each displaying stacked histograms of three Poisson distributions. Each chart differentiates the histograms using various parameters: ax1 uses different linewidths, ax2 uses different hatches, ax3 uses different edgecolors, and ax4 uses different facecolors. Each histogram in ax1 and ax3 also has a different edgecolor.

grammar/construction

# * linestyles
#
#
# Histograms with Edge-Colors
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
# Histograms with Edge-Colors
# Edge colors

I think you can just differentiate on property b/c it's the histograms page

@@ -4603,6 +4603,36 @@ def test_hist_stacked_bar():
ax.legend(loc='upper right', bbox_to_anchor=(1.0, 1.0), ncols=1)


@pytest.mark.parametrize("histtype", ["step", "stepfilled"])
@pytest.mark.parametrize("color", [["blue", "green", "brown"], [None]*3])
Copy link
Member

@story645 story645 Jun 11, 2024

Choose a reason for hiding this comment

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

Isn't the color cycle called to fill up the absence of any colors explicitly mentioned?

Yeah, so here the question is with [None, 'blue', None] does the user intend that these are 3 distinct datasets and therefore that the color cycle should kick in, or that they're just using [None, 'blue', None] cause those two can be the same default color.

Is possibly a call for @timhoffm or something that should go on the project call (I won't be there this week, but you're welcome to come) thursday https://scientific-python.org/calendars/

@story645 story645 added the status: needs comment/discussion needs consensus on next step label Jun 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation: examples files in galleries/examples New feature status: needs comment/discussion needs consensus on next step
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: stacked histogram does not properly handle edgecolor and hatches
6 participants