Skip to content

Commit

Permalink
cleaner order for stacked hist
Browse files Browse the repository at this point in the history
  • Loading branch information
0ctagon committed Oct 16, 2024
1 parent bf7f7af commit 22a8b73
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
27 changes: 13 additions & 14 deletions src/mplhep/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,20 +379,6 @@ def iterable_not_string(arg):
_chunked_kwargs = [_chunked_kwargs[ix] for ix in order]
_labels = [_labels[ix] for ix in order]

elif stack:
# Sort from top to bottom so ax.legend() works as expected
order = np.argsort(label)[::-1]
plottables = [plottables[ix] for ix in order]
_chunked_kwargs = [_chunked_kwargs[ix] for ix in order]
_labels = [_labels[ix] for ix in order]
if "color" not in kwargs:
# Inverse default color cycle
_colors = itertools.cycle(
plt.rcParams["axes.prop_cycle"][len(plottables) - 1 :: -1]
)
for i in range(len(plottables)):
_chunked_kwargs[i].update(next(_colors))

# ############################
# # Stacking, norming, density
if density is True and binwnorm is not None:
Expand Down Expand Up @@ -424,6 +410,19 @@ def iterable_not_string(arg):
##########
# Plotting
return_artists: list[StairsArtists | ErrorBarArtists] = []

if stack:
plottables = plottables[::-1]
_chunked_kwargs = _chunked_kwargs[::-1]
_labels = _labels[::-1]
if "color" not in kwargs:
# Inverse default color cycle
_colors = itertools.cycle(
plt.rcParams["axes.prop_cycle"][len(plottables) - 1 :: -1]
)
for i in range(len(plottables)):
_chunked_kwargs[i].update(next(_colors))

if histtype == "step":
for i in range(len(plottables)):
do_errors = yerr is not False and (
Expand Down
4 changes: 1 addition & 3 deletions src/mplhep/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,6 @@ def to_errorbar(self):


def stack(*plottables):
# Sort from top to bottom so ax.legend() works as expected
plottables = plottables[::-1]
baseline = np.nan_to_num(copy.deepcopy(plottables[0].values), 0)
for i in range(1, len(plottables)):
_mask = np.isnan(plottables[i].values)
Expand All @@ -305,7 +303,7 @@ def stack(*plottables):
baseline += np.nan_to_num(plottables[i].values, 0)
plottables[i].values = np.nansum([plottables[i].values, _baseline], axis=0)
plottables[i].values[_mask] = np.nan
return plottables[::-1]
return plottables


def align_marker(
Expand Down

0 comments on commit 22a8b73

Please sign in to comment.