gpt4 book ai didi

python - 在图形而不是轴的坐标系中设置轴标签

转载 作者:太空狗 更新时间:2023-10-30 01:21:25 25 4
gpt4 key购买 nike

我想使用图形的坐标系而不是轴来设置轴标签的坐标(或者如果这不可能至少是一些绝对坐标系)。

换句话说,对于这两个示例,我希望标签位于同一位置:

import matplotlib.pyplot as plt
from pylab import axes

plt.figure().show()
ax = axes([.2, .1, .7, .8])
ax.plot([1, 2], [1, 2])
ax.set_ylabel('BlaBla')
ax.yaxis.set_label_coords(-.1, .5)
plt.draw()

plt.figure().show()
ax = axes([.2, .1, .4, .8])
ax.plot([1, 2], [1, 2])
ax.set_ylabel('BlaBla')
ax.yaxis.set_label_coords(-.1, .5)

plt.draw()
plt.show()

这在 matplotlib 中可行吗?

Illustrate difference

最佳答案

是的。您可以使用转换从一个坐标系转换到另一个坐标系。这里有一个深入的解释:http://matplotlib.org/users/transforms_tutorial.html

如果要使用图形坐标,首先需要将图形坐标转换为显示坐标。您可以使用 fig.transFigure 执行此操作。稍后,当您准备好绘制轴时,您可以使用 ax.transAxes.inverted() 从显示转换为轴。

import matplotlib.pyplot as plt
from pylab import axes

fig = plt.figure()
coords = fig.transFigure.transform((.1, .5))
ax = axes([.2, .1, .7, .8])
ax.plot([1, 2], [1, 2])
axcoords = ax.transAxes.inverted().transform(coords)
ax.set_ylabel('BlaBla')
ax.yaxis.set_label_coords(*axcoords)
plt.draw()

plt.figure().show()
coords = fig.transFigure.transform((.1, .5))
ax = axes([.2, .1, .4, .8])
ax.plot([1, 2], [1, 2])
ax.set_ylabel('BlaBla')
axcoords = ax.transAxes.inverted().transform(coords)
ax.yaxis.set_label_coords(*axcoords)

plt.draw()
plt.show()

关于python - 在图形而不是轴的坐标系中设置轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31346014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com