首页 > 解决方案 > 切换 3D 绘图的 x 轴和 y 轴的位置

问题描述

我正在使用 Matplotlib 制作 3D 图表,并注意到默认设置是“左侧”是 y 轴,右侧是 x 轴,我想知道是否可以切换它们。这是我制作的代码和图像:

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


fig = plt.figure(figsize=(10, 10))

ax = fig.add_subplot(111, projection='3d')
ax.set_xlim(-3, 5)
ax.set_ylim(-3, 5)
ax.set_zlim(-3, 5)
ax.view_init(elev=20, azim=32)

# u
ax.quiver(0, 0, 0, 2, -1, 0, color='C0', arrow_length_ratio=0.1, label=r'$\vec{u}$')
plt.plot([2, 2], [0, -1], 'k--', linewidth=1)
plt.plot([0, 2], [-1, -1], 'k--', linewidth=1)

# v
ax.quiver(0, 0, 0, -1, 2, -1, color='C1', arrow_length_ratio=0.1, label=r'$\vec{v}$')
ax.plot([0, -1], [2, 2], [0, 0], 'k--', linewidth=1)
ax.plot([-1, -1], [0, 2], [0, 0], 'k--', linewidth=1)
ax.plot([-1, -1], [2, 2], [0, -1], 'k--', linewidth=1)

# w
ax.quiver(0, 0, 0, 0, -1, 2, color='C2', arrow_length_ratio=0.1, label=r'$\vec{w}$')
ax.plot([0, 0], [-1, -1], [0, 2], 'k--', linewidth=1)
ax.plot([0, 0], [-1, 0], [2, 2], 'k--', linewidth=1)

# b
ax.quiver(0, 0, 0, 1, 0, 0, color='C3', arrow_length_ratio=0.2, label=r'$\vec{b}$', linewidth=3)

plt.legend()
ax.set_xlabel(r'$x$', fontsize='large')
ax.set_ylabel(r'$y$', fontsize='large')
ax.set_zlabel(r'$z$', fontsize='large')
ax.quiver(-3, 0, 0, 8, 0, 0, color='k', arrow_length_ratio=0.05) # x-axis
ax.quiver(0, -3, 0, 0, 8, 0, color='k', arrow_length_ratio=0.05) # y-axis
ax.quiver(0, 0, -3, 0, 0, 8, color='k', arrow_length_ratio=0.05) # z-axis

plt.show()

此代码生成以下图像:

在此处输入图像描述

如您所见,y 轴位于左侧,x 位于右侧。有没有办法让我切换这些?

谢谢。

标签: pythonmatplotlib

解决方案


推荐阅读