我在极地格式的圆形区域的不同点处拍摄了一小组不规则间隔的数据。我需要进行插值以在规则间隔的网格上获取数据,然后我想使用等高线图绘制它们。
我已经设法进行插值并绘制结果,但我必须从极坐标转换为直角坐标进行插值,当我将数据转换回极坐标时,我会在极坐标图上得到伪影。 / p>
以下代码演示了到目前为止我所拥有的内容,并在极坐标和矩形图上绘制数据:
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import Rbf
# inputs as 1D arrays
r = np.array([0, 1, 1, 1, 1, 2, 2, 2, 2])
theta = np.radians(np.array([0, 90, 180, 270, 0, 90, 180, 270, 0]))
# z = f(theta, r)
z = np.array([8, 7, 6, 4, 5, 2, 2, 2, 2])
# convert to rect
x = r * np.cos(theta)
y = r * np.sin(theta)
# create RBF for smoothing
rbf = Rbf(x, y, z)
# create grid to smooth over
xi, yi = np.mgrid[-2:2:10j, -2:2:10j]
# smooth
zi = rbf(xi, yi)
# convert back to polar
ri = np.sqrt(xi*xi + yi*yi)
ti = np.arctan2(yi, xi)
# polar plot
fig = plt.figure()
ax = plt.subplot(121, polar=True)
cax = ax.contour(ti, ri, zi, 10, linewidths=0.5, colors='k')
cax = ax.contourf(ti, ri, zi, 10, cmap=plt.cm.Spectral)
ax.set_rmax(2)
# rect plot
ax = plt.subplot(122)
cax = ax.contour(xi, yi, zi, 10, linewidths=0.5, colors='k')
cax = ax.contourf(xi, yi, zi, 10, cmap=plt.cm.Spectral)
plt.show()
剩下的问题是:
答案 0 :(得分:3)
您可能也想阅读this,但就极坐标中的等值线图而言,matplotlib
期望在半径和角度中有规律的网格阵列 ,所以你可以很好地策划:
# polar plot
ri, ti = np.mgrid[0:2:100j, 0:2*np.pi:100j]
zi = rbf(ri*np.cos(ti), ri*np.sin(ti))
fig = plt.figure()
ax = plt.subplot(121, polar=True)
cax = ax.contour(ti, ri, zi, 10, linewidths=0.5, colors='k')
cax = ax.contourf(ti, ri, zi, 10, cmap=plt.cm.Spectral)
ax.set_rmax(2)
# rect plot
xi, yi = np.mgrid[-2:2:100j, -2:2:100j]
zi = rbf(xi, yi)
ax = plt.subplot(122, aspect='equal')
cax = ax.contour(xi, yi, zi, 10, linewidths=0.5, colors='k')
cax = ax.contourf(xi, yi, zi, 10, cmap=plt.cm.Spectral)
plt.show()
我对你使用Rbf
感到有些惊讶。你到底要做什么以及为什么要使用插补器?