When creating a 3D graph with Matplotlib, I want to set the size of the axes of the graph and unify the ratio of the axes, but a function like ``` plt.axes (). Set_aspect ('equal')` `` is a 3D plot. Does not exist in, so how to solve it. (Useful for orbital plots, etc.)
For example, about this plot
# plot in 3D of candidate branches
fig = plt.figure(figsize=(12,10))
plt.rcParams["font.size"] = 20
ax = fig.gca(projection='3d')
# mass
ax.plot_wireframe(xperilune_m2, yperilune_m2, zperilune_m2, color="k", label='m2', alpha=0.1)
# LPO trajectory
ax.plot(dynmatLPO["x_arr"]*Lstar, dynmatLPO["y_arr"]*Lstar, dynmatLPO["z_arr"]*Lstar, c='m', linewidth='1.5', label='Halo')
Suppose you want to solidify the size of the shaft. If you want to stop the $ x $ axis from $ 320000 $ to $ 470000 $, the $ y $ axis from $ -60000 $ to $ 60000 $, and the $ z $ axis from $ -1000 $ to $ 1000 $, have the largest range of these. The range is calculated as `` `max = range``` and the box is created based on this.
# set array for max/min boxing
X = np.array([320000, 470000])
Y = np.array( [-60000, 60000])
Z = np.array([-1000, 1000])
# compute max required range
max_range = np.array([X.max()-X.min(), Y.max()-Y.min(), Z.max()-Z.min()]).max() / 2.0
# setup axis
mid_x = (X.max()+X.min()) * 0.5
mid_y = (Y.max()+Y.min()) * 0.5
mid_z = (Z.max()+Z.min()) * 0.5
ax.set_xlim(mid_x - max_range, mid_x + max_range)
ax.set_ylim(mid_y - max_range, mid_y + max_range)
ax.set_zlim(mid_z - max_range, mid_z + max_range)
ax.grid()
plt.title(f'Targeting from {num_branch} manifolds')
ax.legend(loc='best')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
reference: https://matplotlib.org/3.2.1/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.html https://stackoverflow.com/questions/30196503/2d-plots-are-not-sitting-flush-against-3d-axis-walls-in-python-mplot3d/41779162#41779162
Recommended Posts