是否可以将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?

查看:83
本文介绍了是否可以将matplotlib批注锚定到x轴上的数据坐标,但是锚定到y轴上的相对位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图,我想用箭头和标签在x轴上的特定位置注释:

I have a plot where I'd like to annotate a specific location on the x-axis with an arrow and a label:

  • 箭头的位置需要在数据坐标中精确指定.
  • 箭头应垂直,因此箭头的钝端(和文本标签)的x坐标也应在数据坐标中精确指定.
  • 但是,理想情况下,我希望能够指定箭头的钝端相对于轴定界框而不是数据的y位置.

我当前的工作解决方案涉及在数据坐标中指定箭头尖和标签的位置:

My current working solution involves specifying the locations of both the arrow tip and the label in data coordinates:

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,我可以实现纯文本标签的预期效果:

I would prefer to have the label stay at a constant position in y regardless of how I scale or pan the y-axes. I can achieve the desired effect for a plain text label by passing a blended x-y transformation to 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使用相同的方法,但这似乎不起作用:

If you reproduce this figure then pan or scale it, you will see that the text moves in x relative to the axis bounding box, but remains in a fixed position in y. Of course this still doesn't give me the arrow, though. I was hoping that I could use the same approach with ax.annotate, but this does not seem to work:

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%处:

The label and arrow are placed at y = 0.9 in data coordinates, not at 90% of the total height of the y-axis:

是否有一种方法可以分别指定应用于matplotlib.text.Annotation的x和y转换的参考系?

Is there a way to separately specify the frames of reference for the x- and y-transformations applied to a matplotlib.text.Annotation?

推荐答案

将转换传递到xycoordstextcoords参数,而不是transform参数.像这样:

Pass the transformation to the xycoords and textcoords parameters instead of to the transform parameter. Like so:

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)

这篇关于是否可以将matplotlib批注锚定到x轴上的数据坐标,但是锚定到y轴上的相对位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆