【问题标题】:Matplotlib - Combine text/annotation coordinate systemsMatplotlib - 结合文本/注释坐标系
【发布时间】:2020-05-18 23:26:44
【问题描述】:
在绘图上定位文本/注释时是否可以组合两个不同的坐标系?如this question 中所述,您可以将注释的位置指定为绘图大小的分形位置。文档中还进一步介绍了here。
但是,我想在分形坐标系中指定注释的 x 坐标,在数据坐标系中指定 y 坐标。这将允许我将注释附加到水平线上,但要确保注释始终靠近(远离绘图大小的一部分)绘图边缘。
【问题讨论】:
标签:
python
python-3.x
matplotlib
【解决方案1】:
使用blended_transform_factory(x_transform,y_transform)。该函数返回一个新的转换,它将x_transform 应用于 x 轴,y_transform 应用于 y 轴。例如:
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位置。