Peter
Peter

Reputation: 155

Annotated bubble chart from a dataframe

I have the following data frame

df_SEDL = pd.DataFrame({
    'SE': {0: 'Bug Prediction', 1: 'Code Navigation & Understanding', 2: 'Code Similarity & Clone Detection', 3: 'Security', 4: 'Bug Prediction', 5: 'Code Navigation & Understanding', 6: 'Code Similarity & Clone Detection', 7: 'Security', 8: 'Bug Prediction', 9: 'Code Navigation & Understanding', 10: 'Code Similarity & Clone Detection', 11: 'Security', 12: 'Bug Prediction', 13: 'Code Navigation & Understanding', 14: 'Code Similarity & Clone Detection', 15: 'Security', 16: 'Bug Prediction', 17: 'Code Navigation & Understanding', 18: 'Code Similarity & Clone Detection', 19: 'Security', 20: 'Bug Prediction', 21: 'Code Navigation & Understanding', 22: 'Code Similarity & Clone Detection', 23: 'Security', 24: 'Bug Prediction', 25: 'Code Navigation & Understanding', 26: 'Code Similarity & Clone Detection', 27: 'Security', 28: 'Bug Prediction', 29: 'Code Navigation & Understanding', 30: 'Code Similarity & Clone Detection', 31: 'Security'},
    'DL': {0: 'ANN', 1: 'ANN', 2: 'ANN', 3: 'ANN', 4: 'Autoencoder', 5: 'Autoencoder', 6: 'Autoencoder', 7: 'Autoencoder', 8: 'CNN', 9: 'CNN', 10: 'CNN', 11: 'CNN', 12: 'GNN', 13: 'GNN', 14: 'GNN', 15: 'GNN', 16: 'LSTM', 17: 'LSTM', 18: 'LSTM', 19: 'LSTM', 20: 'Other_DL', 21: 'Other_DL', 22: 'Other_DL', 23: 'Other_DL', 24: 'RNN', 25: 'RNN', 26: 'RNN', 27: 'RNN', 28: 'attention mechanism', 29: 'attention mechanism', 30: 'attention mechanism', 31: 'attention mechanism'},
    'Count': {0: 2.0, 1: 5.0, 2: 3.0, 3: 1.0, 4: 0.0, 5: 11.0, 6: 6.0, 7: 1.0, 8: 1.0, 9: 9.0, 10: 4.0, 11: 5.0, 12: 0.0, 13: 3.0, 14: 3.0, 15: 1.0, 16: 3.0, 17: 17.0, 18: 9.0, 19: 5.0, 20: 1.0, 21: 3.0, 22: 1.0, 23: 2.0, 24: 1.0, 25: 8.0, 26: 4.0, 27: 3.0, 28: 2.0, 29: 16.0, 30: 4.0, 31: 1.0}
})

I'm trying to plot a bubble chart using the following simple code

fig = plt.figure(figsize= (10,8))
ax = fig.add_subplot(111)
ax.scatter(x="DL", y="SE", s="Count", data=df_SEDL,alpha = 0.7,c=df_SEDL.Count*5000)
#plt.margins(.4)
fig.autofmt_xdate()
plt.show()

but eventually, I've got the shape that I don't want which is

enter image description here

I need help to get the shape exactly like the following enter image description here

with the same X & Y axis of the first figure, but with bigger bubbles (different sizes according to the information and different colors), with numeric information ( numbers inside each bubble) so exactly as the second figure

Upvotes: 0

Views: 308

Answers (1)

j-i-l
j-i-l

Reputation: 10957

The marker size s of scatter is set in units of points. So, if your markers are too small, scale the argument you are passing to s.

Here is an example:

s_scaling = 80
fig = plt.figure(figsize= (10,8))
ax = fig.add_subplot(111)
ax.scatter(x="DL", y="SE", s=df.Count*s_scaling,  # scaling the size here
           data=df, alpha=0.7, c=df.Count*5000)

Leading to:

scaled scatter example

If this still is too small, simply adapt the value in s_scaling to your liking.


Now if you want to add the count as text, you can loop over the rows of your df and add text to your axes:

for index, row in df.iterrows():
    ax.text(row.DL, row.SE, row.Count, ha='center', va='center')
plt.show()

enter image description here

To further style and position the text have a look at the available options.

Hope that helps!

Upvotes: 1

Related Questions