将颜色条添加到 seaborn 气泡图

Adding colorbar to seaborn bubble plot

我正在尝试根据我的 interval_size 值添加颜色条,但在尝试使用 seaborn 绘图时出现以下错误:

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

当前剧情

想要的情节

数据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

剧情代码

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)

我无法按原样重现您的情节,因为对我来说 c 不适用于 sns.scatterplot,因此我必须改用 hue。但我使用 hue 进行了测试,以下解决方案对我有用:

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

您的另一个选择是使用 plt.scatter 而不是 sns.scatterplot

[已编辑] 根据下面的评论,使用 c 而不是 hue 解决方案可能适用于:

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