【问题标题】:scipy interpolate 2D data imported from filescipy 插入从文件导入的 2D 数据
【发布时间】:2018-07-20 06:57:11
【问题描述】:

这是我第一次使用 python、scipy 和此任务所需的所有概念。

我使用 Mathematica 计算并在 3D 绘图中显示一个非常复杂的函数。从 Mathematica 导出是一件非常痛苦的事情,并且永远不会在合理的时间范围内提供已发布的数字。 Gnuplot 对 3D 绘图也不是很好,因此我通过使用 matplotlib。通读文档,我看到所有绘图示例都涉及用户定义为向量的简单函数和变量。我的第一个问题如下:

我可以将 Mathematica 中的函数数据导出到一个包含 3 个制表符分隔列的文件中(如果有帮助,分隔可以是别的东西!)。第一列是 x 轴,第二列是 y,第三列是 z 轴。我需要绘制的基本上是z(x,y)

如何导入此类数据文件进行绘图?

第二个问题是:为了获得平滑的曲面图,我是否需要对数据进行某种插值?如果是这种情况,那么从我描述的文件中导入的数据如何处理?

从这个例子here 中,scipy.interpolate.interp2d 函数(?)需要两个一维向量 x 和 y 以及一个二维向量 z,以便获得一个插值函数 z(x,y)。但我的数据是 x(第一列)是一维向量,y(第二列)是一维向量,z(第三列)是一维向量。 给出的示例让我有些困惑,因为它们使用定义的 x 和 y 向量以及 z 的函数,然后他们继续进行插值(我在这里看不到插值点)。

我希望有人会用这个为我指明正确的方向!

【问题讨论】:

  • 使用np.genfromtextPython's csv readerpandas csv reader 加载您的文件。然后使用 scipy.interpolate.griddata 对数据进行上采样。
  • 感谢您的建议。我将尝试使用建议的选项之一进行导入。我仍然对插值感到困惑。所有示例都使用 f(x) 之类的函数来插入其值。我可能不明白这应该如何工作,但这不是我需要的。正如我所说,我的函数的值位于文件的第 3 列,而 x 和 y 是前 2 列。我怎样才能获得这种情况下的插值?我没有可以使用表达式的函数。

标签: python numpy matplotlib scipy


【解决方案1】:

不,您不需要定义函数来使用scipy's griddata。我们可以插入任何给定的数据集,例如,z = x² + y² + 1 删除一些数据点:

0   0   1
1   0   2
3   0   10
0   1   2
1   1   3
2   1   6
0   2   5
1   2   6
2   2   9
3   2   14
0   3   10
2   3   14
3   3   19

以下代码从这些数据点插入一个 3D 表面:

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from scipy.interpolate import griddata

#read data points from text file, could be also a csv reader
arr = np.genfromtxt("test.txt")
#extract x, y, z
x = arr[:,0]
y = arr[:,1]
z = arr[:,2]

#create figure with a 3D projection axis
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")

#number of data points per axis
n = 20
#upsample evenly along x and y axis
x_up = np.linspace(np.min(x), np.max(x), n)
y_up = np.linspace(np.min(y), np.max(y), n)

#create grid from these updsampled 1D arrays
x3d, y3d = np.meshgrid(x_up, y_up)
#interpolate z-values for this regular grid
#x, y, z are your original data points
#methods other than cubic are available
z3d = griddata((x, y), z, (x3d, y3d), method = "cubic")

#plot surface
ax.plot_surface(x3d, y3d, z3d)

#plot original data points on top
ax.scatter(x, y, z, color = "black", marker = "x", s = 50)

ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")

plt.show()

样本输出:

【讨论】:

  • 天哪,这太棒了!我会试试这个!谢谢你为我澄清事情!
  • 我试过你的脚本,但我得到了这些奇怪的错误:\numpy\core_methods.py:29: RuntimeWarning: invalid value遇到reduce return umr_minimum(a, axis, None, out, keepdims) numpy\ core_methods.py:26:RuntimeWarning:reduce return umr_maximum(a,axis,None,out,keepdims)中遇到的无效值 Traceback:文件“plot.py”,第 25 行,在 x3d,y3d = np.meshgrid( x_up, y_up) 文件“numpy\lib\function_base.py”,第 4698 行,在 meshgrid 输出 = [x.copy() for x in output] 文件“numpy\lib\function_base.py”,第 4698 行,在 output = [x.copy() for x in output]
  • 好吧,如果您没有在问题中提供Minimal, Complete, and Verifiable example,就会发生这种情况。步骤 1) 是否将示例数据硬编码到脚本中的复制粘贴脚本 (x = [0, 1, 3, 0,...,2, 3], y = [0, 0, 0, 1 ,..., 3, 3]...) 产生这里显示的输出?第 2 步)如果您注释掉 n = 20 和 ax.plot_surface() 之间的所有内容,包括这些行,您会得到数据的散点图吗?
  • 抱歉,成功了!原来我的输入文件有问题。我修好了它,它的工作原理!现在我必须美化它!感谢您的帮助!
  • 这是最可能的原因,因此,我的问题是区分正确读取文件和库版本不兼容。很高兴我们把它整理出来。祝你的项目好运。
猜你喜欢
  • 1970-01-01
  • 2019-04-02
  • 1970-01-01
  • 2011-07-05
  • 1970-01-01
  • 2016-04-25
  • 2015-06-30
  • 1970-01-01
相关资源
最近更新 更多