【问题标题】:How can I draw coordinate axes at a specific location?如何在特定位置绘制坐标轴?
【发布时间】: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


    【解决方案1】:

    那里似乎至少有几个可能重复的问题,但我不清楚他们是否要求完全相同,而且似乎没有一个答案能解决您的具体问题。

    我能够提出基于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 的密钥。预计这会随时中断。

    【讨论】:

    • 谢谢。这正是我想要表示坐标轴的方式。我试过了,它在我的代码中也能正常工作。轴在预期的方向上,尽管正如您提到的,我确实需要对轴标签位置进行一些操作。因为它不是公共 API 的一部分,所以我将把它放在一个 try-except 块中。我想我以后会安全的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多