使用Matplotlib向Pandas数据框对象中的点添加标签

时间:2016-12-22 22:08:11

标签: python pandas matplotlib

我想为matplotlib自动生成的marker点添加标签,但我不知道如何找到这些点(下面加粗的ij)来自我的Pandas DataFrameGroupby对象。我创建图表的命令是

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对象)

graph=csvdata.groupby("CompressorAlgo", as_index=False)

如果我对已知点进行硬编码,我可以验证是否创建了xy标签。

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

Full Demo

1 个答案:

答案 0 :(得分:1)

很难确定,因为您没有提供数据帧的内容。将来,请考虑生成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")

实现相同目标的另一种方法,但更容易阅读:

for z,row in group.iterrows():
            plt.annotate('This is awesome', xy=(row["CompressorSpeed"],row["ratio"]), arrowprops=dict(arrowstyle="->"))