在 matplotlib 3d 图中移动脊椎? [英] Move spines in matplotlib 3d plot?

查看:34
本文介绍了在 matplotlib 3d 图中移动脊椎?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试移动 3D matplotlib 轴对象中的脊椎.

这似乎是一个非常简单的问题,但我还没有找到任何直接解决这个问题的问题/答案.我在这个问题的底部列出了我对这个主题的研究.

我可以在 matplotlib 2D 图中设置刺的位置.代码如下:

导入 matplotlib.pyplot 为 plt,numpy 为 np图,轴 = plt.subplots(1, 2)r, theta = 1, np.linspace(0, 2*np.pi, 100)x, y = r*np.cos(theta), r*np.sin(theta)for ax in axes: # 在两个轴上绘制相同的数据ax.plot(x, y)ax.set_aspect("相等")对于 ax.spines.values() 中的脊椎:# 调整最后一个活动轴上的脊椎Spine.set_position(("数据", 0))

产生:

然而,当我用 3D 轴尝试同样的事情时......

z = np.zeros(x.shape) # 令人兴奋的东西fig = plt.figure()for i in range(2): # 创建两个 3D 子图ax = plt.subplot(1,2,i+1,projection="3d",aspect="equal")plt.plot(x, y, z)对于 ax.spines.values() 中的脊椎:# 调整最后一个活动轴上的脊椎Spine.set_position(("数据", 0))

上面的代码给了我:

即没有效果,即使代码仍在运行.此外,对于 3D 轴,ax.spines 看起来像:

OrderedDict([('left', ),('right', ),('bottom', ),('top', )])

我不确定在 3D 轴的上下文中左"、右"、底部"、顶部"指的是什么.我试过改变其他属性,比如刺的颜色;那里也没有运气.如何获得轴上实际的 x、y、z 脊椎?

研究:

  • 在撰写本文时,在 stackoverflow 中搜索matplotlib spins 3d"会得到 5 个结果(包括这个问题).
  • 默认设置,ax.xaxis._axinfo['juggled'] = (1,0,2)新设置,ax.xaxis._axinfo['juggled'] = (2,0,1)所有六个外边界的参数是,

    I'm trying to move the spines in a 3D matplotlib axes object.

    This seems like a really simple issue, but I have not found any questions/answers that address this directly. I've included a list of my research on this topic at the bottom of this question.

    I can set the position of the spines in matplotlib 2D plots. The following code:

    import matplotlib.pyplot as plt, numpy as np
    
    fig, axes = plt.subplots(1, 2)
    r, theta = 1, np.linspace(0, 2*np.pi, 100)
    x, y = r*np.cos(theta), r*np.sin(theta)
    for ax in axes:  # plot the same data on both axes
        ax.plot(x, y)
        ax.set_aspect("equal")
    for spine in ax.spines.values():  # adjust spines on last active axis 
        spine.set_position(("data", 0))
    

    produces:

    However, when I try the same thing with a 3D axis...

    z = np.zeros(x.shape)  # exciting stuff
    fig = plt.figure()
    for i in range(2):  # create two 3D subplots
        ax = plt.subplot(1,2,i+1, projection="3d", aspect="equal")
        plt.plot(x, y, z)
    for spine in ax.spines.values():  # adjust spines on last active axis
        spine.set_position(("data", 0))
    

    the above code gives me:

    I.e. no effect, even though the code still runs. Also, for the 3D axes, ax.spines looks like:

    OrderedDict([('left', <matplotlib.spines.Spine at 0x120857b8>),
                 ('right', <matplotlib.spines.Spine at 0xfd648d0>),
                 ('bottom', <matplotlib.spines.Spine at 0xe89e4e0>),
                 ('top', <matplotlib.spines.Spine at 0xe89eef0>)])
    

    I'm not sure what "left", "right", "bottom", "top" refer to in the context of a 3D axis. I've tried changing other properties like colour of the spines; no luck there either. How can I get hold of the actual x, y, z spines on the axes?

    Research:

    • searching "matplotlib spines 3d" in stackoverflow gives 5 results (including this question) at the time of writing.
    • The mplot3d documentation doesn't mention spines at all.
    • This question shows how to set the pane colour with ax.w_xaxis.set_pane_color(), but there is no similar ax.w_zaxis.set_spine... method.
    • This question shows how to set the spine colour using ax.w_zaxis.line.set_color(). I thought about making a horrible workaround to set ax.w_zaxis.line.set_data manually, but it only has x and y data; no z! Even the x and y axes don't have z data.

    解决方案

    There seems to be no obvious way to do this at the moment. Setting the spines when the axis projection is 3D is not implemented. However, there is a small hack here.

    The ax.spines setting is for 2D rendering. When you set projection=3d in the initialization of the figure, certain 2D properties (like ax.spines, etc.) are ignored. It's why you don't get any response when you set the 2D spines.

    The 3D figure axis line (the thick black line for each axis) locations are determined by the parameter ax.xaxis._axinfo['juggled'] (and similarly for y and z axes). This specifies which of the six outer boundaries of a 3D plot bounding box are plotted as thick black lines.

    You can shift the position of the axis line for each of x,y,z axis by overwriting the juggled value, which specifies which axis lines are the main ones, as the following example for the x axis, the default setting, ax.xaxis._axinfo['juggled'] = (1,0,2) new setting, ax.xaxis._axinfo['juggled'] = (2,0,1) The parameters for all the six outer boundaries are,

    这篇关于在 matplotlib 3d 图中移动脊椎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆