0

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

BlazePascal
  • 391
  • 5
  • 12
  • The points are the data in your data frame. Do you want to label every data point? See http://matplotlib.org/devdocs/users/annotations.html – tacaswell Dec 22 '16 at 23:36
  • As long as we do not know by which criterion you want to label your points, I fear we cannot help you. – ImportanceOfBeingErnest Dec 22 '16 at 23:57
  • I'm referring to the `marker` points that are created in the graph. I attached a picture (the `marker` points are the larger circular points). – BlazePascal Dec 23 '16 at 00:33

1 Answers1

1

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="->"))
Community
  • 1
  • 1
Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
  • According to [this answer](https://stackoverflow.com/a/55557758), we should ***not*** loop over dataframes. How would you modify this answer so that it does not iterate over the dataframe? – kkuilla Mar 06 '20 at 12:11