1 """
2 Functions to make plots
3 """
4
5 import matplotlib.pyplot as plt
6 from matplotlib.colors import ColorConverter
7 from matplotlib.backends.backend_agg import FigureCanvasAgg
8 import sys
9 import scipy.signal as spsig
10
11
12 import sp, lib, tem
13
14 import numpy as np
15
16
17 -def mix(spectralist, fluxchoice = 'fluxes_csc' , templatelist=[], crosslist = [], peakslist = [], indtemp = True, printing = False):
18 """plots a list of spectra, templates and correlations [not used]"""
19
20 for i in range(len(spectralist)):
21 completeplot(spectralist[i], fluxchoice, templatelist[i], crosslist[i], peakslist[i], mainpeaks[i], printing)
22
23 if not printing:
24 plt.show()
25
27 """plots the lines in a template as vertical lines with a name"""
28
29 for line in template.linelist:
30 plt.axvline(line.angst, color = template.plotcolour, dashes = template.dashes)
31
32 colorConverter = ColorConverter()
33 lightcolor = np.array(ColorConverter.to_rgb(colorConverter, template.plotcolour))[0:3]+np.array([0.5, 0.5, 0.5])
34 plt.text(line.angst, 0.0, line.name, fontsize=12, color=template.plotcolour, horizontalalignment='center', verticalalignment='top')
35
36 -def plottemplate(template, angst, labels = [], withtemp = True):
42
43
44 -def plotpeaks(peaks, labels = [], color = 'red', withpeaks = True):
45 """plots the peaks as vertical lines"""
46 if withpeaks:
47 for peak in peaks[0]:
48 plt.axvline(peak, color = color, dashes = (5,5))
49
50 -def plotspectra(spectra, labels = [], fluxchoice = 'fluxes_csc', withnoise = True, withmask = True):
51 """plots a spectra, the noise, the mask"""
52 """
53 a = spectra.allfluxes[fluxchoice].copy()
54 a[spectra.mask] = 0
55 a = spsig.medfilt(a, 1)
56 """
57 plt.plot(spectra.angst, spectra.allfluxes[fluxchoice], color=spectra.plotcolour, label = fluxchoice)
58 labels.append(fluxchoice)
59 if withnoise:
60 plt.plot(spectra.angst, spectra.noise, color = 'blue', label = 'noise')
61 labels.append('noise')
62 if withmask:
63 plotmask(spectra)
64
65 plt.xlabel("Wavelength [Angstroem]")
66
67
68
69
70
71
72 -def plotcorr(ccsp, labels = [], withcorr = True):
73 """plots the cross correlation"""
74 if withcorr:
75 corr = ccsp.corr
76 x = range(len(corr))
77 z = map(ccsp.spectra.pixtoz, x)
78 plt.plot(z, corr, color = 'black', label = 'cross correlation')
79 plt.xlabel("redshift")
80 labels.append('cross correlation')
81 """
82 def plotcorrlist(ccsplist, labels = []):
83 for item in ccsplist:
84 plotcorr(item, labels)
85 """
86
88 """plots the flux without continuum, once for the fit file and once using the median filter. It also shows the difference between the two fluxes"""
89 plt.plot(spec.angst,spec.fluxes_cs)
90 plt.plot(spec.angst,spec.fluxes_csc)
91 plt.plot(spec.angst, spec.fluxes_csc - spec.fluxes_cs)
92 plt.legend(('cs', 'csc', 'csc-cs'), 'upper right')
93
94 -def completeplot(spec, fluxchoice, temp, ccsp, peaks, mainpeaks, candidat, printing):
95 """plots whatever can be plotted ! """
96
97 fig = plt.figure(figsize = (14,11))
98
99 labels = []
100
101 plt.subplot(311)
102
103 plotcont(spec)
104
105
106 plt.subplot(312)
107 plotspectra(spec, labels, fluxchoice)
108
109 plottemplate(temp,spec.angst, labels)
110
111 plt.legend(labels, 'upper right' )
112
113 s = 'main z : ' + str(spec.zsdss)
114 xy = (0.7, 0.1)
115 plt.text(4000, -2, s)
116 plt.annotate(s, xy, xycoords='figure fraction')
117 plt.title(spec.name)
118
119
120 plt.subplot(313)
121 labels = []
122 plotcorr(ccsp, labels )
123 peaks = map(ccsp.spectra.pixtoz, peaks)
124 plotpeaks(peaks, labels )
125 plotpeaks(mainpeaks, labels, 'blue')
126
127 plt.legend(labels, 'upper right' )
128
129 temparam = temp.getparam()
130 s = ''
131 for i in range(len(temparam)):
132 s += 'height ' + str(i+1) + ' : ' + str(temparam[i]) + '\n'
133 s += ' secondary z : ' + str(candidat.secz)
134 xy = (0.7, 0.1)
135
136 plt.annotate(s, xy, xycoords='figure fraction')
137 if printing:
138 canvas = FigureCanvasAgg(fig)
139 canvas.print_png('../figs/' + spec.name + '.png')
140
141
142 -def plotcand(cand, directory, reftuples, fluxchoice, minangst, maxangst, withmask = True):
143 """plots what can be interesting to check a candidat"""
144
145 spec = cand.spec
146 z = spec.zsdss
147
148 template = tem.template(cand.lines)
149 fig = plt.figure(figsize = (14,11))
150
151 labels = []
152 ax1 = plt.subplot(211)
153
154 plotcont(spec)
155
156 useful_tuples = lib.select_lines(reftuples, z, minangst, maxangst)
157 reftemplate = tem.factory(useful_tuples)
158 reftemplate.setglobalz(z)
159 plottemplate(reftemplate, spec.angst, labels)
160 plt.title(spec.name)
161
162 plt.subplot(212, sharex = ax1, sharey = ax1)
163 labels = []
164 plotspectra(spec, labels, fluxchoice, withmask = withmask)
165 plottemplate(template, spec.angst, labels)
166
167
168
169 plt.legend(labels, 'upper right' )
170
171 s = 'main z : ' + str(z) + '\n'
172 s += ' secondary z : ' + str(cand.secz) + '\n'
173 s += 'quality : ' + str(cand.quality)
174
175 xy = (0.7, 0.1)
176 plt.annotate(s, xy, xycoords='figure fraction')
177 plt.title(cand.name)
178
180 """plots the mask as gray areas"""
181 angst = spec.angst.copy()
182 angst[spec.mask] = 0
183
184 x1 = []
185 x2 = []
186 if angst[0] == 0:
187 x1.append(spec.angst[0])
188
189 for i in range(len(angst)-1):
190 x = angst[i]
191 if x != 0 and angst[i+1] == 0:
192 x1.append(x)
193 if x == 0 and angst[i+1] != 0:
194 x2.append(spec.angst[i])
195
196 if angst[len(angst)-1] == 0:
197 x2.append(spec.angst[-1])
198
199
200 for i in range(len(x1)):
201 plt.axvspan(x1[i], x2[i], facecolor='black', alpha=0.1)
202