Matplotlib组合文字/注释坐标系

2024-04-30 00:32:18 发布

您现在位置:Python中文网/ 问答频道 /正文

在绘图上定位文本/注释时,是否可以组合两个不同的坐标系?如this question中所述,可以将注释的位置指定为绘图大小的分形位置。文件中还进一步介绍了这一点

但是,我想在分形坐标系中指定注释的x坐标,在数据坐标系中指定y坐标。这将允许我将注释附加到水平线,但要确保注释始终靠近(远离打印大小的一部分)打印边缘


Tags: 文件数据定位文本绘图this边缘分形
1条回答
网友
1楼 · 发布于 2024-04-30 00:32:18

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

相关问题 更多 >