绘制插值函数的等高线图:不同数据部分的不匹配结果
Plotting contour map of interpolated function: unmatching results for different sections of data
我已经为平面中的点网格确定了 z = f(x, y) 的值:
import numpy as np
n_theta, n_phi = 20, 30
thetas = np.linspace(0, 1, n_theta) * np.pi
phis = np.linspace(0, 1, n_phi) * (2 * np.pi)
res = f(thetas, phis)
# 'res' is an np.array with res.shape=(600,3), where each row is [x, y, z]
给定 x
和 y
的 meshgrid
,我使用我以前的 z
数据插入一个函数,并绘制等高线图的结果:
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
X, Y = np.meshgrid(res[:,0], res[:,1])
Z = griddata(res[:,0:2], res[:,2], (X, Y), method='cubic') # Interpolate function.
fig = plt.figure()
contour = plt.contourf(X, Y, Z, levels=100)
这很好用,结果(下面链接的示例数据)是这样的(这实际上不是我在特定模拟中寻找的形状):
但是,当我 运行 具有 phi
值(水平轴)的相同脚本仅占原始间隔的一半时(或者,等效地,只获取我数据的这一半),我得到这样的轮廓:
(为了完整起见,如果我在间隔的后半段进行并手动将两个数字并排放置,结果如下所示,这更像是我期望得到的东西来自完整数据):
(如果需要,这是 example data to generate the plots,后跟一个可以使用的片段):
import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
# Full axis:
res = np.load("Whosebug-example-data")
X, Y = np.meshgrid(res[:,0], res[:,1])
Z = griddata(res[:,0:2], res[:,2], (X, Y), method='cubic') # Interpolate function.
fig = plt.figure()
contour = plt.contourf(X, Y, Z, levels=100)
# Half axis:
half = np.array([res[i,:] for i in range(len(res)) if res[i,0] <= np.pi])
X, Y = np.meshgrid(res[:,0], res[:,1])
Z = griddata(res[:,0:2], res[:,2], (X, Y), method='cubic') # Interpolate function.
fig = plt.figure()
contour = plt.contourf(X, Y, Z, levels=100)
那么我在这里错过了什么?
- 这是按照我的意愿插值和绘制轮廓的正确程序吗?
- 难道我不应该期望整个轴和只有一半的轴得到类似的结果吗?如果不是,为什么?
您的数据已经在网格中。基于那个网格创建一个新的网格会产生一些非常混乱的东西。
现在数组 res
包含 600 个 x,y,z 值。使用 reshape(20, 30)
告诉 numpy 这 600 个条目实际上是 20 行 30 列。显示数据的 'purest' 表格将是一个 scatter
图,仅显示数据。
使用imshow
,数据可以显示为图像。使用 interpolation='nearest'
将 'blow up' 每个 z 值填充一个矩形像素。使用 interpolation='bicubic'
会平滑地抹掉这些像素。
contourf
创建具有等值 z 的轮廓。根据数据的不同,具有多个级别(例如 100)将有助于获得平滑的图像,但与仅显示平滑的图像相比没有价值。具有有限数量的级别(例如 20)可以帮助显示一般的基本形状。
下面是一些用于比较和试验不同方法的代码:
import numpy as np
import matplotlib.pyplot as plt
# Full axis:
res = np.load("Whosebug-example-data.npy")
X = res[:, 0].reshape(20, 30)
Y = res[:, 1].reshape(20, 30)
Z = res[:, 2].reshape(20, 30)
fig, axes = plt.subplots(ncols=3, figsize=(12, 4))
axes[0].scatter(X, Y, c=Z, s=10)
axes[1].imshow(Z, origin='lower', interpolation='bicubic', aspect='auto',
extent=[X.min(), X.max(), Y.min(), Y.max()])
contour = axes[2].contourf(X, Y, Z, levels=20)
axes[0].set_title('scatter plot')
axes[1].set_title('imshow, bicubic interpolation')
axes[2].set_title('contour plot, 20 levels')
plt.show()
试验不同的 colormaps,可以突出显示的函数的不同属性。您还可以转换 Z 值(例如 np.log(Z)
或 np.exp(Z)
),以便不同区域获得或多或少的细节。
for ax, cmap in zip(axes, ('viridis', 'inferno_r', 'Purples')):
ax.imshow(Z, origin='lower', interpolation='bicubic', aspect='auto', cmap=cmap,
extent=[X.min(), X.max(), Y.min(), Y.max()])
ax.set_title(cmap)
我已经为平面中的点网格确定了 z = f(x, y) 的值:
import numpy as np
n_theta, n_phi = 20, 30
thetas = np.linspace(0, 1, n_theta) * np.pi
phis = np.linspace(0, 1, n_phi) * (2 * np.pi)
res = f(thetas, phis)
# 'res' is an np.array with res.shape=(600,3), where each row is [x, y, z]
给定 x
和 y
的 meshgrid
,我使用我以前的 z
数据插入一个函数,并绘制等高线图的结果:
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
X, Y = np.meshgrid(res[:,0], res[:,1])
Z = griddata(res[:,0:2], res[:,2], (X, Y), method='cubic') # Interpolate function.
fig = plt.figure()
contour = plt.contourf(X, Y, Z, levels=100)
这很好用,结果(下面链接的示例数据)是这样的(这实际上不是我在特定模拟中寻找的形状):
但是,当我 运行 具有 phi
值(水平轴)的相同脚本仅占原始间隔的一半时(或者,等效地,只获取我数据的这一半),我得到这样的轮廓:
(为了完整起见,如果我在间隔的后半段进行并手动将两个数字并排放置,结果如下所示,这更像是我期望得到的东西来自完整数据):
(如果需要,这是 example data to generate the plots,后跟一个可以使用的片段):
import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
# Full axis:
res = np.load("Whosebug-example-data")
X, Y = np.meshgrid(res[:,0], res[:,1])
Z = griddata(res[:,0:2], res[:,2], (X, Y), method='cubic') # Interpolate function.
fig = plt.figure()
contour = plt.contourf(X, Y, Z, levels=100)
# Half axis:
half = np.array([res[i,:] for i in range(len(res)) if res[i,0] <= np.pi])
X, Y = np.meshgrid(res[:,0], res[:,1])
Z = griddata(res[:,0:2], res[:,2], (X, Y), method='cubic') # Interpolate function.
fig = plt.figure()
contour = plt.contourf(X, Y, Z, levels=100)
那么我在这里错过了什么?
- 这是按照我的意愿插值和绘制轮廓的正确程序吗?
- 难道我不应该期望整个轴和只有一半的轴得到类似的结果吗?如果不是,为什么?
您的数据已经在网格中。基于那个网格创建一个新的网格会产生一些非常混乱的东西。
现在数组 res
包含 600 个 x,y,z 值。使用 reshape(20, 30)
告诉 numpy 这 600 个条目实际上是 20 行 30 列。显示数据的 'purest' 表格将是一个 scatter
图,仅显示数据。
使用imshow
,数据可以显示为图像。使用 interpolation='nearest'
将 'blow up' 每个 z 值填充一个矩形像素。使用 interpolation='bicubic'
会平滑地抹掉这些像素。
contourf
创建具有等值 z 的轮廓。根据数据的不同,具有多个级别(例如 100)将有助于获得平滑的图像,但与仅显示平滑的图像相比没有价值。具有有限数量的级别(例如 20)可以帮助显示一般的基本形状。
下面是一些用于比较和试验不同方法的代码:
import numpy as np
import matplotlib.pyplot as plt
# Full axis:
res = np.load("Whosebug-example-data.npy")
X = res[:, 0].reshape(20, 30)
Y = res[:, 1].reshape(20, 30)
Z = res[:, 2].reshape(20, 30)
fig, axes = plt.subplots(ncols=3, figsize=(12, 4))
axes[0].scatter(X, Y, c=Z, s=10)
axes[1].imshow(Z, origin='lower', interpolation='bicubic', aspect='auto',
extent=[X.min(), X.max(), Y.min(), Y.max()])
contour = axes[2].contourf(X, Y, Z, levels=20)
axes[0].set_title('scatter plot')
axes[1].set_title('imshow, bicubic interpolation')
axes[2].set_title('contour plot, 20 levels')
plt.show()
试验不同的 colormaps,可以突出显示的函数的不同属性。您还可以转换 Z 值(例如 np.log(Z)
或 np.exp(Z)
),以便不同区域获得或多或少的细节。
for ax, cmap in zip(axes, ('viridis', 'inferno_r', 'Purples')):
ax.imshow(Z, origin='lower', interpolation='bicubic', aspect='auto', cmap=cmap,
extent=[X.min(), X.max(), Y.min(), Y.max()])
ax.set_title(cmap)