4
$\begingroup$

I would like to label my bubble plot, with the labels being INSIDE the bubbles. Something like this. Bubble plot

Is There any way to do that?

What I've tried:

plt.annotate(temp.index, (temp.gre_total, temp.ugrad_gpa))
plt.show()

But that throws an error. I think that would only work if I looped through every value? I'm not very sure.

$\endgroup$
4
$\begingroup$

You can use the seaborn package, using the scatterplot marker size to generate your bubbles. Then you need to loop over the datapoints and add a text labet to each point in your scatterplot.

# Load libraries
import pandas as pd
import matplotlib.pylab as plt
import seaborn as sns

# Create example dataframe
df = pd.DataFrame({
'x': [1, 1.1, 1.2, 2, 5],
'y': [5, 15, 7, 10, 2],
's': [10000,20000,30000,40000,50000],
'group': ['Stamford','Yale','Harvard','MIT','Cambridge']
})

#Create figure
plt.figure(figsize = (15,10))

# Create scatterplot. alpha controls the opacity and s controls the size.
ax = sns.scatterplot(df.x, df.y, alpha = 0.5,s = df.s)

ax.set_xlim(0,6)
ax.set_ylim(-2, 18)

#For each point, we add a text inside the bubble
for line in range(0,df.shape[0]):
     ax.text(df.x[line], df.y[line], df.group[line], horizontalalignment='center', size='medium', color='black', weight='semibold')

Which outputs: enter image description here

$\endgroup$
2
  • 2
    $\begingroup$ I had to use iloc to put labels, just in case someone face issues with the above answer. Try replacing: ax.text(df.x[line], df.y[line], df.group[line], horizontalalignment='center', size='medium', color='black', weight='semibold') with: ax.text(df.x.iloc[line], df.y.iloc[line], df.group.iloc[line], horizontalalignment='center', size='medium', color='black', weight='semibold') $\endgroup$ Jun 15 '19 at 12:12
  • $\begingroup$ @RahulBajaj Thanks for this but is still didn't work for me. $\endgroup$ Feb 21 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.