Skip to content

[BUG] Binning alignment differences between int/regular and own subplots/on the fly #659

@runtingt

Description

@runtingt

Describe the bug

This appears to be somewhat related to #637, #655, with the addition of different behaviour depending on whether one supplies their own axes in plot_ratio. This can be summarised in the following:

Image

The rows define the axis type (Integer or Regular) with the two most recent versions of hist on each side of the plot. Within each half, the columns define wither one supplies their own axes in ax_dict. There are differences in the placement of the bins, the ratio points and the alignment between the two.

Following #637, I'm not exactly sure what the correct behaviour should look like in each case, but at the very least I would expect the behaviour to stay the same between the with/without own suplots (axes) case.

Steps to reproduce

import hist
import matplotlib
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(42)

def _plot_single_ratio_no_subplots(h: hist.Hist, type: str):
    h[:, "a"].plot_ratio(
        h[:, "b"],
    )
    plt.title(f"{type} axis - hist v{hist.__version__}, matplotlib v{matplotlib.__version__}, numpy v{np.__version__}")
    plt.gca().text(0.5, 0.5, "No own subplots", ha="center", va="bottom", transform=plt.gca().transAxes)
    plt.tight_layout()
    plt.savefig(f"{type}_{hist.__version__}_no_subplots.png")
    plt.clf()

def _plot_single_ratio(h: hist.Hist, type: str):
    fig, ax = plt.subplots(nrows=2, ncols=1, gridspec_kw={"height_ratios": [3, 1]})
    h[:, "a"].plot_ratio(
        h[:, "b"],
        ax_dict={"main_ax": ax[0], "ratio_ax": ax[1]},
    )
    fig.suptitle(f"{type} axis - hist v{hist.__version__}, matplotlib v{matplotlib.__version__}, numpy v{np.__version__}")
    ax[0].text(0.5, 0.5, "With own subplots", ha="center", va="bottom", transform=ax[0].transAxes)
    plt.tight_layout()
    plt.savefig(f"{type}_{hist.__version__}.png")
    plt.clf()

data = {
    "a": np.random.randint(-1, 1, size=1000),
    "b": np.random.randint(-1, 1, size=1000),
}

integer_hist_combined = hist.Hist(
    hist.axis.Integer(-1, 1, underflow=False, overflow=False),
    hist.axis.StrCategory(["a", "b"], name="dataset", label="Dataset"),
).fill(data["a"], dataset="a").fill(data["b"], dataset="b")

regular_hist_combined = hist.Hist(
    hist.axis.Regular(2, -1, 1),
    hist.axis.StrCategory(["a", "b"], name="dataset", label="Dataset"),
).fill(data["a"], dataset="a").fill(data["b"], dataset="b")

for hist_obj, hist_type in [
    (integer_hist_combined, "Integer"),
    (regular_hist_combined, "Regular"),
]:
    _plot_single_ratio(hist_obj, hist_type)
    _plot_single_ratio_no_subplots(hist_obj, hist_type)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions