Issue
I've been stuck on this for quite a while and I'm not sure how to fix it - here is the code and the error that comes with it:
data = data.sort_values(by=['Happiness Rank'])
plt.figure(figsize=(20,13))
sns.scatterplot(data=data, x='Happiness Score',y='Economy (GDP per Capita)', hue = 'Year')
plt.title('Life Satisfication vs GDP per Capita',fontsize=20)
plt.xlabel('Life Satisfication',fontsize=16)
plt.ylabel('GDP per Capita',fontsize=16)
for i in range(len(data)):
plt.text(s=data.loc[i,'Country'], x=data.loc[i,'Happiness Score']+0.01,
y=data.loc[i,'Economy (GDP per Capita)']+0.01, fontsize=10)
plt.show()
This code prints out the scatterplot with points but doesn't have the country names next to the points, which is where the error comes in:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-35-c451b5b6b763> in <module>
5 plt.ylabel('GDP per Capita',fontsize=16)
6 for i in range(len(data)):
----> 7 plt.text(s=(data.loc[i,'Country']), x=(data.loc[i,'Happiness Score']+0.01),
8 y=(data.loc[i,'Economy (GDP per Capita)']+0.01), fontsize=10)
9 plt.show()
~/opt/anaconda3/lib/python3.8/site-packages/matplotlib/pyplot.py in text(x, y, s, fontdict, **kwargs)
3007 @_copy_docstring_and_deprecators(Axes.text)
3008 def text(x, y, s, fontdict=None, **kwargs):
-> 3009 return gca().text(x, y, s, fontdict=fontdict, **kwargs)
3010
3011
~/opt/anaconda3/lib/python3.8/site-packages/matplotlib/axes/_axes.py in text(self, x, y, s, fontdict, **kwargs)
763 **kwargs,
764 }
--> 765 t = mtext.Text(x, y, text=s, **effective_kwargs)
766 t.set_clip_path(self.patch)
767 self._add_text(t)
~/opt/anaconda3/lib/python3.8/site-packages/matplotlib/text.py in __init__(self, x, y, text, color, verticalalignment, horizontalalignment, multialignment, fontproperties, rotation, linespacing, rotation_mode, usetex, wrap, **kwargs)
149 self._x, self._y = x, y
150 self._text = ''
--> 151 self.set_text(text)
152 self.set_color(color if color is not None else rcParams["text.color"])
153 self.set_fontproperties(fontproperties)
~/opt/anaconda3/lib/python3.8/site-packages/matplotlib/text.py in set_text(self, s)
1163 if s is None:
1164 s = ''
-> 1165 if s != self._text:
1166 self._text = str(s)
1167 self.stale = True
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py in __nonzero__(self)
1440 @final
1441 def __nonzero__(self):
-> 1442 raise ValueError(
1443 f"The truth value of a {type(self).__name__} is ambiguous. "
1444 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I'd really appreciate it if anyone knows how to fix this/can explain why this is happening so I can avoid things like this in the future. Thanks in advance!
Solution
Your code looks like you expected that the index had consecutive values, starting from 0.
But most likely your DataFrame has the index with repeating values. In such case, loc[i,…] retrieves all rows with the index of i and from them returns the particular column. The type of such returned value is a Series not a "single" value.
One of ways to circumvent this problem is to iterate over rows, e.g. using iterrows. Then you can actually ignore index values and retrieve only columns of interest from the current row.
So change your loop to something like:
for _, row in data.iterrows():
plt.text(s=row['Country'], x=row['Happiness Score']+0.01,
y=row['Economy (GDP per Capita)']+0.01, fontsize=10)
"_" in the above code is needed as a placeholder to capture the index of the current row and row is actually a Series holding the current row.
Then row[…] retrieves the content of partucular column from the current row.
Answered By - Valdi_Bo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.