Skip to content

plotdataMC.py: soft_fail in mplhep.mpl_magic function #135

@felixzinn

Description

@felixzinn

Hi,

i noticed, that in the plotting script plotdataMC.py the mplhep.mpl_magic function is used

try:
hep.mpl_magic(ax=ax)
except RuntimeError as e:
print(f"Warning: {e}")
print("Using soft_fail=True for legend placement")
try:
# Try with soft_fail=True
hep.mpl_magic(ax=ax, soft_fail=True)
except Exception as e2:
print(f"Still failed: {e2}")
# Continue anyway - the plot will still be usable

however in mplhep there is no such argument (see the definition in mplhep)

it is only available in the yscale_legend function (here) which is used inside mpl_magic but the argument cannot be passed for mpl_magic directly

what we could do is fix the definition of mpl_magic for now like this

import matplotlib.pyplot as plt
from mplhep.plot import ylow, yscale_legend, yscale_anchored_text


# patch mplhep mpl_magic function to allow soft_fail=True
def mpl_magic(ax=None, info=True, soft_fail=True):
    """
    Consolidate all ex-post style adjustments:
        ylow
        yscale_legend
    """
    if ax is None:
        ax = plt.gca()
    if info:
        pass

    ax = ylow(ax)
    ax = yscale_legend(ax, soft_fail=soft_fail)
    return yscale_anchored_text(ax)

...

mpl_magic(ax=ax)

maybe we should also open an issue on the mplhep github issue?

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