machine_apprentice
machine_apprentice

Reputation: 439

Adding colorbar to seaborn bubble plot

I'm trying to ad a colorbar based on the interval_size of my values, I've had an issue with the following error when trying to use the seaborn plot:

AttributeError: 'AxesSubplot' object has no attribute 'get_array'

Current Plot

enter image description here

Desired Plot

enter image description here

Data Table

min            max          y    interval_size   y_pred     split
0.654531    1.021657    0.837415    0.367126    0.838094    train
0.783401    1.261898    1.000000    0.478497    1.022649    valid
-0.166070   0.543749    0.059727    0.709819    0.188840    train
0.493270    1.112610    0.504393    0.619340    0.802940    valid
0.140510    0.572957    0.479063    0.432447    0.356734    train

Plot Code

plt.figure(figsize=(16,8))

plots = sns.scatterplot(x="y", 
                y="y_pred",
                #size= "interval_size",            
                data=df,
                alpha=0.65,
                c=df['interval_size'],
                cmap='viridis', 
                #hue = 'split',
                s = (df['interval_size']**2)*50,
                style = 'split',
                markers = {'train': '^', 'valid':'8'}
               )
# Put the legend out of the figure
#plt.legend(bbox_to_anchor=(1.01, 1),borderaxespad=0, title='Data Split', fontsize=20)

##Add Colorbar
plt.colorbar(plots)


#Plot Characteristics
plt.title("Stability: True vs Predicted Labels", fontsize = 36)
plt.xlabel("True Labels", fontsize = 25)
plt.ylabel("Predicted Labels", fontsize = 25)

Upvotes: 0

Views: 510

Answers (1)

Flavio Moraes
Flavio Moraes

Reputation: 1351

I can't reproduce your plot the way it is because for me c doesn't work with sns.scatterplot, thus I have to use hue instead. But I did a test using hue and the following solution worked for me:

bar = plots.get_children()[3] 
plt.colorbar(mappable=bar)

another option for you is to use plt.scatter instead of sns.scatterplot

[Edited] According to comments bellow, using c instead of hue the solution may work with:

bar = plots.get_children()[2] 
plt.colorbar(mappable=bar)

Upvotes: 1

Related Questions