plot_samples_violin_vs_energy#

gammapy.visualization.plot_samples_violin_vs_energy(energy_edges, samples_per_band, weights_per_band=None, energy_power=0, bw_method='scott', grid_size=200, ax=None, errorbar_kwargs=None, errorbar_ul_kwargs=None, violin_kwargs=None, violin_clip=None, y_label='dN/dE')[source]#

Plot sample violin distributions per energy bin on log–log axes.

Unlike standard error bars which only shows summary statistics, a violin plot shows the full probability density for each energy bin.

This function draws one violin per energy interval, using weighted kernel-density estimation (KDE) in log-space. The median and 1σ-equivalent quantiles are overlaid using Gammapy-style flux-point error bars. Users can optionally apply an E_center**p scaling to the flux values and clip violin tails to a specified containment interval. Samples of other energy-dependent parameters can also be plotted, in that case the y_label should be changed accordingly.

Parameters:
energy_edgesndarray or Quantity

Monotonically increasing energy bin edges of length nbins + 1. Must be positive and finite for log scaling.

samples_per_bandlist of Quantity

List of sample arrays, one per energy bin. Each array contains draws from the flux posterior (or other distribution) in that bin.

weights_per_bandlist of ndarray, optional

Per-sample weights for each bin, same length as samples_per_band. If omitted, uniform weights are assumed.

energy_powerfloat, optional

Power of energy to multiply y-axis with. Default is 0.

bw_methodstr or float, optional

Bandwidth selection passed to gaussian_kde. Default is “scott”.

grid_sizeint, optional

Number of evaluation points in log-flux space for the KDE grid. Default is 200.

axAxes, optional

Matplotlib axes to draw the violins and error bars. Default is None.

errorbar_kwargsdict, optional

Keyword arguments forwarded to errorbar for the quantile bars.

errorbar_ul_kwargsdict, optional

Keyword arguments forwarded to errorbar for the upper limit bars.

violin_kwargsdict, optional

Keyword arguments forwarded to fill for violin plot.

violin_clip(float, float), optional

Lower and upper containment fractions (in [0, 1]) used to clip the violin tails in log-space. If omitted, defaults to (norm.cdf(-4), norm.cdf(4)).

y_labelstr, optional

Base label for the y-axis, before unit and scaling prefixes. Default is “dN/dE”.

Returns:
axAxes

Matplotlib axes.

Notes

  • KDE is performed on the transformed variable log10(E^p * F), where p = energy_power. The violin polygon is constructed by mirroring the normalized log-density around the bin center in log-energy.

  • Violin clipping is applied only to the KDE grid, not to the quantile-based error bars.

  • Negative flux samples are replaced by a small positive surrogate (10% of the smallest positive sample) to allow log-space evaluation.

  • The plotted error bars correspond to the 16%, 50%, and 84% weighted percentiles of the unclipped samples, unless the median is non-positive, in which case an upper limit marker is drawn at the 98% percentile. For a gaussian distribution these values corresponds to 1σ errors and 2σ upper limit plotted by default by the plot method.