我仍在努力寻找解决我的问题的方法:matplotlib: assign color to a radius . 我首先使用 contourplot 进行了尝试,但我认为当我尝试在磁盘表面绘制圆圈并为其分配颜色时,这是一个更好的解决方案。 因此我有一个数组:
arr = np.array([[ 114.28, 14],
[ 128.57, 16],
[ 142.85,19],
[ 157.13,20],
[ 171.41,21],
[ 185.69,22],
[ 199.97,24],
[ 214.25,27],
[ 228.53,29],
[ 242.81,30],
[ 257.09,31],
[ 271.37,34],
[ 288.65,35],
[ 299.93,36],
[ 300,38]])
我想将这个数组扩展到一个包含大约 300 个元素的数组(取决于磁盘的半径。如果半径是例如 500,我想要一个包含 500 个元素的数组)通过在值之间进行插值阵列线性。我需要对两列进行插值。我找到了这篇文章:Interpolate between elements in an array of floats但我不明白代码,因为他们在谈论毫秒和 LED... 提前致谢 !
最佳答案
IIUC,我认为你可以使用 np.interp插入你的点。根据文档,np.interp
用于:
One-dimensional linear interpolation.
这听起来正是您所追求的。
因此,您可以使用 np.linspace
从最小 x 值到最大 x 值创建一个由 300 个均匀分布的点组成的数组:
new_x = np.linspace(min(arr[:,0]), max(arr[:,0]), num=300)
然后插入新的 y
值:
new_y = np.interp(new_x, arr[:,0], arr[:,1])
以图形方式说明:
# Show the original 15 points:
plt.scatter(arr[:,0], arr[:,1], label='original', zorder=10)
# Show the interpolated 300 points:
plt.scatter(new_x, new_y, label='interpolated', s=0.5)
plt.legend()
plt.show()
编辑 根据您的评论,要在数组中的每个数据点之间恰好插入 20 个点,您可以通过遍历数组并应用 linspace
创建新的 x 轴在每个连续的 x 值之间得到 20 分。但是,这将产生 280 个点,因为您将在 15 个数据点之间创建 20 个点,从而导致 20*(15-1)
新数据点:
new_x = np.concatenate([np.linspace(arr[i,0],arr[i+1,0], num=20)
for i in range(len(arr)-1)])
new_y = np.interp(new_x, arr[:,0], arr[:,1])
# Show the original 15 points:
plt.scatter(arr[:,0], arr[:,1], label='original', zorder=10)
# Show the interpolated 280 points:
plt.scatter(new_x, new_y, label='interpolated', s=0.5)
plt.legend()
plt.show()
关于arrays - 在数组中的元素之间进行插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51714504/