This code snippet provides a Python function that replicates the functionality of the given MATLAB function for plotting a 3D figure. It utilizes the matplotlib library, specifically the Axes3D object from mpl_toolkits.mplot3d to create a 3D plotting environment. pythonimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3D

def plotFigure(startPos, goalPos, X, Y, Z, GlobalBest): # 画起点和终点 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(startPos[0], startPos[1], startPos[2], c='y', marker='s', s=100) ax.scatter(goalPos[0], goalPos[1], goalPos[2], c='y', marker='p', s=100)

# 画山峰曲面    ax.plot_surface(X, Y, Z, cmap='viridis', edgecolor='none')    ax.grid(False)

# 画路径    path = GlobalBest['path']    pos = GlobalBest['pos']    ax.scatter(pos['x'], pos['y'], pos['z'], c='g')    ax.plot(path[:, 0], path[:, 1], path[:, 2], 'r', linewidth=2)

plt.show()

The Python code uses similar functionalities as the MATLAB code to achieve the same visual outcome:

  • Plotting start and goal points: ax.scatter() is used to plot the start and goal positions with specified colors and markers.- Plotting the surface: ax.plot_surface() draws the surface with the viridis colormap and no edge lines.- Plotting the path: ax.scatter() and ax.plot() are used to visualize the path with green markers and a red line.

This Python version simplifies the plotting process while maintaining the essential visual elements from the original MATLAB code.

MATLAB to Python: 3D Plot Conversion with Matplotlib

原文地址: https://www.cveoy.top/t/topic/i5X 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录