我有三个数组
theta
array([180., 180., 180., 180., 180., 180., 180., 180., 180., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 225., 225., 225.,
225., 225., 225., 225., 225., 225., 45., 45., 45., 45., 45.,
45., 45., 45., 45., 45., 270., 270., 270., 270., 270., 270.,
270., 270., 270., 90., 90., 90., 90., 90., 90., 90., 90.,
90., 90., 315., 315., 315., 315., 315., 315., 315., 315., 315.,
135., 135., 135., 135., 135., 135., 135., 135., 135., 135.])
phi
array([45. , 39.98823529, 35.01176471, 30. , 24.98823529,
20.01176471, 15. , 9.98823529, 5.01176471, 0. ,
5.01176471, 9.98823529, 15. , 20.01176471, 24.98823529,
30. , 35.01176471, 39.98823529, 45. , 45. ,
39.98823529, 35.01176471, 30. , 24.98823529, 20.01176471,
15. , 9.98823529, 5.01176471, 0. , 5.01176471,
9.98823529, 15. , 20.01176471, 24.98823529, 30. ,
35.01176471, 39.98823529, 45. , 45. , 39.98823529,
35.01176471, 30. , 24.98823529, 20.01176471, 15. ,
9.98823529, 5.01176471, 0. , 5.01176471, 9.98823529,
15. , 20.01176471, 24.98823529, 30. , 35.01176471,
39.98823529, 45. , 45. , 39.98823529, 35.01176471,
30. , 24.98823529, 20.01176471, 15. , 9.98823529,
5.01176471, 0. , 5.01176471, 9.98823529, 15. ,
20.01176471, 24.98823529, 30. , 35.01176471, 39.98823529,
45. ])
violet_ints
array([ 60821.47285091, 71469.47778826, 81950.58139812, 92660.95741024,
103228.07719577, 113446.6171393 , 123862.48451513, 132666.65201113,
138673.94497866, 141418.87424494, 140947.68057807, 137704.80141046,
131712.10023037, 122871.74227686, 111022.93897237, 98051.87876927,
85884.71515151, 74789.81179595, 64223.21535332, 47239.6029958 ,
59060.46569283, 71631.51992138, 85056.88727878, 97838.32318773,
110985.04206186, 123046.60805235, 131994.74029036, 138009.81758468,
141092.29699058, 141436.99265739, 139367.40863866, 134564.53259257,
127256.22737589, 117476.56579378, 105893.78184852, 94434.79559289,
83012.77977437, 71513.83906988, 35465.27768276, 52489.59008641,
70259.4064192 , 87011.24343702, 101845.51856053, 114496.37908394,
125113.38041456, 132956.15831876, 138245.26502938, 140988.36471711,
141467.04309028, 139302.73813352, 135346.73647267, 128688.98045284,
120273.38645452, 110273.45442647, 99177.33744858, 87341.78739578,
74874.97094334, 50986.33091372, 63371.63698099, 76937.62936534,
91341.43206481, 105418.35862792, 117371.0063522 , 127397.07283259,
134693.40421132, 139258.98685025, 141392.73492377, 140797.81487188,
137360.21016255, 130967.39590833, 122385.40640793, 113290.91743172,
103305.45203984, 93133.15852394, 82350.70121816, 71046.51493323])
我用这些数组制作了极坐标图
plt.figure(figsize=(10, 20))
ax = plt.axes(projection='polar')
axx = ax.scatter(theta*np.pi/180, phi, c=violet_ints,
cmap='viridis')
plt.colorbar(axx, shrink = 0.4);
现在我想对这些数据进行插值,以便获得像这样的颜色网格图。
插值图:
我尝试将数组转换为网格
Theta, Phi = np.meshgrid(theta, phi)
_, Violet_Ints = np.meshgrid(violet_ints, violet_ints)
但是,当我尝试使用 pcolormesh 绘制绘图时,它没有意义
plt.figure(figsize=(10, 20))
ax = plt.axes(projection='polar')
ax.pcolormesh(Theta*np.pi/180, Phi, Violet_Ints,
cmap='viridis');
我尝试绘制网格:
你们能帮我解决这个问题吗?
您缺少的主要内容是
pcolormesh
尝试在相邻值之间进行插值。这意味着数据的排序很重要。修复此问题后,您需要做一些 numpy
来确保满足 .pcolormesh
需要的坐标系。
使用上面的变量:
import matplotlib.pyplot as plt
import numpy as np
theta = np.radians(theta)
idx = np.argsort(theta)
theta, phi, violet_ints = theta[idx], phi[idx], violet_ints[idx] # reroder data according to theta
theta_g, phi_g = np.meshgrid(theta, phi) # coordinates of theta/pho
_, violet_g = np.meshgrid(violet_ints, violet_ints) # tile violet to align on coordinate grid
theta_g = np.hstack([theta_g, theta_g[:, [0]]]) # append a column that is same as the first
phi_g = np.hstack([phi_g, phi_g[:, [0]]])
mosaic = [
['scatter', '.' ],
['flat', 'gouraud'],
]
fig, axd = plt.subplot_mosaic(
mosaic, figsize=(15, 15), subplot_kw={'projection': 'polar'}, gridspec_kw={'hspace': .5}
)
# plot scatter
axd['scatter'].scatter(theta, phi, c=violet_ints, cmap='viridis')
axd['scatter'].set_title('Scatter', size='xx-large', loc='left')
# plot "Flat Shading" pcolormesh
violet_g_flat = violet_g[:-1, :]
axd['flat'].pcolormesh(theta_g, phi_g, violet_g_flat, cmap='viridis', shading='flat');
axd['flat'].set_title('Flat Shading', loc='left', size='xx-large')
# plot "Gouraud Shading" pcolormesh
# note that we use the same trick to "wrap" out colors
violet_g_gouraud = np.hstack([violet_g, violet_g[:, [0]]])
axd['gouraud'].pcolormesh(theta_g, phi_g, violet_g_gouraud, cmap='viridis', shading='gouraud');
axd['gouraud'].set_title('Gouraud Shading', loc='left', size='xx-large')
plt.show()