Mercurial > repos > astroteam > crbeam_astro_tool
comparison spec_plot.py @ 0:f40d05521dca draft default tip
planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit de01e3c02a26cd6353a6b9b6f8d1be44de8ccd54
author | astroteam |
---|---|
date | Fri, 25 Apr 2025 19:33:20 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f40d05521dca |
---|---|
1 import matplotlib.pyplot as plt | |
2 import numpy as np | |
3 | |
4 | |
5 def plot_spec(spec_file, title="", Emin=0, Emax=0, ext="pdf", show=True, logscale=True): | |
6 data = np.loadtxt(spec_file) | |
7 if Emin > 0: | |
8 data = data[Emin <= data[:, 0]] | |
9 else: | |
10 Emin = 10 ** np.floor(np.log10(np.min(data[:, 0]))) | |
11 if Emax > 0: | |
12 data = data[data[:, 0] <= Emax] | |
13 else: | |
14 Emax = 10 ** np.ceil(np.log10(np.max(data[:, 0]))) | |
15 | |
16 ymin = np.min(data[:, 1] - data[:, 1] / np.sqrt(data[:, 2])) | |
17 ymax = np.max(data[:, 1] + data[:, 1] / np.sqrt(data[:, 2])) | |
18 | |
19 if title: | |
20 plt.title(title) | |
21 if logscale: | |
22 plt.xscale("log") | |
23 plt.yscale("log") | |
24 if ymin <= 0: | |
25 ymin = data[:, 1] - data[:, 1] / np.sqrt(data[:, 2]) | |
26 ymin = ymin[ymin > 0] | |
27 if len(ymin) > 0: | |
28 ymin = np.min(ymin) | |
29 else: | |
30 ymin = 0 | |
31 if ymin > 0: | |
32 ymin = 10 ** np.floor(np.log10(ymin)) | |
33 ymax = 10 ** np.ceil(np.log10(ymax)) | |
34 plt.ylim(ymin, ymax) | |
35 else: | |
36 plt.ylim(ymin - 0.1 * (ymax - ymin), ymax + 0.1 * (ymax - ymin)) | |
37 | |
38 plt.xlim(Emin, Emax) | |
39 | |
40 # plt.plot(data[:,2]) | |
41 plt.errorbar( | |
42 data[:, 0], data[:, 1], yerr=data[:, 1] / np.sqrt(data[:, 2]), fmt="-o" | |
43 ) | |
44 out_file = spec_file + "." + ext | |
45 plt.savefig(out_file) | |
46 if show: | |
47 plt.show() | |
48 return out_file |