zblz / naima

Derivation of non-thermal particle distributions through MCMC spectral fitting
http://naima.readthedocs.io
BSD 3-Clause "New" or "Revised" License
43 stars 54 forks source link

Plotting two MCMC fits with same data on one figure #210

Closed jleagle94 closed 2 years ago

jleagle94 commented 3 years ago

Hello,

I have been trying to figure out how to share a figure when plotting a NAIMA fit for two different broadband emission models with the same dataset. No matter what I do, Naima overwrites the previous figure and model fit and only prints out the last calculated fit in the figure.

Here is an example of what I am working with:

fig, ax = plt.subplots(1, 1, sharex=True, sharey=True,figsize=(8,4))

    naima.plot_fit(bb_snr, label='Relativistic Electrons from SNR', sed=True, last_step=False, 
               n_samples=100, confs=[3,2,1], ML_info=False, 
               figure=fig, plotdata=True, plotresiduals=True, 
               e_unit=u.eV, e_range=(5.789e-05*u.eV,200*u.TeV), e_npoints=200, threads=3, 
               xlabel=None, ylabel=None, ulim_opts={}, errorbar_opts={})

    naima.plot_fit(bb_pwn, label='Relativistic Electrons from PWN', sed=True, last_step=False, 
               n_samples=100, confs=[3,2,1], ML_info=False, 
               figure=fig, plotdata=True, plotresiduals=True, 
               e_unit=u.eV, e_range=(5.789e-05*u.eV,200*u.TeV), e_npoints=200, threads=3, 
               xlabel=None, ylabel=None, ulim_opts={}, errorbar_opts={})

    fig.axes[0].set_ylim(bottom=1e-18)
    fig.axes[0].set_ylim(top=1e-10)

currently, the first fit gets overwritten completely by the last fit. I'm hoping someone has an easy solution to this.

zblz commented 2 years ago

Hi @jleagle94,

The naima.plot module has two lower level functions (plot_CI and plot_samples) that plot a model straight into an existing axis. From your snippet above, it seems like plot_CI is the one you want. You could create the first one with plot_fit, so that the data and residuals are plotted, and then add confidence intervals of a different model with plot_CI. To plot the residuals for the second model you would need to use the naima.plot._plot_residuals_to_ax functions, which is even lower level so I would not recommend using without understanding what the code is doing and how it is being used in plot_fit.