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

Allow for irregular bin widths in hist.plot.plot_pull_array #369

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## WIP
* Take into account the irregular bin widths of `Variable` axes in
`plot_pull_array` and `plot_ratio_array` when plotting bars
[#369](https://github.com/scikit-hep/hist/pull/369)

## Version 2.5.2

* Remove more-itertools requirement
Expand Down
19 changes: 13 additions & 6 deletions src/hist/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,15 @@ def _fit_callable_to_hist(

# Infer best fit model parameters and covariance matrix
xdata = histogram.axes[0].centers

# For axes with varying bin widths correct hist values with widths while
# maintaining normalization.
bin_widths = histogram.axes[0].widths
bin_width_fractions = bin_widths / np.sum(bin_widths) * len(bin_widths)
meliache marked this conversation as resolved.
Show resolved Hide resolved
h_values_width_corrected = histogram.values() / bin_width_fractions

popt, pcov = _curve_fit_wrapper(
model, xdata, histogram.values(), hist_uncert, likelihood=likelihood
model, xdata, h_values_width_corrected, hist_uncert, likelihood=likelihood
)
model_values = model(xdata, *popt)

Expand Down Expand Up @@ -425,7 +432,7 @@ def plot_ratio_array(
)
axis_artists = RatioErrorbarArtists(central_value_artist, errorbar_artists)
elif uncert_draw_type == "bar":
bar_width = (right_edge - left_edge) / len(ratio)
bar_widths = __hist.axes[0].widths

bar_top = ratio + ratio_uncert[1]
bar_bottom = ratio - ratio_uncert[0]
Expand All @@ -440,7 +447,7 @@ def plot_ratio_array(
bar_artists = ax.bar(
x_values,
height=bar_height,
width=bar_width,
width=bar_widths,
bottom=bar_bottom,
fill=False,
linewidth=0,
Expand Down Expand Up @@ -494,12 +501,12 @@ def plot_pull_array(
right_edge = __hist.axes.edges[-1][-1]

# Pull: plot the pulls using Matplotlib bar method
width = (right_edge - left_edge) / len(pulls)
bar_artists = ax.bar(x_values, pulls, width=width, **bar_kwargs)
bin_widths = __hist.axes[0].widths
bar_artists = ax.bar(x_values, pulls, width=bin_widths, **bar_kwargs)

pp_num = pp_kwargs.pop("num", 5)
patch_height = max(np.abs(pulls)) / pp_num
patch_width = width * len(pulls)
patch_width = right_edge - left_edge
patch_artists = []
for i in range(pp_num):
# gradient color patches
Expand Down