如何更改Seaborn小提琴中内盒的颜色?

时间:2020-10-01 03:57:33

标签: python seaborn

我希望将sns.violinplot()生成的内盒图的颜色更改为黑色,请参见下图。我试过使用补丁,但只能找到小提琴图的外边缘而不是内盒。 porosity violin plot

相关代码:

def poros_voilin(data):

    fig, ax = plt.subplots()
    sns.set_style(rc={'patch'})
    ax.axhline(0.84, xmin=0.26, xmax=0.94, color='k')
    ax.annotate(xy=(0.795, 0.63), xycoords='axes fraction', xytext=(0.795, 0.63), textcoords='axes fraction',
                s='SGL 25 BA')
    ax.axhline(0.78, xmin=0.36, xmax=0.985, color='k')
    ax.annotate(xy=(0.69, 0.535), xycoords='axes fraction', xytext=(0.69, 0.535), textcoords='axes fraction',
                s='Toaray 060, 090, 120')
    ax.axhline(0.63, xmin=0.485, xmax=0.755, color='k')
    ax.annotate(xy=(0.54, 0.29), xycoords='axes fraction', xytext=(0.54, 0.29), textcoords='axes fraction',
                s='ELAT LT 1400W')
    vplot = sns.violinplot(y=data['poros'].astype(float), ax=ax)

    plt.show()

if __name__ == '__main__':
    data = load_data() 
    poros_violin(data)

2 个答案:

答案 0 :(得分:1)

补丁艺术家可以用来更改情节内部元素的颜色。

def poros_voilin(data):

    fig, ax = plt.subplots()
    # ax.axhline(0.84, xmin=0.26, xmax=0.98, color='k')
    ax.axhline(0.84, color='k', linestyle='--', linewidth=1)
    ax.annotate(xy=(0.98, 0.63), xycoords='axes fraction', xytext=(0.98, 0.63), textcoords='axes fraction',
                s='SGL 25 BA', ha='right')
    # ax.axhline(0.78, xmin=0.36, xmax=0.98, color='k')
    ax.axhline(0.78, color='k', linestyle='-.', linewidth=1)
    ax.annotate(xy=(0.98, 0.535), xycoords='axes fraction', xytext=(0.98, 0.535), textcoords='axes fraction',
                s='Toray 060, 090, 120', ha='right')
    # ax.axhline(0.63, xmin=0.485, xmax=0.98, color='k')
    ax.axhline(0.63, color='k', linestyle=':', linewidth=1.5)
    ax.annotate(xy=(0.98, 0.29), xycoords='axes fraction', xytext=(0.98, 0.29), textcoords='axes fraction',
                s='ELAT LT 1400W', ha='right')
    vplot = sns.violinplot(y=data['poros'].astype(float), ax=ax)
    ax.get_children()[5].set_color('k')  # <------------- changes the colour of the sticks
    ax.get_children()[6].set_color('k')  # <------------- changes the colour of the box
    # sns.boxenplot(y=data['poros'].astype(float), ax=ax)

    plt.show()

if __name__ == '__main__':
    data = load_data() 
    poros_violin(data)

收益

black box inside vplot

答案 1 :(得分:0)

您可以通过以下方式指定小提琴图的颜色。

import seaborn as sns
df = sns.load_dataset('iris')

sns.violinplot( x=df["species"], y=df["sepal_length"], color="skyblue")

enter image description here

import seaborn as sns
df = sns.load_dataset('iris')

sns.violinplot(x=df["species"], y=df["sepal_length"], color="skyblue", inner=None)
sns.boxenplot(x=df["species"], y=df["sepal_length"], color="red", width=0.05)

enter image description here