Matplotlib fills a 3D closed image

problem description

use matplotlib to draw 3D image curve, but do not know how to fill its closed space with color. Try the fill method in 2D, but it will prompt that it cannot be used in 3D module.

related codes

def draw3d(yy, zz, y_f, z_f, y_l, z_l):
    figure = plt.figure()
    ax_3d = figure.gca(projection="3d")

    
    yy[-1].reverse()
    zz[-1].reverse()
    y_l.reverse()
    z_l.reverse()

    -sharpyzdraw_ydraw_z167float64
    draw_y = y_f + yy[-1] + y_l + yy[0]
    draw_z = z_f + zz[-1] + z_l + zz[0]

    -sharpdraw_x = np.array([[500]*167]*167)
    draw_z = np.array(draw_z)
    draw_y = np.array(draw_y)


    ax_3d.plot(draw_y, draw_z, zs=500, zdir="x") -sharp3
    ax_3d.set(xlabel="x/mm", ylabel="y/mm", zlabel="z/mm")

    plt.show()

    figure.savefig("./x_500_3d.png")

what result do you expect? What is the error message actually seen?

as shown in the figure, I want the middle of the curve to be filled with color. Because yrecoery z is a collection of values, it is not a complete curve, but these points are connected by the plot method, so I do not know how to fill the space inside the blue edge.

Apr.01,2021

ask for answers, newlyweds ask for help


3D graphics seem to have no filling method and need to be converted to 3D Polygon first. Here is a test I wrote based on an answer on SO. Try it for yourself.
Note: this method comes from the original address of SO,: https://stackoverflow.com/que.. If you don't understand, you can check it on the original post.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection

size = 40 
x = [[0] * size] * size
y = list(map(sorted, np.random.rand(size, size)))
z = list(map(sorted, np.random.rand(size, size)))
vect = []
for i in range(size):
    vect.append(list(zip(x[i], y[i], z[i])))
poly3dCollection = Poly3DCollection(vect)

fig = plt.figure()
ax = Axes3D(fig)
ax.add_collection3d(poly3dCollection)
ax.set_xlim([-1, 1])
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")

plt.show()

Menu