【发布时间】:2021-04-08 03:02:57
【问题描述】:
我有 17 个测量点,我想在它们之间推断一个圆圈。这是我尝试过的:
import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
import math
# Measurement data
x1 = np.array([[-144.00, -101.80, -101.80, -75.00, -53.00, -53.00, 0.00, 0.00, 0.00, 0.00, 0.00, 53.00, 53.00, 75.00, 101.80, 101.80, 144.00]])
y1 = np.array([[0.00, 101.80, -101.80, 0.00, 53.00, -53.00, 144.00, 75.00, 0.00, -75.00, -144.00, 53.00, -53.00, 0.00, 101.80, -101.80, 0.00]])
z1 = np.array([148.3861807, 148.9051447, 148.0415147, 147.9976293, 147.98485, 147.9579673, 148.89261, 148.0217707, 147.9312247, 147.7952, 147.3225247, 148.2489567, 148.2120013, 148.3169953, 149.578092, 147.9356893, 148.556672])
# Convert to polar coords
r1 = np.sqrt(x1**2 + y1**2)
t1 = np.arctan2(y1, x1)
# Add cyclic points
for i in range(0,x1.shape[1]):
if np.arctan2(y1[0,i], x1[0,i]) == math.pi:
print(np.sqrt(x1[0,i]**2 + y1[0,i]**2), np.arctan2(y1[0,i], x1[0,i]))
r1 = np.append(r1,([np.sqrt(x1[0,i]**2 + y1[0,i]**2)]))
t1 = np.append(t1,([-np.arctan2(y1[0,i], x1[0,i])]))
z1 = np.append(z1,([z1[i]]))
# New points
r2, t2 = np.meshgrid(np.linspace(np.min(r1), np.max(r1), num=50),np.linspace(np.min(t1), np.max(t1), num=50))
# Griddata function used to interpolate between scattered data
z2 = griddata(np.concatenate((np.array([t1]), np.array([r1])), axis=0).T, np.array([z1]).T, (t2, r2), method='linear')
# Surface plots
fig, ax = plt.subplots(figsize=(10,10), subplot_kw=dict(projection='polar'))
ax.contourf(t2, r2, np.squeeze(z2), 50, cmap='jet')
for i in range(len(t1)):
plt.text(t1[i], r1[i], "{:.2f}".format(z1[i]), ha="center", va="center", color="k")
plt.show()
然而,生成的极坐标图在中间缺少点。我怎样才能摆脱它们?
【问题讨论】:
-
我遇到了类似的问题:link。不幸的是,@henry 从未回应他是否/如何修复了他的代码。
标签: python numpy interpolation polar-coordinates