Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add optional bihistogram parameter to plt.hist() #27180

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
wants to merge 6 commits into from
Closed

Add optional bihistogram parameter to plt.hist() #27180

wants to merge 6 commits into from

Conversation

i-jey
Copy link
Contributor

@i-jey i-jey commented Oct 24, 2023

PR summary

This PR adds an optional boolean parameter to plt.hist(), bihist to enable plotting bihistograms.

Bihistograms are one way to visualize an effect on a distribution in a pre-post analysis, making it easier to see changes in the distribution's mean and skew.

plt.hist([data1, data2], bihist=True, bins=50, label=['Original', 'Thresholded'])
plt.legend()
image

PR checklist

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.

@ksunden
Copy link
Member

ksunden commented Oct 24, 2023

I think this is a better candidate for an example than an API expansion, setting the weights parameter to -np.ones_like(data2) gets the desired affect without any change in the current API.

import numpy as np
import matplotlib.pyplot as plt

data1 = np.random.poisson(10, 2000)
data2 = np.random.poisson(20, 1000)

fig, ax = plt.subplots()

ax.hist(data1, bins=np.arange(40), label="Before")
ax.hist(data2, weights=-np.ones_like(data2), bins=np.arange(40), label="After")

ax.axhline(0, color="k")
ax.legend()
fig.savefig("test.png")

Yields:
test

@i-jey
Copy link
Contributor Author

i-jey commented Oct 24, 2023

I think this is a better candidate for an example than an API expansion, setting the weights parameter to -np.ones_like(data2) gets the desired affect without any change in the current API.

import numpy as np
import matplotlib.pyplot as plt

data1 = np.random.poisson(10, 2000)
data2 = np.random.poisson(20, 1000)

fig, ax = plt.subplots()

ax.hist(data1, bins=np.arange(40), label="Before")
ax.hist(data2, weights=-np.ones_like(data2), bins=np.arange(40), label="After")

ax.axhline(0, color="k")
ax.legend()
fig.savefig("test.png")

Yields: test

Ah, that's beautiful and far simpler. Heh, kicking myself for not seeing that earlier. Thanks!

@i-jey i-jey closed this Oct 24, 2023
@i-jey i-jey deleted the bihistogram branch October 24, 2023 08:13
@story645
Copy link
Member

@i-jey would you be interested in adding Kyle's solution to maybe https://matplotlib.org/devdocs/gallery/statistics/histogram_features.html#sphx-glr-gallery-statistics-histogram-features-py as an example of using weights?

@i-jey
Copy link
Contributor Author

i-jey commented Oct 24, 2023

@story645 for sure! I’ll get to that today evening :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.