[Python] limit axis of 3D graph with Matplotlib

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

[Python] limit axis of 3D graph with Matplotlib
[Python] Set the graph range with matplotlib
Draw a flat surface with a matplotlib 3d graph
2-axis plot with Matplotlib
Read Python csv data with Pandas ⇒ Graph with Matplotlib
Increase the font size of the graph with matplotlib
Solve A ~ D of yuki coder 247 with python
Heatmap with Python + matplotlib
Band graph with matplotlib
The basis of graph theory with matplotlib animation
3D plot with matplotlib
Create 3D scatter plot with SciPy + matplotlib (Python)
Installation of matplotlib (Python 3.3.2)
[Python] How to draw a line graph with Matplotlib
[Python] How to create a 2D histogram with Matplotlib
Graph Excel data with matplotlib (1)
Create 3d gif with python3
Graph drawing method with matplotlib
python> Handling of 2D arrays
Graph Excel data with matplotlib (2)
Interactive plot of 3D graph
Study math with Python: Draw a sympy (scipy) graph with matplotlib
Acquisition of 3D point cloud with Softbank Pepper (Choregraphe, Python)
Perform isocurrent analysis of open channels with Python and matplotlib
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
Create plot animation with Python + Matplotlib
A python graphing manual with Matplotlib.
Installation of SciPy and matplotlib (Python)
[Python] font family and font with matplotlib
3D skeleton structure analysis with Python
Solve ABC166 A ~ D with Python
Draw a loose graph with matplotlib
Getting Started with Python Basics of Python
Life game with Python! (Conway's Game of Life)
10 functions of "language with battery" python
Installation of Python, SciPy, matplotlib (Windows)
Heatmap with Dendrogram in Python + matplotlib
Let's make a graph with python! !!
Implementation of Dijkstra's algorithm with python
Draw Lyapunov Fractal with Python, matplotlib
When matplotlib doesn't work with python2.7
Coexistence of Python2 and 3 with CircleCI (1.0)
Lognormal probability plot with Python, matplotlib
Graph Based Segmentation with Python + OpenCV
Basic study of OpenCV with Python
[Python] [3D line graph] Multiple data in one graph, axis values in characters
[Statistics] Grasp the image of the central limit theorem with a graph
Basics of binarized image processing with Python
[Examples of improving Python] Learning Python with Codecademy
[Python] Extension using inheritance of matplotlib (NavigationToolbar2TK)
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
Execute Python script with cron of TS-220
Check the existence of the file with python
Algorithm learned with Python 8th: Evaluation of algorithm
2D FEM stress analysis program with Python
Clogged with python update of GCP console ①
Easy introduction of speech recognition with Python
Example of 3D skeleton analysis by Python
Draw hierarchical axis labels with matplotlib + pandas
[Python] Let's make matplotlib compatible with Japanese
UnicodeEncodeError struggle with standard output of python3