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

3
  • The points are the data in your data frame. Do you want to label every data point? See matplotlib.org/devdocs/users/annotations.html Commented Dec 22, 2016 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. Commented Dec 22, 2016 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). Commented Dec 23, 2016 at 0:33

1 Answer 1

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="->"))
Sign up to request clarification or add additional context in comments.

1 Comment

According to this answer, we should not loop over dataframes. How would you modify this answer so that it does not iterate over the dataframe?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.