matplotlib 3d 曲面图不显示
matplotlib 3d surface plots not showing
我正在尝试使用 matplotlib 制作一个简单的 3D 表面图,但该图最后没有显示;我只得到空的 3D 轴。
这是我所做的:
from mpl_toolkits.mplot3d import Axes3D
x = np.arange(1, 100, 1)
y = np.arange(1, 100, 1)
z = np.arange(1, 100, 1)
fig = figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, rstride=5, cstride=5)
show()
...我明白了:
有什么建议吗?
您没有绘制曲面:x
、y
和 z
需要是二维数组。看这个例子:http://matplotlib.org/examples/mplot3d/surface3d_demo.html.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d, Axes3D
from scipy.interpolate import griddata
x= np.arange(1, 100, 1)
y= np.arange(1, 100, 1)
z= np.arange(1, 100, 1)
fig = plt.figure()
ax = fig.gca(projection='3d')
xi = np.linspace(x.min(), x.max(), 50)
yi = np.linspace(y.min(), y.max(), 50)
zi = griddata((x, y), z, (xi[None, :], yi[:, None]), method='nearest') # create a uniform spaced grid
xig, yig = np.meshgrid(xi, yi)
surf = ax.plot_wireframe(X=xig, Y=yig, Z=zi, rstride=5, cstride=3, linewidth=1) # 3d plot
plt.show()
我正在尝试使用 matplotlib 制作一个简单的 3D 表面图,但该图最后没有显示;我只得到空的 3D 轴。
这是我所做的:
from mpl_toolkits.mplot3d import Axes3D
x = np.arange(1, 100, 1)
y = np.arange(1, 100, 1)
z = np.arange(1, 100, 1)
fig = figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, rstride=5, cstride=5)
show()
...我明白了:
有什么建议吗?
您没有绘制曲面:x
、y
和 z
需要是二维数组。看这个例子:http://matplotlib.org/examples/mplot3d/surface3d_demo.html.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d, Axes3D
from scipy.interpolate import griddata
x= np.arange(1, 100, 1)
y= np.arange(1, 100, 1)
z= np.arange(1, 100, 1)
fig = plt.figure()
ax = fig.gca(projection='3d')
xi = np.linspace(x.min(), x.max(), 50)
yi = np.linspace(y.min(), y.max(), 50)
zi = griddata((x, y), z, (xi[None, :], yi[:, None]), method='nearest') # create a uniform spaced grid
xig, yig = np.meshgrid(xi, yi)
surf = ax.plot_wireframe(X=xig, Y=yig, Z=zi, rstride=5, cstride=3, linewidth=1) # 3d plot
plt.show()