I am trying to make a 3d surface plot of experimental data using matplotlib. I would like to plot different Z values against the same X and Y axes. When I try the simple code below, I get the error
我正在尝试使用matplotlib制作实验数据的三维表面图。我想在相同的X和Y轴上绘制不同的Z值。当我尝试下面的简单代码时,我得到了错误
"plot_surface() missing 1 required positional argument: 'Z' ".
“plot_surface()缺少1个必需的位置参数:'Z'”。
It seems that the Axes3D package only work if Z is given as a function of X and Y, rather than an actual data matrix. Does anybody know a way around this?
似乎Axes3D软件包仅在Z作为X和Y的函数给出时才起作用,而不是实际的数据矩阵。有人知道解决这个问题吗?
Please note that the Zmatrix that I need is actual data, but I just used a random matrix for illustration here.
请注意,我需要的Zmatrix是实际数据,但我在这里只是使用随机矩阵进行说明。
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
X=[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
Y= [0,2500,5000,7500,10000,15000,20000,25000,30000,35000,40000,45000,50000,55000,60000,65000,70000]
Zmatrix=np.random.rand(len(X),len(Y))
Axes3D.plot_surface(X,Y,Zmatrix)
2 个解决方案
#1
1
There were sone issues with your code: First you have to get a mashgrid
of X and Y (all combinations as matrices). Next swap len(X)
and len(Y)
inside the Zmatrix
. And first define ax = Axes3D(plt.gcf())
and plot_surface
afterwards on ax
.
您的代码存在问题:首先,您必须获得X和Y的mashgrid(所有组合都作为矩阵)。接下来在Zmatrix中交换len(X)和len(Y)。首先在ax上定义ax = Axes3D(plt.gcf())和plot_surface。
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
X=[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
Y= [0,2500,5000,7500,10000,15000,20000,25000,30000,35000,40000,45000,50000,55000,60000,65000,70000]
Xm, Ym = np.meshgrid(X, Y)
Zmatrix=np.random.rand(len(Y),len(X))
ax = Axes3D(plt.gcf())
ax.plot_surface(Xm, Ym, Zmatrix)
#2
0
Here is an example of surface plot.
这是表面图的一个例子。
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import random
def fun(x, y):
return x**2 + y
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = y = np.arange(-3.0, 3.0, 0.05)
X, Y = np.meshgrid(x, y)
zs = np.array([fun(x,y) for x,y in zip(np.ravel(X), np.ravel(Y))])
Z = zs.reshape(X.shape)
ax.plot_surface(X, Y, Z)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
#1
1
There were sone issues with your code: First you have to get a mashgrid
of X and Y (all combinations as matrices). Next swap len(X)
and len(Y)
inside the Zmatrix
. And first define ax = Axes3D(plt.gcf())
and plot_surface
afterwards on ax
.
您的代码存在问题:首先,您必须获得X和Y的mashgrid(所有组合都作为矩阵)。接下来在Zmatrix中交换len(X)和len(Y)。首先在ax上定义ax = Axes3D(plt.gcf())和plot_surface。
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
X=[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
Y= [0,2500,5000,7500,10000,15000,20000,25000,30000,35000,40000,45000,50000,55000,60000,65000,70000]
Xm, Ym = np.meshgrid(X, Y)
Zmatrix=np.random.rand(len(Y),len(X))
ax = Axes3D(plt.gcf())
ax.plot_surface(Xm, Ym, Zmatrix)
#2
0
Here is an example of surface plot.
这是表面图的一个例子。
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import random
def fun(x, y):
return x**2 + y
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = y = np.arange(-3.0, 3.0, 0.05)
X, Y = np.meshgrid(x, y)
zs = np.array([fun(x,y) for x,y in zip(np.ravel(X), np.ravel(Y))])
Z = zs.reshape(X.shape)
ax.plot_surface(X, Y, Z)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()