编辑大熊猫和海豹箱中箱形图不同部分的颜色

时间:2020-06-30 10:28:59

标签: python pandas seaborn

我正在进行一些预测研究,将一些东西与箱线图进行比较。 yaxis代表我预测的将来。 xaxis是预测的误差

然后,对于每个y值,我都会运行4次(因为我更改了预测模型的输入数据)。因此,我想为每个y坐标获取4个重叠的箱形图。

现在我正在玩盒子的宽度和透明度,但是我无法编辑颜色。

这是两个示例: enter image description here

这是一个小插图中的另一个示例,我确实设法完成了该操作:

enter image description here

我希望盒图的颜色代码与条形图的颜色代码相同。

这是我到目前为止绘制方框图的代码:

browser.find_element_by_xpath("/html/body/div[6]/jn-root/jn-layout-main/section/main/article/jn-jobseeking-detail/jn-page/jn-form/form/jn-job/jn-details-container/div/fieldset/div/div[1]/jn-form-typeahead-occupations/div/jn-typeahead-reactive-occupations/input").send_keys('hello')

这是条形图的一种(以防万一):

def PrintBoxPlots(Error0, Error1, Error2, Error3, str_Sampling, str_SOMOHY):
    """Plot boxplots of the errors"""
    fig, ax = plt.subplots()  # new plot
    
    ax = sns.boxplot(data=Error0, width=1, boxprops=dict(alpha=1)) #This is for Type0
    
    #Type 1 from no on
    for i,artist in enumerate(ax.artists):
        # Set the linecolor on the artist to the facecolor, and set the facecolor to None
        col = artist.get_facecolor()
        artist.set_edgecolor('black')
        artist.set_facecolor(col)
        artist.set_linewidth(2)
    
        # Each box has 6 associated Line2D objects (to make the whiskers, fliers, etc.)
        # Loop over them here, and use the same colour as above
        for j in range(i*6,i*6+6):
            line = ax.lines[j]
            line.set_color(col)
            line.set_mfc(col)
            line.set_mec(col)
            line.set_linewidth(2)
    
    ax = sns.boxplot(data=Error1, width=0.75, boxprops=dict(alpha=0.8))
    
    #Type 2 from no on
    for i,artist in enumerate(ax.artists):
        
        for j in range(i*6,i*6+6):
            line = ax.lines[j]
            line.set_color(col)
            line.set_mfc(col)
            line.set_mec(col)
            line.set_linewidth(2)
    
    ax = sns.boxplot(data=Error2, width=0.5, boxprops=dict(alpha=0.6))
    
    #Type 3 from no on
    for i,artist in enumerate(ax.artists):
       
        for j in range(i*6,i*6+6):
            line = ax.lines[j]
            line.set_color(col)
            line.set_mfc(col)
            line.set_mec(col)
            line.set_linewidth(2)
    
    ax = sns.boxplot(data=Error3, width=0.25, boxprops=dict(alpha=0.4))
    
    
    ax.set_xlabel('Prediction Horizon [sec]')
    labels = ['5', '15', '30', '60', '100', '300', '600', '900']#, '1800', '2700','3600', '7200', '21600']
    # plt.xticks(range(0,14), labels, rotation='vertical')
    plt.xticks(range(0,8), labels, rotation='vertical')
    ax.set_ylabel('Error=Forecasted-Real in pu')
    ax.set_title("Error for different horizons and methods " + str_SOMOHY + ", sampling " + str_Sampling)
    ax.grid(alpha=0.5)  # background grid, alpha again regulates the transparency
    
    patch_Type0 = mpatches.Patch(color='blue', label='Type0 RF', alpha=0.4)
    patch_Type1 = mpatches.Patch(color='blue', label='Type1 RF')
    patch_Type2 = mpatches.Patch(color='red', label='Type2 RF', alpha=1)
    patch_Type3 = mpatches.Patch(color='green', label='Type3 RF', alpha=1)
    plt.legend(handles=[patch_Type0, patch_Type1, patch_Type2 , patch_Type3 ])

0 个答案:

没有答案