Matplotlib - 合并文本/注释坐标系统
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的位置。