简体   繁体   中英

How to change colour of inner box in seaborn violinplot?

I am looking to change the colour of the inner box plot generated by sns.violinplot() to black, see picture below. I've tried using patch but can only find how to do the outer edge of the violin plot and not the inner box. 孔隙率小提琴图

Relevant code:

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)

You can specify the color of the violin plot in the following ways.

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

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

在此处输入图片说明

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)

在此处输入图片说明

Patch artist can be used to change the colour of elements inside of a plot.

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)

yields

vplot里面的黑盒子

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM