gammapy icon indicating copy to clipboard operation
gammapy copied to clipboard

Call to plt.legend() within gammapy.maps.region.ndmap.py triggering a matplotlib info/warning message.

Open mireianievas opened this issue 4 months ago • 2 comments

Gammapy version 1.3.dev726+g6c6d15956

(this is present in the stable release too)

Bug description Loading FluxPointDatasets from a Light Curve file previously generated with the LightCurveEstimator and then running the .plot() method triggers this annoying warning in matplotlib:

No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument..

If you call the method N times for N FluxPointsDatasets, you get that message repeated N times.

The issue comes from gammapy.maps.region.ndmap.py:L132

if len(self.geom.axes) > 1:
    plt.legend()

Here for the LC the geometry contains 2 axes (time and energy) therefore fulfilling that condition.

Expected behavior probably that if should be different, or avoid calling there plt.legend() at all and letting the user handle the legend plotting on demand.

To Reproduce

LC_Swift_XRT = FluxPointsDataset.read("XRT_2.0-10keV.ecsv",name='Swift_XRT_HE')
fig,axs = plt.subplots(nrows=8,ncols=1,figsize=(10,15),dpi=120,sharex=True)
name = LC_Swift_XRT.name
LC_Swift_XRT.data.plot(ax=axs[3],sed_type='eflux',
    color=colors[name],
    marker=markers[name],markersize=4,mfc='white',
    label=name,
)

Example LC file

# %ECSV 1.0
# ---
# datatype:
# - {name: time_min, datatype: float64}
# - {name: time_max, datatype: float64}
# - {name: e_ref, unit: keV, datatype: string, subtype: 'float64[1]'}
# - {name: e_min, unit: keV, datatype: string, subtype: 'float64[1]'}
# - {name: e_max, unit: keV, datatype: string, subtype: 'float64[1]'}
# - {name: ref_dnde, unit: 1 / (keV s cm2), datatype: string, subtype: 'float64[1]'}
# - {name: ref_flux, unit: 1 / (s cm2), datatype: string, subtype: 'float64[1]'}
# - {name: ref_eflux, unit: keV / (s cm2), datatype: string, subtype: 'float64[1]'}
# - {name: norm, unit: '', datatype: string, subtype: 'float64[1]'}
# - {name: norm_err, unit: '', datatype: string, subtype: 'float64[1]'}
# - {name: norm_errn, unit: '', datatype: string, subtype: 'float64[1]'}
# - {name: norm_errp, unit: '', datatype: string, subtype: 'float64[1]'}
# - {name: norm_ul, unit: '', datatype: string, subtype: 'float64[1]'}
# - {name: ts, unit: '', datatype: string, subtype: 'float64[1]'}
# - {name: sqrt_ts, unit: '', datatype: string, subtype: 'float64[1]'}
# - {name: npred, unit: '', datatype: string, subtype: 'float64[1,42]'}
# - {name: npred_excess, unit: '', datatype: string, subtype: 'float64[1,42]'}
# - {name: stat, datatype: string, subtype: 'float64[1]'}
# - {name: stat_null, unit: '', datatype: string, subtype: 'float64[1]'}
# - {name: stat_scan, datatype: string, subtype: 'float64[1,11]'}
# - {name: is_ul, datatype: string, subtype: 'bool[1]'}
# - {name: counts, unit: '', datatype: string, subtype: 'float64[1,42]'}
# - {name: success, unit: '', datatype: string, subtype: 'bool[1]'}
# - {name: norm_scan, unit: '', datatype: string, subtype: 'float64[1,11]'}
# meta: !!omap
# - {MJDREFI: 0}
# - {MJDREFF: 0.0}
# - {TIMESYS: tt}
# - {TIMEUNIT: d}
# schema: astropy-2.0
time_min time_max e_ref e_min e_max ref_dnde ref_flux ref_eflux norm norm_err norm_errn norm_errp norm_ul ts sqrt_ts npred npred_excess stat stat_null stat_scan is_ul counts success norm_scan
60281.12488604712 60281.62344459574 [0.7956129672660611] [0.30000001192092896] [2.109999895095825] [0.0011161887524712864] [0.0019378066080740176] [0.0013955329573471598] [0.7752916265944435] [0.06597860529829877] [0.06414730900105568] [0.06783732481849691] [0.9147446786825122] [831.1093172826522] [28.828966635706042] [[142.07081856176617,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN]] [[140.0352783203125,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN]] [2.558660945115946] [833.6679782277682] [[168.2819549011328,108.46547386964133,57.9279025977955,20.93499775917762,3.1714437999156146,12.333147828088755,58.93070552811681,157.38746998625126,327.54674275100285,596.7454947938338,1002.671290993458]] [false] [[142.0,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN]] [true] [[0.2,0.275945932292243,0.3807307877431757,0.5253055608807534,0.7247796636776955,0.9999999999999998,1.3797296614612147,1.9036539387158784,2.6265278044037665,3.623898318388476,4.999999999999999]]
60284.105210714246 60284.66370593 [0.7956129672660611] [0.30000001192092896] [2.109999895095825] [0.0011161887524712864] [0.0019378066080740176] [0.0013955329573471598] [0.753428309150252] [0.06825394510261418] [0.0662357899132271] [0.07030495023451007] [0.8982149683989826] [761.9098091199592] [27.602713799913936] [[NaN,125.1520959709184,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN]] [[NaN,123.41741943359375,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN]] [2.5884007014517394] [764.4982098214109] [[144.2011679355602,91.8327370394233,48.04070305533804,16.64553134284665,2.7693482107992438,13.368413311036413,57.958589694238064,149.60903057934564,306.30760011879454,552.8405477965421,923.3827842753849]] [false] [[NaN,125.0,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN]] [true] [[0.2,0.275945932292243,0.3807307877431757,0.5253055608807534,0.7247796636776955,0.9999999999999998,1.3797296614612147,1.9036539387158784,2.6265278044037665,3.623898318388476,4.999999999999999]]

Other information Any other information you think will be useful for us to fix the issue can go here.

mireianievas avatar Oct 18 '24 08:10 mireianievas