ldmx-sw icon indicating copy to clipboard operation
ldmx-sw copied to clipboard

Ratio inset for plots in DQM Validation module

Open tvami opened this issue 1 year ago • 1 comments

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 avatar Aug 06 '24 21:08 tvami

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

tvami avatar Aug 13 '24 02:08 tvami

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

tomeichlersmith avatar Aug 21 '24 13:08 tomeichlersmith

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

tvami avatar Aug 21 '24 14:08 tvami

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.

tomeichlersmith avatar Aug 21 '24 15:08 tomeichlersmith

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

tvami avatar Aug 21 '24 15:08 tvami

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

tomeichlersmith avatar Aug 21 '24 16:08 tomeichlersmith

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

tvami avatar Aug 21 '24 17:08 tvami