matplotlib:如何在视口中调整绘图?

matplotlib: How to fit plot in viewport?

我正在尝试使用 matplotlibmplot3d 绘制模拟离子运动通过孔隙的 3D 轨迹,但我在将图形拟合到生成的视口时遇到问题。

我最初的问题是轴没有正确缩放,尽管 z 轴的长度是 x 轴和 y 轴的三倍,但它占用的 space 量与 x 轴和 y 轴相同(抱歉link,我还没有嵌入的声誉):poor scaling.

我使用来自 question, however, this leads to another problem -- the graph no longer fits in the viewport: ill-fitting graph 的 Andrzej Pronobis 的回答解决了这个问题。

增加图形大小没有帮助,放大和缩小只是改变轴,而不是图形本身的大小。有谁知道如何 "zoom out" 视口本身或缩小绘图以使其适合 window?下面是我的缩写代码。感谢您的帮助。

# Imports
# (various other stuff...)
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# (load data and extract xyz coordinates)

# Make axes
fig = plt.figure(figsize = (10, 10))
ax = fig.add_subplot(111, projection = "3d")

# Set properties
ax.set_xlim3d([-20.0, 20.0])
ax.set_xlabel("X")
ax.set_ylim3d([-20.0, 20.0])
ax.set_ylabel("Y")
ax.set_zlim3d([-60.0, 60.0])
ax.set_zlabel("Z")

# Properly scale the axes
ax.get_proj = lambda: np.dot(Axes3D.get_proj(ax), np.diag([1, 1, 3, 1]))

# Plot data
ax.plot(xs = x, ys = y, zs = z)

# Display results
plt.show()

您可以使用例如[1, 1, 3, 1.75]

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

fig = plt.figure(figsize = (8,8))
ax = fig.add_subplot(111, projection = "3d")

ax.set_xlim3d([-20.0, 20.0])
ax.set_xlabel("X")
ax.set_ylim3d([-20.0, 20.0])
ax.set_ylabel("Y")
ax.set_zlim3d([-60.0, 60.0])
ax.set_zlabel("Z")

# Properly scale the axes
ax.get_proj = lambda: np.dot(Axes3D.get_proj(ax), np.diag([1, 1, 3, 1.75]))

plt.show()