【问题标题】:Changing position of axes in Axes3D在 Axes3D 中更改轴的位置
【发布时间】:2018-05-23 16:13:41
【问题描述】:

我正在使用 mpl_toolkits 库中的 mplot3d。当在图上显示 3D 表面时,我意识到轴的位置没有像我希望的那样。

让我展示一下,我在下面的截图中添加了每个轴的位置:

有没有办法改变轴的位置以获得这个结果:

这是工作代码:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

ax = Axes3D(plt.figure())
def f(x,y) :
    return -x**2 - y**2

X = np.arange(-1, 1, 0.02)
Y = np.arange(-1, 1, 0.02)
X, Y = np.meshgrid(X, Y)
Z = f(X, Y)
ax.plot_surface(X, Y, Z, alpha=0.5)

# Hide axes ticks
ax.set_xticks([-1,1])
ax.set_yticks([-1,1])
ax.set_zticks([-2,0])

ax.set_yticklabels([-1,1],rotation=-15, va='center', ha='right')

plt.show()

我尝试过使用xaxis.set_ticks_position('left') 语句,但它不起作用。

【问题讨论】:

    标签: python mplot3d


    【解决方案1】:

    没有记录的方法,但你可以从https://stackoverflow.com/a/15048653/1149007 获得一些黑客思想。

    import matplotlib.pyplot as plt
    import numpy as np
    from mpl_toolkits.mplot3d import Axes3D
    
    fig = plt.figure()
    ax = ax = fig.add_subplot(111, projection='3d')
    ax.view_init(30, 30)
    
    
    def f(x,y) :
        return -x**2 - y**2
    
    X = np.arange(-1, 1, 0.02)
    Y = np.arange(-1, 1, 0.02)
    X, Y = np.meshgrid(X, Y)
    Z = f(X, Y)
    ax.plot_surface(X, Y, Z, alpha=0.5)
    
    # Hide axes ticks
    ax.set_xticks([-1,1])
    ax.set_yticks([-1,1])
    ax.set_zticks([-2,0])
    
    ax.xaxis._axinfo['juggled'] = (0,0,0)
    ax.yaxis._axinfo['juggled'] = (1,1,1)
    ax.zaxis._axinfo['juggled'] = (2,2,2)
    
    plt.show()
    

    我不知道三元组中第三个数字的含义。如果设置为零,则图中没有任何变化。所以应该查看代码以进行进一步的调整。

    您还可以查看相关问题 Changing position of vertical (z) axis of 3D plot (Matplotlib)? 与 _PLANES 属性的低级黑客攻击。

    【讨论】:

    • 对于zaxis,元组的第一个数字会改变轴的放置位置(右、左、中)。
    猜你喜欢
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多