-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotter.py
More file actions
231 lines (187 loc) · 8.65 KB
/
Copy pathplotter.py
File metadata and controls
231 lines (187 loc) · 8.65 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
import cartopy.crs as ccrs
from cartopy.feature import ShapelyFeature
import geopandas as gpd
import matplotlib.gridspec as gridspec
from matplotlib.lines import Line2D
import matplotlib.pyplot as plt
import numpy as np
from themes import color_themes
from themes import symmetry_classes_1
class Plotter:
def __init__(self, lons=None, lats=None, central_longitude=0, write=False,
highlight=None):
self.lons = lons
self.lats = lats
self.central_longitude = central_longitude
self.write = write
self.highlight = highlight
def correlation_plots(self, x, y, xlabel, ylabel,
cmaps1=('jet', False, None, None), cmaps2=None,
residual=False, rcmaps=('jet', False, None, None),
trend=False, aspect=None, filename=None):
if cmaps2 is None: cmaps2 = cmaps1
if residual:
fig = plt.figure(figsize=(18, 10))
gs = gridspec.GridSpec(2, 2, width_ratios=[1, 1])
z = y - x
zlabel = f"{ylabel} - {xlabel}"
self.global_plot(z, zlabel, cmaps=rcmaps,
fig=fig, figloc=gs[0,1], fig_label='c)')
self.correlation_plot(x, y, xlabel, ylabel,
fig=fig, figloc=gs[1,1], fig_label='d)',
residual=residual, trend=trend, aspect=aspect)
else:
fig = plt.figure(figsize=(14, 10))
gs = gridspec.GridSpec(2, 3, width_ratios=[1, 0.05, 0.5])
self.correlation_plot(x, y, xlabel, ylabel,
fig=fig, figloc=gs[:,2], fig_label='c)',
trend=trend, aspect=aspect)
self.global_plot(y, ylabel, cmaps=cmaps2,
fig=fig, figloc=gs[0,0], fig_label='a)')
self.global_plot(x, xlabel, cmaps=cmaps1,
fig=fig, figloc=gs[1,0], fig_label='b)')
plt.tight_layout()
if self.write: plt.savefig(f"{filename}.png", bbox_inches='tight')
plt.show()
def multi_correlation_plots(self, x, y, z, xlabel, ylabel, zlabel,
cmaps1=('jet', False, None, None),
cmaps2=None, cmaps3=None,
trend=False, aspect=None, filename=None):
if cmaps2 is None: cmaps2 = cmaps1
if cmaps3 is None: cmaps3 = cmaps1
fig = plt.figure(figsize=(14, 15))
gs = gridspec.GridSpec(3, 3, width_ratios=[1, 0.05, 0.5])
self.global_plot(z, zlabel, cmaps=cmaps3,
fig=fig, figloc=gs[0, 0], fig_label='a)')
self.global_plot(y, ylabel, cmaps=cmaps2,
fig=fig, figloc=gs[1, 0], fig_label='b)')
self.global_plot(x, xlabel, cmaps=cmaps1,
fig=fig, figloc=gs[2, 0], fig_label='c)')
self.correlation_plot(z, x, zlabel, xlabel,
fig=fig, figloc=gs[0,2], fig_label='d)',
trend=trend, aspect=aspect)
self.correlation_plot(y, z, ylabel, zlabel,
fig=fig, figloc=gs[1,2], fig_label='e)',
trend=trend, aspect=aspect)
self.correlation_plot(x, y, xlabel, ylabel,
fig=fig, figloc=gs[2,2], fig_label='f)',
trend=trend, aspect=aspect)
plt.tight_layout()
if self.write: plt.savefig(f"{filename}.png", bbox_inches='tight')
plt.show()
def global_plot(self, values, title, cmaps=('jet', False, None, None),
fig=None, figloc=111, fig_label=None, filename=None):
show = False
if fig is None:
show = True
fig = plt.figure(figsize=(9, 5))
cmap, reverse_cmap, vmin, vmax = cmaps
cmap = plt.get_cmap(cmap)
if reverse_cmap: cmap = cmap.reversed()
masked_values = np.ma.masked_invalid(values)
cmap.set_bad('grey')
extend = 'neither'
if vmin == None:
vmin = min(values)
elif vmin > min(values):
extend = 'min'
if vmax == None:
vmax = values.max()
elif vmax < max(values):
if extend == 'min':
extend = 'both'
else:
extend = 'max'
ax = fig.add_subplot(figloc,
projection=ccrs.Robinson(central_longitude=self.central_longitude))
ax.set_title(title, fontsize=20)
ax.set_global()
ax.coastlines(color='dimgrey')
self.plot_boundaries(ax)
scr = ax.scatter(self.lons, self.lats, c=masked_values, alpha=0.6,
edgecolors='w', linewidth=0.5, cmap=cmap,
vmin=vmin, vmax=vmax, transform=ccrs.PlateCarree())
clb = plt.colorbar(scr, extend=extend, orientation='vertical',
fraction=0.0235, pad=0.05)
clb.ax.tick_params(labelsize=10)
if self.highlight:
index = self.highlight - 1
ax.scatter(self.lons[index], self.lats[index], s=200,
facecolors=None, edgecolors='k', linewidth=3, alpha=0.5,
transform=ccrs.PlateCarree())
if fig_label:
ax.text(-0.02, 1.05, fig_label,
transform=ax.transAxes, fontsize='xx-large')
if self.write:
if filename:
plt.savefig(f'{filename}.png', bbox_inches='tight')
if show: plt.show()
def correlation_plot(self, x, y, xlabel, ylabel,
residual=False, trend=False, aspect=None,
fig=None, figloc=111, fig_label=None, filename=None):
show = False
if fig is None:
show = True
fig = plt.figure(figsize=(5,5))
ax = fig.add_subplot(figloc)
ax.scatter(x, y, s=0.4)
ax.set_xlabel(xlabel, fontsize=15)
ax.set_ylabel(ylabel, fontsize=15)
if trend:
ax.plot([-100, 100], [-100, 100],
c='r', linestyle='--', linewidth=0.5)
if self.highlight:
index = self.highlight - 1
ax.scatter(x[index], y[index], s=200,
facecolors=None, edgecolors='k', linewidth=3, alpha=0.5)
ax.plot([x[index], x[index]], [-1, y[index]], c='k', linestyle='--')
ax.plot([-1, x[index]], [y[index], y[index]], c='k', linestyle='--')
side_x = max(x)
side_y = max(y)
if aspect == "equal":
side = max(side_x, side_y)
axis_limits = [-0.05, 1.05, -0.05, 1.05]
ax.axis([item * side for item in axis_limits])
else:
ax.axis([-0.05*side_x, 1.05*side_x, -0.05*side_y, 1.05*side_y])
ax.set_box_aspect(1)
if residual:
ax.text(-0.45, 0.95, fig_label,
transform=ax.transAxes, fontsize='xx-large')
else:
ax.text(-0.2, 1.05, fig_label,
transform=ax.transAxes, fontsize='xx-large')
if self.write:
if filename:
plt.savefig(f'{filename}.png', bbox_inches='tight')
if show: plt.show()
def global_sigma_plot(self, values, color_coastlines,
legend_title, filename=None):
colors = color_themes()
fig = plt.figure(figsize=(9, 5))
ax = fig.add_subplot(111,
projection=ccrs.Robinson(central_longitude=self.central_longitude))
ax.set_global()
ax.coastlines(color=color_coastlines)
self.plot_boundaries(ax)
legend_elements = []
for k, symmetry_class in enumerate(symmetry_classes_1):
color = colors[symmetry_class]
ax.scatter(self.lons[values == k], self.lats[values == k],
color=color, alpha=0.6, edgecolors='w', linewidth=0.5,
transform=ccrs.PlateCarree())
legend_elements.append(Line2D([0], [0], linestyle='-', marker='o',
color=color, label=symmetry_class))
ax.legend(handles=legend_elements, bbox_to_anchor=(1, 1),
loc='upper left', fontsize=9, title=legend_title,
title_fontsize=12)
if self.write:
if filename:
plt.savefig(f'{filename}.png', bbox_inches='tight')
plt.show()
def plot_boundaries(self, ax):
geo_df = gpd.read_file('Data/PB2002_boundaries.json')
for geometry in geo_df['geometry']:
shape_feature = ShapelyFeature([geometry], ccrs.PlateCarree(),
edgecolor='black', facecolor='none')
ax.add_feature(shape_feature)