是否可以将 matplotlib 注释锚定到 x 轴中的数据坐标,但锚定到 y 轴中的相对位置?
Is it possible to anchor a matplotlib annotation to a data coordinate in the x-axis, but to a relative location in the y-axis?
我有一个绘图,我想在其中用箭头和标签注释 x 轴上的特定位置:
- 需要在数据坐标中准确指定箭头尖端的位置。
- 箭头应该是垂直的,因此箭头钝端(和文本标签)的 x 坐标也应该在数据坐标中准确指定。
- 但是,理想情况下,我希望能够指定箭头钝端相对于轴边界框的 y 位置,而不是数据。
我目前的工作解决方案包括在数据坐标中指定箭头和标签的位置:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.transforms import blended_transform_factory
x = np.random.randn(10000)
r = 3
label = 'foo'
arrowprops = dict(fc='r', ec='k')
def make_example_plot():
fig, ax = plt.subplots(1, 1)
ax.hold(True)
counts, edges, patches = ax.hist(x)
return fig, ax
fig, ax = make_example_plot()
lo, hi = ax.get_ylim()
ax.annotate(label, xy=(r, 0), xycoords='data',
xytext=(r, hi * 1.1), textcoords='data', fontsize='xx-large',
ha='center', va='center', color='r', arrowprops=arrowprops)
ax.set_ylim(0, hi * 1.3)
无论我如何缩放或平移 y 轴,我都希望标签保持在 y 方向的恒定位置。我可以通过将混合 x-y 转换传递给 ax.text
:
来实现纯文本标签的预期效果
fig, ax = make_example_plot()
tform = blended_transform_factory(ax.transData, ax.transAxes)
ax.text(r, 0.9, label, fontsize='xx-large', color='r', transform=tform)
如果您复制此图然后平移或缩放它,您会看到文本相对于轴边界框在 x 方向移动,但在 y 方向保持固定位置。当然,这仍然没有给我箭头。我希望我可以对 ax.annotate
使用相同的方法,但这似乎不起作用:
fig, ax = make_example_plot()
tform = blended_transform_factory(ax.transData, ax.transAxes)
ax.annotate(label, xy=(r, 0), xycoords='data', transform=tform,
xytext=(r, 0.9), textcoords='data', fontsize='xx-large',
ha='center', va='center', color='r', arrowprops=arrowprops)
标签和箭头位于数据坐标中的 y = 0.9
,而不是 y 轴总高度的 90%:
有没有办法单独指定应用于 matplotlib.text.Annotation
的 x 和 y 变换的参考系?
将转换传递给 xycoords
和 textcoords
参数而不是 transform
参数。像这样:
fig, ax = make_example_plot()
tform = blended_transform_factory(ax.transData, ax.transAxes)
ax.annotate(label, xy=(r, 0), xycoords=tform,
xytext=(r, 0.9), textcoords=tform, fontsize='xx-large',
ha='center', va='center', color='r', arrowprops=arrowprops)
我有一个绘图,我想在其中用箭头和标签注释 x 轴上的特定位置:
- 需要在数据坐标中准确指定箭头尖端的位置。
- 箭头应该是垂直的,因此箭头钝端(和文本标签)的 x 坐标也应该在数据坐标中准确指定。
- 但是,理想情况下,我希望能够指定箭头钝端相对于轴边界框的 y 位置,而不是数据。
我目前的工作解决方案包括在数据坐标中指定箭头和标签的位置:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.transforms import blended_transform_factory
x = np.random.randn(10000)
r = 3
label = 'foo'
arrowprops = dict(fc='r', ec='k')
def make_example_plot():
fig, ax = plt.subplots(1, 1)
ax.hold(True)
counts, edges, patches = ax.hist(x)
return fig, ax
fig, ax = make_example_plot()
lo, hi = ax.get_ylim()
ax.annotate(label, xy=(r, 0), xycoords='data',
xytext=(r, hi * 1.1), textcoords='data', fontsize='xx-large',
ha='center', va='center', color='r', arrowprops=arrowprops)
ax.set_ylim(0, hi * 1.3)
无论我如何缩放或平移 y 轴,我都希望标签保持在 y 方向的恒定位置。我可以通过将混合 x-y 转换传递给 ax.text
:
fig, ax = make_example_plot()
tform = blended_transform_factory(ax.transData, ax.transAxes)
ax.text(r, 0.9, label, fontsize='xx-large', color='r', transform=tform)
如果您复制此图然后平移或缩放它,您会看到文本相对于轴边界框在 x 方向移动,但在 y 方向保持固定位置。当然,这仍然没有给我箭头。我希望我可以对 ax.annotate
使用相同的方法,但这似乎不起作用:
fig, ax = make_example_plot()
tform = blended_transform_factory(ax.transData, ax.transAxes)
ax.annotate(label, xy=(r, 0), xycoords='data', transform=tform,
xytext=(r, 0.9), textcoords='data', fontsize='xx-large',
ha='center', va='center', color='r', arrowprops=arrowprops)
标签和箭头位于数据坐标中的 y = 0.9
,而不是 y 轴总高度的 90%:
有没有办法单独指定应用于 matplotlib.text.Annotation
的 x 和 y 变换的参考系?
将转换传递给 xycoords
和 textcoords
参数而不是 transform
参数。像这样:
fig, ax = make_example_plot()
tform = blended_transform_factory(ax.transData, ax.transAxes)
ax.annotate(label, xy=(r, 0), xycoords=tform,
xytext=(r, 0.9), textcoords=tform, fontsize='xx-large',
ha='center', va='center', color='r', arrowprops=arrowprops)