BlazePascal
BlazePascal

Reputation: 401

Adding labels to points from a Pandas dataframe object using Matplotlib

I want to add labels to the marker points automatically generated by matplotlib, but I don't know how to find the points (the boldened i and j below) from my Pandas DataFrameGroupby object. My command to create the graph is

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")

Where the graph is (and csvdata is a Pandas DataFrame object that is created from read_csv(...))

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

I can verify that an xy label is created if I hardcode a known point.

Here is an attached image with marker points:

enter image description here

Upvotes: 0

Views: 803

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40697

it's really hard to be sure, since you do not provide the content of your dataframe. In the future, please consider generating a Minimal, Complete, and Verifiable example

That being said, I think this is what you are looking for:

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")

an alternate way of achieving the same thing, but which could be easier to read would be:

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

Upvotes: 1

Related Questions