-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
289 lines (241 loc) · 7.57 KB
/
plot.py
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
279
280
281
282
283
284
285
286
287
288
289
# Plot routine for analysis pics
import matplotlib as mpl
from matplotlib import cm # Colormap commands (cm.get_cmap())
from matplotlib import colors
import numpy as np
from pskf.tools.run import pythonmodule as pm
from pskf.tools.plot import plotfunctions as pf
from pskf.tools.plot import specs as sc
from pskf.scripts.analysis import arrays as aa
###############################################################################
# Plot Nonuniform Image of Variable array #
###############################################################################
def plot(
ax,
model_name,
dat,
let,
befaft='aft',
nt=10,
numpy_array_name=None,
is_grid=True,
is_mask=False,
is_labels=True,
is_ownticks=False,
axistitle='',
title_fontsize=30,
varname='kz_mean', # 'head','v','temp','kz', 'uindex'
v_component=1, # 0, 1, 2
is_position=True,
position=[0.1, 0.1, 0.6, 0.8],
is_ownlims=False,
xlims=[0.0, 620.0],
ylims=[0.0, 620.0],
alpha=1.0,
maskvalue=7,
xlabelfontsize=40,
ylabelfontsize=40,
xownticks=[0.1 + i * 0.1 for i in range(9)],
yownticks=[0.1 + i * 0.1 for i in range(9)],
diff_ticks=1,
num_cbar=7,
low_cbar=10.0285,
high_cbar=10.0304,
auto_cbar=True,
pic_format='pdf', # 'png','eps','pdf'
):
"""
A plotting function for variable arrays in a NonUniformGrid.
Parameters
----------
ax : Axes
The axes to draw to.
model_name : string
String of model name.
dat : string
String with date of model run.
let : string
String of letter of model run.
nt : integer
Number inside file name.
numpy_array_name : string
Full file name of numpy array including ending .npy
Returns
-------
ax : Axes
Axes containing image of variable array.
pic_name : string
Containing proposed saving location for Figure.
"""
# Read grid arrays from pskf/tools/plot/specs.py
x = sc.x(model_name, dat, let)
y = sc.y(model_name, dat, let)
xticks = sc.xticks(model_name, dat, let)[::diff_ticks]
yticks = sc.yticks(model_name, dat, let)[::diff_ticks]
# Load variable array
if not numpy_array_name:
var = np.load(
pm.py_output_filename(
aa.tag, varname + '_' + str(nt).zfill(4) + '_' + befaft,
sc.specl(model_name, dat, let), "npy"))
else:
var = np.load(numpy_array_name)
if varname == 'v':
var = var[:, :, v_component]
if auto_cbar:
low_cbar = var.min()
high_cbar = var.max()
# # Possible Mask
if is_mask:
var = np.ma.array(var,
mask=np.logical_or(var < maskvalue - 0.5,
var > maskvalue + 0.5))
# Axis position
if is_position:
ax.set_position(position)
# Create image
im = mpl.image.NonUniformImage(ax,
interpolation='nearest',
cmap=pf.cmap_discretize(
cm.viridis, num_cbar),
norm=colors.Normalize(vmin=low_cbar,
vmax=high_cbar,
clip=False))
im.set_data(x, y, var)
im.set_alpha(alpha)
ax.images.append(im)
# Ticks
if is_ownticks:
ax.xaxis.set_ticks(xownticks)
ax.yaxis.set_ticks(yownticks)
else:
ax.xaxis.set_ticks(xticks)
ax.yaxis.set_ticks(yticks)
# Grid
if is_grid:
ax.grid()
# Title
ax.set_title(axistitle, fontsize=title_fontsize)
# Labels
ax.set_xlabel('[m]', fontsize=xlabelfontsize, visible=is_labels)
ax.set_ylabel('[m]', fontsize=ylabelfontsize, visible=is_labels)
ax.tick_params(length=10 if is_labels else 0)
ax.set_yticklabels(ax.get_yticks(), visible=is_labels)
ax.set_xticklabels(ax.get_xticks(), visible=is_labels)
# Axis Limits
ax.set_xlim(xlims[0], xlims[1])
ax.set_ylim(ylims[0], ylims[1])
# Figure name
if varname == 'v':
varname = varname + '_' + str(v_component)
if is_mask:
varname = varname + '_' + str(maskvalue).zfill(2)
pic_name = pm.py_output_filename(aa.tag, varname + '_' + str(nt).zfill(4),
sc.specl(model_name, dat, let),
pic_format)
return ax, pic_name
def plot_all(
ax,
x,
y,
xticks,
yticks,
var,
pic_name,
numpy_array_name=None,
is_grid=True,
is_mask=False,
is_labels=True,
is_ownticks=False,
axistitle='',
title_fontsize=30,
varname='kz_mean', # 'head','v','temp','kz', 'uindex'
v_component=1, # 0, 1, 2
is_position=True,
position=[0.1, 0.1, 0.6, 0.8],
is_ownlims=False,
xlims=[0.0, 620.0],
ylims=[0.0, 620.0],
alpha=1.0,
maskvalue=7,
colormap=cm.viridis,
xlabelfontsize=40,
ylabelfontsize=40,
xownticks=[0.1 + i * 0.1 for i in range(9)],
yownticks=[0.1 + i * 0.1 for i in range(9)],
diff_ticks=1,
tickfontsize=15,
num_cbar=7,
low_cbar=10.0285,
high_cbar=10.0304,
auto_cbar=True,
):
"""
A plotting function for variable arrays in a NonUniformGrid.
Parameters
----------
ax : Axes
The axes to draw to.
model_name : string
String of model name.
dat : string
String with date of model run.
let : string
String of letter of model run.
nt : integer
Number inside file name.
numpy_array_name : string
Full file name of numpy array including ending .npy
Returns
-------
ax : Axes
Axes containing image of variable array.
pic_name : string
Containing proposed saving location for Figure.
"""
if auto_cbar:
low_cbar = var.min()
high_cbar = var.max()
# # Possible Mask
if is_mask:
var = np.ma.array(var,
mask=np.logical_or(var < maskvalue - 0.5,
var > maskvalue + 0.5))
# Axis position
if is_position:
ax.set_position(position)
# Create image
im = mpl.image.NonUniformImage(ax,
interpolation='nearest',
cmap=pf.cmap_discretize(colormap, num_cbar),
norm=colors.Normalize(vmin=low_cbar,
vmax=high_cbar,
clip=False))
im.set_data(x, y, var)
im.set_alpha(alpha)
ax.images.append(im)
# Ticks
if is_ownticks:
ax.xaxis.set_ticks(xownticks)
ax.yaxis.set_ticks(yownticks)
else:
ax.xaxis.set_ticks(xticks)
ax.yaxis.set_ticks(yticks)
ax.tick_params(axis='both', which='major', labelsize=tickfontsize)
ax.tick_params(axis='both', which='minor', labelsize=tickfontsize-5)
# Grid
if is_grid:
ax.grid()
# Title
ax.set_title(axistitle, fontsize=title_fontsize)
# Labels
ax.set_xlabel('x [m]', fontsize=xlabelfontsize, visible=is_labels)
ax.set_ylabel('y [m]', fontsize=ylabelfontsize, visible=is_labels)
ax.tick_params(length=10 if is_labels else 0)
ax.set_yticklabels(ax.get_yticks(), visible=is_labels)
ax.set_xticklabels(ax.get_xticks(), visible=is_labels)
# Axis Limits
if is_ownlims:
ax.set_xlim(xlims[0], xlims[1])
ax.set_ylim(ylims[0], ylims[1])
return ax, pic_name