0

I have searched and found that using annotate in matplotlib for jupyter, we can name the x and y of a point.

I have retried doing as you suggested.

import matplotlib.pyplot as plt
import pandas as pd

def fit_data():

    fig = plt.figure(1,figsize=(20,6))
    plt.subplot(111)

    data1 = pd.DataFrame({"ID" : list(range(11)), 
                   "R" : list(range(11)),
                   "Theta" : list(range(11))})

    plt.scatter(data1['R'], data1['Theta'],  marker='o', color='b', s=15)

    for i, row in data1.iterrows():
        plt.annotate(row["ID"], xy=(row["R"],row["Theta"]))

    plt.xlabel('R',size=20)
    plt.ylabel('Theta',size=20)

    plt.show()
    plt.close()

fit_data()

It still doesn't take the ID from my data. It is still plotting an arbitrary plot. this is the image after using the revised code

My data is as follows

1 19.177    24.642
2 9.398     12.774
3 9.077     12.373
4 15.287    19.448
5 4.129     5.41
6 2.25      3.416
7 11.674    15.16
8 10.962    14.469
9 1.924     3.628
10 2.087    3.891
11 9.706    13.186
2
  • The syntax for annotate is ax.annotate("text", xy=(x,y)). Instead of "text" you can use your ID. Feel free to provide a code with your attempt and an explanation at which point you are stuck. Commented Aug 3, 2018 at 12:46
  • I have added the code. I need the points to display the ID Commented Aug 3, 2018 at 12:57

1 Answer 1

1

I suppose the confusion comes from the fact that while scatter can plot all points at once, while an annotation is a singular object. You would hence need one annotation per row in the dataframe.

import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({"ID" : list(range(6)),          # Do not copy this part.
                   "R" : [5,4,1,2,3,4],            # Use your own data 
                   "Theta" : [20,15,40,60,51,71]}) # instead.

fig = plt.figure(1,figsize=(20,6))
plt.subplot(111)

plt.scatter(df['R'], df['Theta'],  marker='o', color='b', s=15)

for i, row in df.iterrows():
    plt.annotate(row["ID"], xy=(row["R"],row["Theta"]))

plt.xlabel('R',size=20)
plt.ylabel('Theta',size=20)

plt.show()
Sign up to request clarification or add additional context in comments.

11 Comments

Thank you. The ID that it marks is from '0' not from my data set. If I have a large data set say 100 odd points, then do I have to do the 'df = pd.DataFrame({"ID" : list(range(6)), "R" : [5,4,1,2,3,4], "Theta" : [20,15,40,60,51,71]})' for all those points.
No. I created a dataset df here because I do not have your data. In your case you will use your own data of course.
I have edited the question putting in the trail data set I am using. It has to use the ID mentioned in the file and not an arbitrary one. How do I go about doing that
Instead of the dataset df I used here, you need to use your own dataset data1.
Pardon me I didn't follow. Could you please explain.
|

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.