![](/img/trans.png)
[英]How to get y-axis inverted to share a common original point with x-axis for 3D plot in matplotlib?
[英]Plotting curves on 3d plot with common x-axis
我想在 3d 中 plot 兩條曲線,以便兩條曲線共享 x 軸。 一條曲線必須位於 XZ 平面上,而第二條曲線必須位於 XY 平面上。
我嘗試使用以下代碼完成相同的操作。
import numpy as np
import matplotlib.pyplot as plt
ax = plt.figure().add_subplot(projection='3d')
x = np.linspace(0, 1, 100)
y = abs(np.sin(x*2*np.pi))
ax.plot(x, y, 'b', zs=0, zdir='x', label='curve in (x, z)')
ax.plot(x, y, 'r', zs=0, zdir='z', label='curve in (x, y)')
# Make legend, set axes limits and labels
ax.legend()
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 1)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.view_init(elev=20., azim=-35)
plt.show()
我已經在此圖的左側面板中展示了我使用此代碼完成的操作。 但是,我想得到如圖右面板所示的東西。
任何幫助將不勝感激!
你可以像這樣簡單地做到這一點:
x = np.linspace(0, 1, 100)
y = np.abs(np.sin(x*2*np.pi))
zero = np.zeros_like(x)
ax.plot(zero, x, y, 'b', label='curve in (x, z)')
ax.plot(y, x, zero , 'r', label='curve in (x, y)')
你幾乎說對了。 您只需要翻轉 xy 軸。 以下代碼將為您完成。
import numpy as np
import matplotlib.pyplot as plt
ax = plt.figure().add_subplot(projection='3d')
x = np.linspace(0, 1, 100)
y = abs(np.sin(x*2*np.pi))
ax.plot(x, y, 'b', zs=0, zdir='x', label='curve in (x, z)')
ax.plot(y, x, 'r', zs=0, zdir='z', label='curve in (x, y)')
ax.legend()
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 1)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.view_init(elev=20., azim=-35)
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.