Commit 45ad9a3b4953ee87983909e5c27d06af7290a299
1 parent
69800b1c
Avoid using of gridspec matplotlib module in show_residuals.py
The module is not available on older matplotlib versions, for example the one installed by default on CentOS 6.
Showing
1 changed file
with
36 additions
and
22 deletions
examples/show_residuals.py
| @@ -24,7 +24,6 @@ import gammalib | @@ -24,7 +24,6 @@ import gammalib | ||
| 24 | import cscripts | 24 | import cscripts |
| 25 | try: | 25 | try: |
| 26 | import matplotlib.pyplot as plt | 26 | import matplotlib.pyplot as plt |
| 27 | - from matplotlib import gridspec | ||
| 28 | plt.figure() | 27 | plt.figure() |
| 29 | plt.close() | 28 | plt.close() |
| 30 | except (ImportError, RuntimeError): | 29 | except (ImportError, RuntimeError): |
| @@ -32,6 +31,26 @@ except (ImportError, RuntimeError): | @@ -32,6 +31,26 @@ except (ImportError, RuntimeError): | ||
| 32 | sys.exit() | 31 | sys.exit() |
| 33 | 32 | ||
| 34 | 33 | ||
| 34 | +# ======================= # | ||
| 35 | +# Set residual label text # | ||
| 36 | +# ======================= # | ||
| 37 | +def set_residual_label(algorithm): | ||
| 38 | + """ | ||
| 39 | + """ | ||
| 40 | + # Set algorithm dependent residual level | ||
| 41 | + if algorithm == 'SUB': | ||
| 42 | + label = 'Residuals (counts)' | ||
| 43 | + elif algorithm == 'SUBDIV': | ||
| 44 | + label = 'Residuals (fraction)' | ||
| 45 | + elif algorithm == 'SUBDIVSQRT' or algorithm == 'SIGNIFICANCE': | ||
| 46 | + label = r'Residuals ($\sigma$)' | ||
| 47 | + else: | ||
| 48 | + label = '' | ||
| 49 | + | ||
| 50 | + # Return label | ||
| 51 | + return label | ||
| 52 | + | ||
| 53 | + | ||
| 35 | # ================================= # | 54 | # ================================= # |
| 36 | # Fill counts/model/residual arrays # | 55 | # Fill counts/model/residual arrays # |
| 37 | # ================================= # | 56 | # ================================= # |
| @@ -186,23 +205,19 @@ def plot_residuals(filename, plotfile, hdu): | @@ -186,23 +205,19 @@ def plot_residuals(filename, plotfile, hdu): | ||
| 186 | if is_onoff: | 205 | if is_onoff: |
| 187 | model_off = [model_off[0]] + model_off | 206 | model_off = [model_off[0]] + model_off |
| 188 | 207 | ||
| 189 | - # Plot | 208 | + # Initialise figure |
| 190 | axarr = [] | 209 | axarr = [] |
| 191 | if is_onoff: | 210 | if is_onoff: |
| 192 | - f = plt.figure(figsize=(8, 4)) | ||
| 193 | - gs = gridspec.GridSpec(2, 2, height_ratios=[2, 1]) | ||
| 194 | - gs.update(hspace=0, left=0.09, right=0.97) | ||
| 195 | - axarr.append(f.add_subplot(gs[0])) | ||
| 196 | - axarr.append(f.add_subplot(gs[2])) | ||
| 197 | - axarr.append(f.add_subplot(gs[1], sharex=axarr[0])) | ||
| 198 | - axarr.append(f.add_subplot(gs[3], sharex=axarr[1])) | 211 | + f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex='col', sharey='row') |
| 212 | + axarr.append(ax1) | ||
| 213 | + axarr.append(ax2) | ||
| 214 | + axarr.append(ax3) | ||
| 215 | + axarr.append(ax4) | ||
| 199 | else: | 216 | else: |
| 200 | - f = plt.figure() | ||
| 201 | - gs = gridspec.GridSpec(2, 1, height_ratios=[2, 1]) | ||
| 202 | - gs.update(hspace=0) | ||
| 203 | - axarr.append(f.add_subplot(gs[0])) | ||
| 204 | - axarr.append(f.add_subplot(gs[1], sharex=axarr[0])) | ||
| 205 | - plt.setp(axarr[1].get_xticklabels(), visible=False) | 217 | + f, (ax1, ax2) = plt.subplots(2, sharex=True) |
| 218 | + axarr.append(ax1) | ||
| 219 | + axarr.append(ax2) | ||
| 220 | + f.subplots_adjust(hspace=0) | ||
| 206 | axarr[0].set_xscale('log') | 221 | axarr[0].set_xscale('log') |
| 207 | axarr[1].set_xscale('log') | 222 | axarr[1].set_xscale('log') |
| 208 | axarr[0].set_yscale('log') | 223 | axarr[0].set_yscale('log') |
| @@ -219,21 +234,19 @@ def plot_residuals(filename, plotfile, hdu): | @@ -219,21 +234,19 @@ def plot_residuals(filename, plotfile, hdu): | ||
| 219 | fmt='ko', capsize=0, linewidth=2, zorder=2) | 234 | fmt='ko', capsize=0, linewidth=2, zorder=2) |
| 220 | axarr[1].axhline(0, color='0.5', linestyle='--') | 235 | axarr[1].axhline(0, color='0.5', linestyle='--') |
| 221 | axarr[1].set_xlabel('Energy (TeV)') | 236 | axarr[1].set_xlabel('Energy (TeV)') |
| 222 | - if algorithm == 'SUB': | ||
| 223 | - axarr[1].set_ylabel('Residuals (counts)') | ||
| 224 | - elif algorithm == 'SUBDIV': | ||
| 225 | - axarr[1].set_ylabel('Residuals (fraction)') | ||
| 226 | - elif algorithm == 'SUBDIVSQRT' or algorithm == 'SIGNIFICANCE': | ||
| 227 | - axarr[1].set_ylabel(r'Residuals ($\sigma$)') | 237 | + axarr[1].set_ylabel(set_residual_label(algorithm)) |
| 228 | 238 | ||
| 229 | # On/Off | 239 | # On/Off |
| 230 | if is_onoff: | 240 | if is_onoff: |
| 241 | + | ||
| 242 | + # Set titles and axes labels | ||
| 231 | axarr[0].set_title('ON') | 243 | axarr[0].set_title('ON') |
| 232 | axarr[2].set_title('OFF') | 244 | axarr[2].set_title('OFF') |
| 233 | - plt.setp(axarr[3].get_xticklabels(), visible=False) | 245 | + axarr[2].set_ylabel('Counts') |
| 234 | axarr[2].set_xscale('log') | 246 | axarr[2].set_xscale('log') |
| 235 | axarr[3].set_xscale('log') | 247 | axarr[3].set_xscale('log') |
| 236 | axarr[2].set_yscale('log') | 248 | axarr[2].set_yscale('log') |
| 249 | + axarr[3].set_ylabel(set_residual_label(algorithm)) | ||
| 237 | 250 | ||
| 238 | # Counts and model | 251 | # Counts and model |
| 239 | axarr[2].errorbar(em_engs, counts_off, yerr=e_counts_off, | 252 | axarr[2].errorbar(em_engs, counts_off, yerr=e_counts_off, |
| @@ -242,6 +255,7 @@ def plot_residuals(filename, plotfile, hdu): | @@ -242,6 +255,7 @@ def plot_residuals(filename, plotfile, hdu): | ||
| 242 | label='Data') | 255 | label='Data') |
| 243 | axarr[2].step(ebounds, model_off, color='0.5', linewidth=2, zorder=1, | 256 | axarr[2].step(ebounds, model_off, color='0.5', linewidth=2, zorder=1, |
| 244 | label='Model') | 257 | label='Model') |
| 258 | + axarr[2].set_xlabel('Energy (TeV)') | ||
| 245 | 259 | ||
| 246 | # Residuals | 260 | # Residuals |
| 247 | axarr[3].errorbar(em_engs, resid_off, yerr=e_resid_off, | 261 | axarr[3].errorbar(em_engs, resid_off, yerr=e_resid_off, |