我是Python的新手,所以请耐心等待。我感谢任何帮助!
我有什么:三个1D列表( xr,yr,zr ),一个包含x值,另外两个y值和z值
我想做什么:在matplotlib中创建一个三维等高线图
我意识到我需要使用 meshgrid 函数将三个1D列表转换为三个2D列表。
这是我到目前为止所拥有的:
class SecondJob < SidekiqJob
def perform(params)
data = SomeData.find
return unless data.ready?
# do whatever you need to do with the ready data
end
end
运行它会给我以下错误(对于我在上面输入的最后一行):
Snackbar snack = Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null);
ViewGroup group = (ViewGroup) snack.getView();
group.setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.red));
snack.show();
我开始挖掘stackoverflow,并尝试使用有类似问题的人的建议。以下是我从每个建议中得到的错误:
将最后一行更改为:
xr = np.asarray(xr)
yr = np.asarray(yr)
zr = np.asarray(zr)
X, Y = np.meshgrid(xr,yr)
znew = np.array([zr for x,y in zip(np.ravel(X), np.ravel(Y))])
Z = znew.reshape(X.shape)
给出了同样的错误。
将最后一行更改为:
total size of new array must be unchanged
给出错误:
Z = znew.reshape(X.shape[0])
将其更改为:
Z = znew.reshape(X.shape[0], len(znew))
给出错误:
Shape of x does not match that of z: found (294, 294) instead of (294, 86436).
有什么想法吗?
答案 0 :(得分:1)
嗯,下面的示例代码适用于我
import numpy as np
import matplotlib.pyplot as plt
xr = np.linspace(-20, 20, 100)
yr = np.linspace(-25, 25, 110)
X, Y = np.meshgrid(xr, yr)
#Z = 4*X**2 + Y**2
zr = []
for i in range(0, 110):
y = -25.0 + (50./110.)*float(i)
for k in range(0, 100):
x = -20.0 + (40./100.)*float(k)
v = 4.0*x*x + y*y
zr.append(v)
Z = np.reshape(zr, X.shape)
print(X.shape)
print(Y.shape)
print(Z.shape)
plt.contour(X, Y, Z)
plt.show()