scikit-hep / mplhep

Extended histogram plotting on top of matplotlib and HEP collaboration compatible styling
https://mplhep.readthedocs.io
MIT License
185 stars 64 forks source link

fix: legend linestyle for step histogram #496

Closed 8me closed 5 months ago

8me commented 5 months ago

The legend entries for mplhep.histplot for histtype='step' shows always continuous entires no matter what linestyle arg is defined. In order to fix that the linestyle is forwarded to _e_leg and xerr is set to None as it is not used in the step mode.

andrzejnovak commented 5 months ago

Hi @8me thanks for the PR! I think I understand the change, but could you add a picture here just for posterity?

8me commented 5 months ago

@andrzejnovak Here we go 😉 ... ok I created this example script:

#!/usr/bin/env python
# coding=utf-8
# Filename: test.py
"""
MPLHEP testplot script

"""
import mplhep
import numpy as np
import matplotlib.pyplot as plt

BINS = np.linspace(0, 1, 10)

def main():
    samples = np.random.normal(loc=0.5, scale=0.1, size=1000)
    wgts = np.random.rand(1000)
    h = np.histogram(samples, bins=BINS, weights=wgts)
    h2 = np.histogram(samples, bins=BINS, weights=wgts**2)
    fig, ax = plt.subplots()
    mplhep.histplot(h, w2=h2[0], linestyle=':', ax=ax, label="Test Hist")
    ax.legend()
    plt.savefig("new.pdf")

if __name__ == '__main__':
    main()

So with the current master the legend for a histogram with linestyle=':' looks like: old ... with the changes on this PR it would look like: new

andrzejnovak commented 5 months ago

Perfect, thanks for the PR!