【问题标题】:Adding colorbar to seaborn bubble plot向 seaborn 气泡图添加颜色条
【发布时间】:2021-02-28 18:18:32
【问题描述】:

我正在尝试根据我的值的 interval_size 广告颜色条,在尝试使用 seaborn 绘图时遇到以下错误:

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

当前剧情

想要的情节

数据表

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)

【问题讨论】:

    标签: python matplotlib seaborn bubble-chart


    【解决方案1】:

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

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

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

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

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

    【讨论】:

    • 使用c 而不是hue,您的解决方案添加了一个稍有变化的颜色条:bar = plots.get_children()[2]
    • @BigBen 感谢您的贡献,我会将此信息添加到解决方案中
    • 非常感谢你们!使用时:bar = plots.get_children()[0] 我得到了绘图的实际颜色,因为bar = plots.get_children()[2] 它给出了不同的颜色范围
    • @machine_apprentice - 是的,我不确定[2] 是否生成了正确的颜色条,感谢您的澄清。
    猜你喜欢
    • 2020-11-03
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多