使用 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
。