1

I have a pandas dataframe (db_USA) that I'm using to create a scatterplot. I want to label each observation with the values in the "TIME" column.
The examples I've found use a list, and I've tried converting the columns im using in the code to lists but I'm not getting the result I'm looking for

plt.scatter(db_USA["Value"], db_USA["3M Change %"])
plt.axvline(x=100.0, color ="black")
plt.axhline(y=0.0, color="black")
plt.xlim(right=103)
for i, txt in enumerate(list(db_USA["TIME"])):
    plt.annotate(txt, (list(db_USA["Value"])[i],list(db_USA["LOCATION"])[i]))
plt.show()

This is the result I'm getting.

enter image description here

Any suggestions are welcomed. Thank you.

Yago
  • 63
  • 5
  • Watch this : https://stackoverflow.com/a/14434334/4688639 – Soroosh Noorzad Apr 20 '21 at 15:08
  • And, Why do you plotting a figure based on something and annotating it based on something else? I mean, are not any differences between "3M Change %" and "LOCATION"? – Soroosh Noorzad Apr 20 '21 at 15:13
  • Soroosh- I saw the example you reference but it uses lists instead of the panda dataframe. If I try running the code without the conversion to lists I get the following error message: "A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead." I don't understand the suggestion. – Yago Apr 20 '21 at 16:47
  • 1
    It's difficult to diagnose the problem without an MWE. As @SorooshNoorzad noted, it's unclear why the label is being placed at "LOCATION" and not "3M Change %". I tried to reproduce the problem with a fake data set and the code you posted worked without error, so the issue might have to do with the nature of the DataFrame. – gil Apr 20 '21 at 17:40
  • @gil Thank you for your response. Mapping the correct column to the iteration fixed the problem. – Yago Apr 20 '21 at 18:06

0 Answers0