本文介绍了在 matplotlib 的 Axes3D 中调整标签位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在轴标签与 matplotlib 中的刻度标签重叠时遇到问题.我尝试通过应用转换或调用 set_y() 来手动"重新定位标签,但无济于事.

I am having trouble with axes labels overlapping ticks labels in matplotlib. I've tried to reposition the labels "manually" by applying transforms or by calling set_y(), but no avail.

这是重现问题的片段:

import matplotlib
matplotlib.use("TKAGG")
import matplotlib.pyplot as pyplot
import mpl_toolkits.mplot3d

figure = pyplot.figure()
figure.subplots_adjust(bottom=0.25, top=0.75)
axes = figure.gca(projection='3d')
xLabel = axes.set_xlabel('XXX xxxxxx xxxx x xx x')
yLabel = axes.set_ylabel('YY (y) yyyyyy')
zLabel = axes.set_zlabel('Z zzzz zzz (z)')
plot = axes.plot([1,2,3],[1,2,3])

pyplot.show()

注意 x 和 y 标签如何与刻度冲突.我可以优雅地解决这个问题吗?

Note how the x and y labels clash with the ticks. Can I solve this elegantly ?

推荐答案

我和您一样感到沮丧.我为此工作了半个小时,但一无所获.文档说 set_xlabel 需要一个参数 labelpad 但我得到一个错误(AttributeError: Unknown property labelpad)!在 xaxis 或 w_xaxis 上,事后设置它不会做任何事情.

I share your frustration. I worked on it for a good half hour and got nowhere. The docs say set_xlabel takes an arg labelpad but I get an error (AttributeError: Unknown property labelpad)! Setting it after the fact doesn't do anything, on xaxis or w_xaxis.

这是一个粗略的解决方法:

Here's a crude workaround:

import matplotlib
matplotlib.use("TKAGG")
import matplotlib.pyplot as pyplot
import mpl_toolkits.mplot3d

figure = pyplot.figure(figsize=(8,4), facecolor='w')
ax = figure.gca(projection='3d')

xLabel = ax.set_xlabel('
XXX xxxxxx xxxx x xx x', linespacing=3.2)
yLabel = ax.set_ylabel('
YY (y) yyyyyy', linespacing=3.1)
zLabel = ax.set_zlabel('
Z zzzz zzz (z)', linespacing=3.4)
plot = ax.plot([1,2,3],[1,2,3])
ax.dist = 10

pyplot.show()

这篇关于在 matplotlib 的 Axes3D 中调整标签位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:21