-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotlib.py
182 lines (141 loc) · 5.36 KB
/
plotlib.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import matplotlib.colors as colors
def get_grid2D(theta, phi):
nphi = len(phi)
ntheta = len(theta)
p2D = np.zeros([nphi, ntheta])
th2D = np.zeros([nphi, ntheta])
for i in range(nphi):
p2D[i,:] = phi[i]
for j in range(ntheta):
th2D[:, j] = theta[j]
return p2D, th2D
def radContour(theta, phi, data, grid, levels, cm, proj, plotbg, titl=None):
if plotbg == 'dark':
plt.style.use("dark_background")
plt.rcParams.update({
"axes.facecolor" : "#1b1b1b",
"figure.facecolor" : "#1b1b1b",
"figure.edgecolor" : "#1b1b1b",
"savefig.facecolor": "#1b1b1b",
"savefig.edgecolor": "#1b1b1b"})
lc = 'w'
elif plotbg == 'light':
lc = 'k'
p2D, th2D = get_grid2D(theta, phi)
p2D = p2D - np.pi
th2D= np.pi/2 - th2D
lon = p2D * 180./np.pi
lat = th2D * 180./np.pi
fig = plt.figure(figsize=(12,6.75))
if proj == 'Orthographic':
fig = plt.figure(figsize=(10, 10))
plotcrs = ccrs.Orthographic(0, 40)
else:
plotcrs = eval('ccrs.'+proj+'()')
ax = fig.add_subplot(1, 1, 1, projection=plotcrs)
datMax = (np.abs(data)).max()
divnorm = colors.TwoSlopeNorm(vmin=-datMax, vcenter=0, vmax=datMax)
if grid:
ax.gridlines(linewidth=1, color='gray', alpha=0.5, linestyle=':')
if proj == "Orthographic":
cont = ax.pcolormesh(lon, lat, data, \
transform=ccrs.PlateCarree(), cmap=cm, \
norm=divnorm)
else:
cont = ax.contourf(lon, lat, data, levels, \
transform=ccrs.PlateCarree(), cmap=cm, \
norm=divnorm)
for c in cont.collections:
c.set_edgecolor("face")
if titl is not None:
ax.set_title(titl,fontsize=30)
# if vec:
# ut = self.U.Us*cos(self.grid.th3D) - self.U.Uz*sin(self.grid.th3D)
# ut = ut[::vecStride,::vecStride,idxPlot]
# up = self.U.Up[::vecStride,::vecStride,idxPlot]
# lon = lon[::vecStride,::vecStride]
# lat = lat[::vecStride,::vecStride]
# #ax.quiver(lon,lat,up,ut,transform=plotcrs,width=vecWidth,scale=vecScale,regrid_shape=20)
# ax.quiver(lon,lat,up,ut,transform=plotcrs)#,regrid_shape=regrid_shape)
plt.axis('equal')
plt.axis('off')
plt.tight_layout()
def merContour(r, theta, data, levels, cm, plotbg, titl=None):
if plotbg == 'dark':
plt.style.use("dark_background")
plt.rcParams.update({
"axes.facecolor" : "#1b1b1b",
"figure.facecolor" : "#1b1b1b",
"figure.edgecolor" : "#1b1b1b",
"savefig.facecolor": "#1b1b1b",
"savefig.edgecolor": "#1b1b1b"})
lc = 'w'
elif plotbg == 'light':
lc = 'k'
rr, tth = np.meshgrid(r, theta)
xx = rr*np.sin(tth)
yy = rr*np.cos(tth)
plt.figure(figsize=(5, 10))
datMax = (np.abs(data)).max()
divnorm = colors.TwoSlopeNorm(vmin=-datMax, vcenter=0, vmax=datMax)
cont = plt.contourf(xx, yy, data, levels, cmap=cm, norm=divnorm)
plt.plot(r[0]*np.sin(theta), r[0]*np.cos(theta), lc, lw=1)
plt.plot(r[-1]*np.sin(theta), r[-1]*np.cos(theta), lc, lw=1)
plt.plot([0, 0], [ r.min(), r.max() ], lc, lw=1)
plt.plot([0, 0], [ -r.max(), -r.min() ], lc, lw=1)
for c in cont.collections:
c.set_edgecolor("face")
if titl is not None:
plt.title(titl,fontsize=20)
plt.axis('equal')
plt.axis('off')
plt.tight_layout()
def eqContour(r, phi, data, levels, cm, plotbg, titl=None):
if plotbg == 'dark':
plt.style.use("dark_background")
plt.rcParams.update({
"axes.facecolor" : "#1b1b1b",
"figure.facecolor" : "#1b1b1b",
"figure.edgecolor" : "#1b1b1b",
"savefig.facecolor": "#1b1b1b",
"savefig.edgecolor": "#1b1b1b"})
lc = 'w'
elif plotbg == 'light':
lc = 'k'
phi2D, r2D = np.meshgrid(phi, r, indexing='ij')
xx = r2D * np.cos(phi2D)
yy = r2D * np.sin(phi2D)
plt.figure(figsize=(10, 10))
datMax = (np.abs(data)).max()
divnorm = colors.TwoSlopeNorm(vmin=-datMax, vcenter=0, vmax=datMax)
cont = plt.contourf(xx, yy, data, levels, cmap=cm, norm=divnorm)
plt.plot(r[0]*np.cos(phi), r[0]*np.sin(phi), lc, lw=1)
plt.plot(r[-1]*np.cos(phi), r[-1]*np.sin(phi), lc, lw=1)
for c in cont.collections:
c.set_edgecolor("face")
if titl is not None:
plt.title(titl,fontsize=20)
plt.axis('equal')
plt.axis('off')
plt.tight_layout()
def surface3D(x,y,z,idx,ux,uy,uz,dat,cm='seismic',quiv=True,fac=0.01,col=True):
from mayavi import mlab
lut=eval('plt.cm.'+cm+'(np.linspace(0,1,255))*255')
if col:
col = (0.43, 0.43, 0.43)
else:
col = None
mlab.figure(size=(800, 800))
mesh_handle = mlab.mesh(x[..., idx], y[..., idx], z[..., idx], scalars=dat)
mesh_handle.module_manager.scalar_lut_manager.lut.table = lut
# mesh_handle.module_manager.scalar_lut_manager.reverse_lut = True
if quiv:
mlab.quiver3d(x, y, z, ux, uy, uz, color=col, scale_mode='vector',
mode='arrow', mask_points=4, scale_factor=fac)
#mlab.show()
mlab.draw()