Matplotlib - 结合文本/注释坐标系统

6
可以在绘图时将两个不同的坐标系结合起来放置文本/注释吗?如此问题中所述,可以将注释的位置指定为绘图大小的分数位置。这在文档中也有进一步介绍。
然而,我想在分形坐标系中指定注释的x坐标,在数据坐标系中指定y坐标。这将使我能够将注释附加到一条水平线上,但确保注释始终靠近(距离绘图边缘一些绘图大小的分数距离)。

2
在教程中查看混合变换 - JohanC
谢谢@JohanC,这就是我想要的! - stacker
1个回答

5

使用 blended_transform_factory(x_transform,y_transform)。该函数返回一个新的变换对象,它应用于x轴的是x_transform,应用于y轴的是y_transform。例如:

import matplotlib.pyplot as plt
from matplotlib.transforms import blended_transform_factory
import numpy as np

x = np.linspace(0, 100,1000)
y = 100*np.sin(x)
text = 'Annotation'

f, ax = plt.subplots()
ax.plot(x,y)
trans = blended_transform_factory(x_transform=ax.transAxes, y_transform=ax.transData)
ax.annotate(text, xy=[0.5, 50], xycoords=trans,ha='center')

然后你将注释放在x轴的中心,y轴的位置为y=50

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接