使用Matplotlib从Pandas dataframe对象向点添加标签

2024-06-28 19:13:35 发布

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

我想给matplotlib自动生成的marker点添加标签,但是我不知道如何从Pandas DataFrameGroupby对象中找到这些点(下面加粗的i和{})。我创建图形的命令是

for x, group in graph:
    t = group.plot(x="CompressorSpeed", y="ratio", marker='o').set_title(x)
    plt.annotate('This is awesome', xy=( **i**, **j** ), arrowprops=dict(arrowstyle="->"))
    plt.savefig(pp, format="pdf")

图在其中(并且csvdata是从read_csv(...)创建的Pandas DataFrame对象)

^{pr2}$

如果我硬编码一个已知点,我可以验证是否创建了xy标签。在

以下是带有标记点的附加图像:

enter image description here


Tags: 对象in命令图形pandasforplotmatplotlib
1条回答
网友
1楼 · 发布于 2024-06-28 19:13:35

很难确定,因为您没有提供数据帧的内容。以后,请考虑生成Minimal, Complete, and Verifiable example

话虽如此,我想这就是你想要的:

for x, group in graph:
    t = group.plot(x="CompressorSpeed", y="ratio", marker='o').set_title(x)
    for i,j in group[["CompressorSpeed","ratio"]].values:
        plt.annotate('This is awesome', xy=(i,j), arrowprops=dict(arrowstyle="->"))
    plt.savefig(pp, format="pdf")

实现相同目标的另一种方法是:

^{pr2}$

相关问题 更多 >