Commit 26d34cb1dbb475752a2c4cbaaf72604d9e302c1b
1 parent
d1eeb716
Improve spectral plotting (automatic dynamic range adjustment)
Showing
1 changed file
with
21 additions
and
5 deletions
examples/show_model.py
| ... | ... | @@ -20,6 +20,7 @@ |
| 20 | 20 | # ========================================================================== |
| 21 | 21 | import gammalib |
| 22 | 22 | import sys |
| 23 | +import math | |
| 23 | 24 | try: |
| 24 | 25 | import matplotlib.pyplot as plt |
| 25 | 26 | except: |
| ... | ... | @@ -41,21 +42,36 @@ def plot_spectrum(model, emin=0.01, emax=100.0, enumbins=100): |
| 41 | 42 | ebounds = gammalib.GEbounds(enumbins, e_min, e_max) |
| 42 | 43 | |
| 43 | 44 | # Setup model plot |
| 44 | - x = [] | |
| 45 | - y = [] | |
| 45 | + x = [] | |
| 46 | + y = [] | |
| 47 | + min = 1.0e30 | |
| 48 | + max = 0.0 | |
| 46 | 49 | for i in range(enumbins): |
| 47 | 50 | energy = ebounds.elogmean(i) |
| 51 | + value = spectrum.eval(energy, gammalib.GTime()) | |
| 52 | + if value > max: | |
| 53 | + max = value | |
| 54 | + if value < min: | |
| 55 | + min = value | |
| 48 | 56 | x.append(energy.TeV()) |
| 49 | - y.append(spectrum.eval(energy, gammalib.GTime())) | |
| 57 | + y.append(value) | |
| 58 | + | |
| 59 | + # Set minimum and maximum | |
| 60 | + max = math.pow(10.0, math.log10(max)+1) | |
| 61 | + if min < 0.0001*max: | |
| 62 | + min = 0.0001*max | |
| 63 | + min = math.pow(10.0, math.log10(min)-1) | |
| 64 | + | |
| 50 | 65 | |
| 51 | 66 | # Show spectrum |
| 52 | 67 | plt.figure() |
| 53 | - plt.title(model.name()) | |
| 68 | + plt.title(model.name()+" ("+spectrum.type()+")") | |
| 54 | 69 | plt.loglog() |
| 55 | 70 | plt.grid() |
| 56 | 71 | plt.loglog(x, y, color='red') |
| 57 | 72 | plt.xlabel("Energy [TeV]") |
| 58 | - plt.ylabel(r"dN/dE [ph s$^{-1}$ cm$^{-2}$ MeV$^{-1}$]") | |
| 73 | + plt.ylabel(r"dN/dE [ph s$^{-1}$ cm$^{-2}$ MeV$^{-1}$]") | |
| 74 | + plt.ylim([min,max]) | |
| 59 | 75 | plt.show() |
| 60 | 76 | |
| 61 | 77 | # Return | ... | ... |