-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiguras.py
More file actions
278 lines (209 loc) · 8.27 KB
/
figuras.py
File metadata and controls
278 lines (209 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""
ATMÓSFERAS ESTELARES - ANÁLISIS DE DOS MODELOS DE ATMÓSFERA
Módulo con las funciones utilizadas para el análisis y la elaboración
de figuras.
Autores: Víctor Alonso, Alejandro Cerón
"""
### Imports:
#############################################################################################
import os
import shutil
import sys
import pdb
import numpy as np
from astropy.constants import M_sun, R_sun, G, R, h, m_e, m_p, c, sigma_sb, k_B
from astropy.io.ascii import read
from astropy import units as u
from astropy.table import QTable
import pandas as pd
pd.set_option('mode.chained_assignment', None)
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
from matplotlib.ticker import FormatStrFormatter
import matplotlib.font_manager as font_manager
### Utilizar LaTeX en las figuras:
#############################################################################################
# Creating a general function to plot
def plot_gen(x_data=[],
y_data=[],
con_factor=1,
label_list=[],
legend_pos = 'upper left',
leg_col=1,
fig_title = None,
fig_name='figure_withnoname',
x_axis_label = 'X label',
x_log_scale = False,
x_invert=False,
x_lim=None,
x_ticks_num = 7,
x_ticks_dec = 2,
x_secaxix_ticks = True,
y_axis_label = 'Y label',
y_log_scale = False,
y_invert = False,
y_lim = None,
y_ticks_num = 7,
y_ticks_dec = 2,
y_secaxix_ticks = True,
line_color = None,
line_alpha = None,
line_style = None,
anchuras = None,
mark_style = None,
mark_size = None,
zorder = None,
scatter_plot = False,
point_lab = None,
aspect_ratio=[1,2],
guide_lines = [False,(None,None)]):
# Determinando el tamaño de las figuras
# Cuánto miden de alto
plot_cols = 1
size_factor_cols = 6*aspect_ratio[0]
fig_size_cols = size_factor_cols * plot_cols
# Cuánto miden de ancho
plot_rows = 1
size_factor_rows = 6*aspect_ratio[1]
fig_size_rows = size_factor_rows * plot_rows
# Tamaño de ls figuras
fig_size_global = (fig_size_rows,fig_size_cols)
# Tamaño de la letra de los ejes
axis_font_size = 30
# Tamaño de la letra de la leyenda
legend_font_size = 26
# Definiendo algunos colores
if line_color is None:
line_color = ['red','dodgerblue','darkorange','lime','gold','deeppink','blueviolet','black']
# Definiendo alpha:
if line_alpha is None:
line_alpha = [1]*len(x_data)
# zorder
if zorder is None:
zorder = [0]*len(x_data)
# Estilos de linea
if line_style is None:
line_style = ['-', '--', '-', '--','-', '--','-', '--','-', '--','-', '--','-', '--']
# Grosor de linea
if anchuras is None:
anchuras = [2]*len(x_data)
# Estilos de marcadores de scatter
if mark_style is None:
mark_style = ['.', '<', '>', 'v','*']
if mark_size is None:
mark_size = [20,10,10,10,10,10]
# Informando en la terminal sobre lo que se está ploteando
print(f'Creando el grafico {fig_name.split("/")[-1]}')
# Creando la figura como un solo subplot con ejes 'ax'
fig = plt.figure(figsize=fig_size_global)
plt.subplots_adjust(hspace=0.5, wspace=0.5)
ax = plt.subplot(plot_rows, plot_cols, 1)
# Si se indica un título se pone. Por defecto desactivado
if fig_title is not None:
fig.suptitle(f'{fig_title}')
# Representando los datos
for data_pos in range(len(x_data)):
plt.plot(np.array(x_data[data_pos])/con_factor,
np.array(y_data[data_pos]),
label=f'{label_list[data_pos]}',
color=line_color[data_pos],
linestyle=line_style[data_pos],
linewidth=anchuras[data_pos],
zorder=zorder[data_pos],
alpha=line_alpha[data_pos])
# Para añadir lineas verticales de referencia
# Por defecto desactivadas
if guide_lines[0]==True:
if guide_lines[1][0] is not None:
ax.axvline(guide_lines[1][0], color="black",linestyle="dashdot", linewidth=1,
zorder=0, alpha=0.9)
if guide_lines[1][1] is not None:
ax.axhline(guide_lines[1][1], color="black",linestyle="dashdot", linewidth=1,
zorder=0, alpha=0.9)
##### Personalizando los ejes
### Eje X inferior
ax.set_xlabel(f'{x_axis_label}',fontsize=axis_font_size)
# Si se dan los limites se ponen sino se calculan
#if x_lim is not None:
#ax.set_xticks(np.linspace(x_lim[0], x_lim[1], x_ticks_num))
#else:
#ax.set_xticks(np.linspace(np.nanmin(x_data)/con_factor, np.nanmax(x_data)/con_factor, x_ticks_num))
ax.set_xmargin(0.05)
ax.minorticks_on()
# Numero de decimales de las etiquetas de los ticks
ax.xaxis.set_major_formatter(FormatStrFormatter(f'%.{x_ticks_dec}f'))
# Escala logarítmica si se indica
if x_log_scale == True:
ax.set_xscale('log')
# Invertir el eje si se indica
if x_invert==True:
ax.invert_xaxis()
### Eje X superior
axxtop = ax.secondary_xaxis('top')
axxbot_ticks = ax.get_xticks()
axxtop.set_xticks(axxbot_ticks)
axxtop.xaxis.set_major_formatter(FormatStrFormatter(f'%.{x_ticks_dec}f'))
# Para poner np los labels sobre los ticks, por defecto se ponen
if x_secaxix_ticks == False:
axxtop.xaxis.set_major_formatter(FormatStrFormatter(''))
axxtop.xaxis.set_minor_formatter(plt.NullFormatter())
axxtop.minorticks_on()
axxtop.tick_params(axis='x', which='major')
# Escala logarítmica si se indica
if x_log_scale == True:
axxtop.set_xscale('log')
# Invertir el eje si se indica
if x_invert==True:
axxtop.invert_xaxis()
### Eje Y izquierdo
ax.set_ylabel(f'{y_axis_label}',fontsize=axis_font_size,labelpad=10)
# Si se dan los limites se ponen sino se calculan
#if y_lim is not None:
#ax.set_yticks(np.linspace(y_lim[0], y_lim[1], y_ticks_num))
#else:
#ax.set_yticks(np.linspace(np.nanmin(y_data), np.nanmax(y_data), y_ticks_num))
ax.set_ymargin(0.1)
ax.minorticks_on()
ax.yaxis.set_major_formatter(FormatStrFormatter(f'%.{y_ticks_dec}f'))
# Escala logarítmica si se indica
if y_log_scale == True:
ax.set_yscale('log')
# Invertir el eje si se indica
if y_invert==True:
ax.invert_yaxis()
### Eje Y derecho
axyrig = ax.secondary_yaxis('right')
# Obteniendo los ticks
axylef_ticks = ax.get_yticks()
axyrig.set_yticks(axylef_ticks)
axyrig.yaxis.set_major_formatter(FormatStrFormatter(f'%.{y_ticks_dec}f'))
# Para no poner los labels sobre los ticks, por defecto se ponen
if y_secaxix_ticks == False:
axyrig.yaxis.set_major_formatter(FormatStrFormatter(''))
axyrig.yaxis.set_minor_formatter(plt.NullFormatter())
axyrig.minorticks_on()
axyrig.tick_params(axis='y', which='major')
# Escala logarítmica si se indica
if y_log_scale == True:
axyrig.set_yscale('log')
# Invertir el eje si se indica
if y_invert==True:
axyrig.invert_yaxis()
#### Otros aspectos a personalizar
# Generando un grid sutil
ax.grid(True,alpha=0.2)
# Propiedades de la leyenda
font = font_manager.FontProperties(size=legend_font_size)
plt.legend(loc=legend_pos,ncol=leg_col,prop=font)
# Dando espacio si se pone el título
fig.tight_layout(pad=0.5)
# Guardando los archivos en pdf o png desde la terminal.
# Por defecto en pdf, para guardar en png usar -png en terminal
if '-png' in sys.argv:
plt.savefig(fig_name+'.png',format='png', dpi=1000, bbox_inches='tight')
else:
plt.savefig(fig_name+'.pdf',format='pdf', dpi=1000, bbox_inches='tight')
# Cerrando el plot
plt.close()