Iva*_*van 5 python surface mplot3d matplotlib-3d
我正在使用 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()
Run Code Online (Sandbox Code Playgroud)
我尝试过使用xaxis.set_ticks_position('left')声明,但它不起作用。
没有记录的方法,但使用/sf/answers/1053405741/中的一些黑客想法就可以。
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
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()
Run Code Online (Sandbox Code Playgroud)
我不知道三元组中第三个数字的含义。如果设置为零,则图中没有任何变化。因此应该查看代码以进行进一步调整。
您还可以查看相关问题Changingposition of Vertical (z) axis of 3Dplot with low level hacking of _PLANES property。