【发布时间】:2018-05-12 16:25:48
【问题描述】:
我试图找到一种方法来在与 matplotlib 默认情况下不同的位置显示 3D 空间的坐标轴。
为了显示所需的情况,我提供了两个图形,我在其中绘制了立方体的八个角。
图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()
提前感谢您的帮助。
【问题讨论】:
标签: python matplotlib draw