matplotlib 在3D打印中,Z标签方向随着字符数的增加而反转

1sbrub3j  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(135)

我在使用matploblib的3D绘图时遇到了一个非常奇怪的问题,如果我使z轴标签的长度超过4个字符,它将反转标签的方向。我试过使用标签填充和字体大小,但似乎找不到它这样做的原因。下面的代码将使z标签正确向上:

fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
ax.patch.set_facecolor('black')
plt.setp([ax.get_xticklines(), ax.get_yticklines(), ax.get_xticklabels(), ax.get_yticklabels(),
          ax.get_zticklabels(),ax.spines.values(), ax.set_xlabel('east',fontsize=14,labelpad=8),
          ax.set_ylabel('north',fontsize=14, labelpad=5),ax.set_zlabel('al m', labelpad =0)],
         color='#03fc14')
axes = fig.gca(projection='3d')
zLabel = axes.set_zlabel('al m',fontsize=14, color='#03fc14',rotation='vertical', labelpad =-5 )
ax.scatter(ku.east, ku.north, ku.alt, color = 'blue', alpha = 0.3,label = 'ku')
ax.scatter(*list(zip(*atm.coords[slice(0,len(atm.coords),50)])), atm.alt[slice(0,len(atm.coords),50)],
           color = 'orange', alpha=0.3, label = 'atm')
plt.legend(loc='best')
ax.view_init(26, 40)
fig.tight_layout()

而此代码(除了z标签之外不改变任何其他内容)反转了z标签的方向:

fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
ax.patch.set_facecolor('black')
plt.setp([ax.get_xticklines(), ax.get_yticklines(), ax.get_xticklabels(), ax.get_yticklabels(),
          ax.get_zticklabels(),ax.spines.values(), ax.set_xlabel('east',fontsize=14,labelpad=8),
          ax.set_ylabel('north',fontsize=14, labelpad=5),ax.set_zlabel('alt m', labelpad =0)],
         color='#03fc14')
axes = fig.gca(projection='3d')
zLabel = axes.set_zlabel('alt /m',fontsize=14, color='#03fc14',rotation='vertical', labelpad =-5 )
ax.scatter(ku.east, ku.north, ku.alt, color = 'blue', alpha = 0.3,label = 'ku')
ax.scatter(*list(zip(*atm.coords[slice(0,len(atm.coords),50)])), atm.alt[slice(0,len(atm.coords),50)],
           color = 'orange', alpha=0.3, label = 'atm')
plt.legend(loc='best')
ax.view_init(26, 40)
fig.tight_layout()

以下是图中图像的链接:
Using 'al m' as the z-axis label
Using 'alt m' as the z-axis label
我使用matplotlib版本3.1.1和python版本3.7.4。
任何解释或建议将不胜感激。

2guxujil

2guxujil1#

我设法克服了以下问题:

axes = fig.gca(projection='3d')
ax.zaxis.set_rotate_label(False)  # disable automatic rotation (so that I can actually subsequently change it)
zLabel = axes.set_zlabel('alt /m',fontsize=14, color='#03fc14',rotation=90, labelpad =0 )

相关问题