简体   繁体   中英

matplotlib 3d: Axis label padding when spine positions are changed

In the code below, the y-axis label is not in the place I want it.

I've seen posts about how to adjust the axis label padding and how to move the axis spines, but not the two together. Below, I change the position of the z and y splines and adjust the padding on the x and y axis labels without any issue. The y-label, however, lands exactly on the spine. Adjusting the padding does not move the label further from the spine, but rather up and down the spine. Am I missing something in my code? Any help appreciated.

from mpl_toolkits.mplot3d import Axes3D


fig = plt.figure(1)
plt.clf()
plt.ion()

ax1 = fig.add_subplot(111, projection='3d')

ax1.yaxis._axinfo['juggled'] = (1,1,1)
ax1.zaxis._axinfo['juggled'] = (1,2,0)

ax1.set_xlabel("X label", labelpad=-10)
ax1.set_ylabel("Y label", labelpad=-20)
ax1.set_zlabel("Z label", labelpad= -8)


ax1.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax1.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax1.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax1.grid(False)


for line in ax1.xaxis.get_ticklines():
    line.set_visible(False)
for line in ax1.yaxis.get_ticklines():
    line.set_visible(False)
for line in ax1.zaxis.get_ticklines():
    line.set_visible(False)
    
ax1.xaxis.set_ticklabels([])
ax1.yaxis.set_ticklabels([])
ax1.zaxis.set_ticklabels([])

在此处输入图像描述

Your label doesn't land on the spine. It's just the angle that makes it look like that. You can try to move your plot and you'll see that by yourself.

The labelpad parameters makes it move further in X and Z , that's why from this perspective it seems to move only down and up the Y spine.

I modified the labelpad to show you from another perspective.

fig = plt.figure(1)
plt.clf()
plt.ion()

ax1 = fig.add_subplot(111, projection='3d')

ax1.yaxis._axinfo['juggled'] = (1, 1, 1)
ax1.zaxis._axinfo['juggled'] = (1, 2, 0)

ax1.set_xlabel("X label", labelpad=10)
ax1.set_ylabel("Y label", labelpad=10)
ax1.set_zlabel("Z label", labelpad=10)

ax1.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax1.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax1.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax1.grid(False)

for line in ax1.xaxis.get_ticklines():
    line.set_visible(False)
for line in ax1.yaxis.get_ticklines():
    line.set_visible(False)
for line in ax1.zaxis.get_ticklines():
    line.set_visible(False)

ax1.xaxis.set_ticklabels([])
ax1.yaxis.set_ticklabels([])
ax1.zaxis.set_ticklabels([])

And this is the figure

另一种观点

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM