LDMX-Software / ldmx-sw

The Light Dark Matter eXperiment simulation and reconstruction framework.
https://ldmx-software.github.io
GNU General Public License v3.0
21 stars 19 forks source link

Ratio inset for plots in DQM Validation module #1382

Closed tvami closed 2 weeks ago

tvami commented 1 month ago

Is your feature request related to a problem? Please describe.

Right now the Validation module offers to overlay 1D histograms.

Describe the solution you'd like

I believe it would be super useful to automatically generate a ratio plot in the bottom of the overlaid plot to show the differences.

Describe alternatives you've considered

One could do this outside of the Validation module in a private script, but wouldnt it be easier to do this once and have it in ldmx-sw?

tvami commented 1 month ago

Talked with Pritam privately and he agreed to have a look at this.

tomeichlersmith commented 3 weeks ago

I've done this so many times, here is the matplotlib code to do a raw/ratio plot.

fig, (raw, ratio) = plt.subplots(
    nrows = 2,
    sharex = 'col',
    height_ratios = [2, 1], # height of raw panel relative to ratio panel
    gridspec_kw = dict(
        hspace = 0.05 # separation between panels, can be zero to overlap axis lines
    )
)

raw.hist(..., label='One')
raw.hist(..., label='Two')
raw.set_ylabel('Events')
raw.legend()

ratio.plot(one/two)
ratio.set_ylabel('Ratio')
ratio.set_xlabel('X Label / Units')

fig.savefig('filename.png', bbox_inches='tight')
fig.close() # only helpful in scripts like Validation where we want to cleanup figures that are done
tvami commented 3 weeks ago

Nice! I think what I'd like is to have the ratio inserted in the bottom of the canvas just below the main plot. An example: Screenshot 2024-08-21 at 07 37 33

tomeichlersmith commented 3 weeks ago

That is what the above code does. subplots creates two "axes" in a grid within the "figure".

Vocab Change

matplotlib ROOT
figure TCanvas
axes TPad
axis TAxis

The code above is basically following this guide but with some extra parameters I've learned while tuning the plots to be closer to what we want.

tvami commented 3 weeks ago

Ahh perfect, I didnt read it carefully! Will you PR it @tomeichlersmith ?

tomeichlersmith commented 3 weeks ago

Got a draft on the attached branch :+1: not sure when I'll get a chance to test it

tvami commented 3 weeks ago

I can give a try to testing, do you wanna open a draft PR?