Repository: gammapy/gammapy

Issue:
Plot of a SpectralModel with the sed type `flux'
**Gammapy version**
The dev version (I have not tested older versions)

**Bug description**
Using the Spectral analysis tutorial, the script works any issue. I have superposed the SpectralModel on the TS scan plot with the sed_type `e2dnde':
![e2dnde](https://github.com/gammapy/gammapy/assets/16781593/5a48b388-5f38-43b8-baab-b8cda1d53634)

When I use the sed_type `flux', the result is the following:
![flux](https://github.com/gammapy/gammapy/assets/16781593/5e09a14c-f731-4833-b5ae-9baa4e7a2814)




Discussion:
Thanks @bkhelifi for the issue! 

I remember I have seen this in the past and the conclusion was that this is expected behavior: when plotting an integral flux quantity it is important, that the energy bins match between the spectral model and the flux points. This is just math. But we should make people aware of this by adding this to our docs.
Maybe we can adapt our tutorial to say something like 

```
axis = flux_points.geom.axes["energy"]
spectral_model.plot(energy_bounds=(axis.edges_min[0], axis.edges_max[-1]),
                                    sed_type="flux", n_points=axis.nbin)
```
@AtreyeeS We should probably even change `SpectralModel.plot()` to allow for passing energy axis. Such that the following works:

```python
axis = flux_points.geom.axes["energy"]
spectral_model.plot(energy_axis=axis, sed_type="flux")
```
> Maybe we can adapt our tutorial to say something like
> 
> ```
> axis = flux_points.geom.axes["energy"]
> spectral_model.plot(energy_bounds=(axis.edges_min[0], axis.edges_max[-1]),
>                                     sed_type="flux", n_points=axis.nbin)
> ```

fig, ax = plt.subplots()
flux_points.plot(ax=ax, sed_type="flux", color="darkorange")
flux_points.plot_ts_profiles(ax=ax, sed_type="flux")
axis = flux_points.energy_axis
model_best_joint.spectral_model.plot(energy_bounds=(axis.edges_min[0], axis.edges_max[-1]),
                                    sed_type="flux", n_points=axis.nbin, ax=ax)
plt.savefig("flux.png")
plt.show()

![flux](https://github.com/gammapy/gammapy/assets/16781593/b27abd51-60dc-494e-9c7f-a7a3e1bf734d)

> We should probably even change `SpectralModel.plot()` to allow for passing energy axis. Such that the following works:

@adonath Personally, +1. But we had discussed this in details in https://github.com/gammapy/gammapy/issues/3078, and I think the consensus was to pass Quantities instead of axes for plotting... 

@AtreyeeS Thanks for pointing to the discussion. I guess we wanted to keep the convenience of not having to define an axis just for plotting. However I think it would be ok to just support both and the if defined, the axis object gets preference. 

Resolving patch:
diff --git a/examples/tutorials/analysis-1d/spectral_analysis.py b/examples/tutorials/analysis-1d/spectral_analysis.py
--- a/examples/tutorials/analysis-1d/spectral_analysis.py
+++ b/examples/tutorials/analysis-1d/spectral_analysis.py
@@ -418,6 +418,12 @@
 flux_points.plot_ts_profiles(ax=ax, sed_type="e2dnde")
 plt.show()
 
+######################################################################
+# Note: it is also possible to plot the flux distribution with the spectral model overlaid,
+# but you must ensure the axis binning is identical for the flux points and
+# integral flux.
+#
+
 
 ######################################################################
 # The final plot with the best fit model, flux points and residuals can be


Test patch: