如何根据所述轴上的位置设置轴标签的位置?

2024-04-24 05:07:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把我的轴标签Number of States移到左边,这样它就在我的数字上了。其他类似的问题/答案建议使用labelpad,但这会使文本上下移动,而不是左/右移动。如何将标题移到右侧?你知道吗

我也试过horizontalalignmentkwarg,a.似乎把rightleft对齐颠倒了,也没有把标题移得足够远,也没有提供任何实际的控制权。你知道吗

我看到我可以使用set_[xy]()设置_x_y实例的Text属性,但这似乎有点不太方便。有没有一种方便的方法可以设置标题相对于xaxis上的值的位置?你知道吗

enter image description here


Tags: of答案文本right标题number数字标签
1条回答
网友
1楼 · 发布于 2024-04-24 05:07:54

您可以绘制title对象并将其各自的position属性设置为您喜欢的任何值。 一个简单的例子是:

%matplotlib inline
import matplotlib.pyplot as plt
plt.plot([1,2,3])
ti=plt.title('foo')
ti.set_position((0.2,1))

这样就产生了一个类似
enter image description here
请注意,位置是在相对坐标中设置的。你知道吗

@theppredator建议的position参数(loc:{'center','left','right'},请参见docs)也以类似的方式工作。你知道吗

更新

要设置文本在数据坐标系而不是轴坐标系中的位置,可以使用简单的转换。有关详细信息,请查看Transformation Documentation。最简单的例子如下:

%matplotlib inline
import matplotlib.pyplot as plt
f,ax = plt.subplots(1)
ax.plot([1,2,3])
ti=ax.set_title('foo')
ti.set_position(ax.transLimits.transform((0.5,3)))

这将标题置于(0.5,3)中心,如下图所示
enter image description here

相关问题 更多 >