我试图找到一种方法来显示3D空间的坐标轴,其位置与默认情况下matplotlib不同。
为了显示所需的情况,我提供了两个数字,其中我画了一个立方体的八个角。
图1:matplotlib-绘制坐标轴的默认值
图1是matplotlib坐标轴的默认表示。坐标轴“在空格的末端绘制。
图2:绘制坐标轴时所需的位置
图2中的演示显示了源自一个公共点的坐标轴。这是期望的演示文稿。
现在的问题是:“我怎样才能达到理想的目标?”
我环顾四周,无法找到这个问题的答案。我认为它可能是一个简单的设置,不是那么容易找到,因此我也提供了生成“matplotlib默认演示文稿”的代码(见下文)。
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def plot_3Dcube():
"""Plots a 3D cube in a 3D-window
"""
#--------------------------------------
# PROCESS ARGUMENTS AND LOCAL VARIABLES
#--------------------------------------
mycube = cube() # The cube to be plotted
#---------------
# Setup the plot
#---------------
fig = plt.figure()
ax = fig.add_subplot(111,projection="3d")
#---------------------------------------------
# Format the point-data and add it to the plot
#---------------------------------------------
colour = (1.0, 0, 0, 1) # RGB, alpha
mrkr = '^'
s = 50
print (mycube)
ax.scatter(mycube[:,0],mycube[:,1],mycube[:,2],
c = colour,
marker = mrkr
)
ptnr=0
for row in mycube:
ptnr += 1
ax.text(row[0],row[1],row[2],str(ptnr),
size = 8,
color = (0,0,0)
)
#----------------
# Format the plot
#----------------
ax.set_xlabel('X as')
ax.set_ylabel('Y as')
ax.set_zlabel('Z as')
#--------------
# SHOW THE PLOT
#--------------
ax.view_init(15,30)
plt.show()
#=-=-=-=-=-=-=-=-=-=-=-
# RETURN FROM FUNCTION
#=-=-=-=-=-=-=-=-=-=-=-
return None
#</plot_3dCube()>
def cube():
"""Returns the eight points of a cube in a numpy-array.
"""
c = np.array([[5.0, 0.0, 5.0], # Ptnr 1, Front UpperLeft
[5.0, 5.0, 5.0], # Ptnr 2, Front Upper Right
[5.0, 5.0, 0.0], # Ptnr 3, Front Lower Right
[5.0, 0.0, 0.0], # Ptnr 4, Front Lower Left
[0.0, 0.0, 5.0], # Ptnr 5, Back, Upper Right
[0.0, 5.0, 5.0], # Ptnr 6, Back, Upper Left
[0.0, 5.0, 0.0], # Ptnr 7, Back, Lower Left
[0.0, 0.0, 0.0], # Ptnr 8, Back, Lower Right
])
return c
#</cube()>
#==================================================================
# TEST area
#==================================================================
def main():
print ("Entering Main()\n")
plot_3Dcube()
#</main()>
if __name__ == "__main__":
main()
提前感谢您的帮助。
答案 0 :(得分:0)
似乎有几个可能重复的东西,但我不清楚他们是否要求完全一样,没有一个答案似乎能解决你的具体问题。
我能够提出基于another answer的解决方案。公平地说,我并不真正理解这些数字的实际含义,我(或多或少)通过反复试验得到它。
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.xaxis._axinfo['juggled'] = 0, 0, 0
ax.yaxis._axinfo['juggled'] = 1, 1, 1
ax.zaxis._axinfo['juggled'] = 2, 2, 2
如果您在某种互动环境中这样做,请致电plt.draw()
查看更改。
您仍有一些问题需要解决。 Y轴的方向可能不是您想要的,标签的可读性不太理想。希望你能找到解决这些问题的方法。
请注意,您使用的属性并非旨在成为公共API的一部分,并设置名为juggled
的密钥。期待随时突破。