plot_trisurf()这个函数的作用是,把一定数量的点(x,y,z),连接成一个平面,但不能垂直地面;
你的程序错误在于,y为常数,垂直地面所以报错,我的思路和你一样,绘制六个平面构成立方体,既然不能垂直地面(x,y不能是常数),那就给它加一个渐进的很小的数,就可以了,程序如下:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.arange(0,1.1,0.1)
y = np.arange(0,1.1,0.1)
xx,yy = np.meshgrid(x, y)
dz = np.arange(0,0.00000121,0.00000001) #xx,yy的数量为121,因此取121个很小的,渐进的数
dz = dz.reshape(xx.shape[0],xx.shape[1]) # 保证xyz三个参数形状一样
z1 = np.zeros((xx.shape[0],xx.shape[1]))
z2 = np.ones((xx.shape[0],xx.shape[1]))
z1 = z1 + dz #这样得到z1全都十分接近于0,但互相不相等
z2 = z2 + dz
ax.plot_trisurf(xx.flatten(), yy.flatten(), z1.flatten())
ax.plot_trisurf(xx.flatten(), yy.flatten(), z2.flatten())
ax.plot_trisurf(xx.flatten(), z1.flatten(), yy.flatten())
ax.plot_trisurf(xx.flatten(), z2.flatten(), yy.flatten())
ax.plot_trisurf(xx.flatten(), yy.flatten(), z1.flatten())
ax.plot_trisurf(xx.flatten(), yy.flatten(), z2.flatten())
ax.plot_trisurf(z1.flatten(), xx.flatten(), yy.flatten())
ax.plot_trisurf(z2.flatten(), xx.flatten(), yy.flatten())
plt.show()
程序运行结果如下:
![](https://img-ask.csdnimg.cn/upload/1615031660482.png?x-oss-process=image/auto-orient,1/resize,w_320,m_lfit)