切换3D图的x和y轴的位置

时间:2019-02-22 05:35:24

标签: python matplotlib

我正在使用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()

此代码产生以下图像:

enter image description here

如您所见,y轴在左侧,x在右侧。我有办法切换这些吗?

谢谢。

0 个答案:

没有答案