简体   繁体   中英

How to change border color of violin plot in pandas graph?

I want to change the color of lineborder of violinplots. I can set lines.linewidth to 0 but I want to show borders not to hide them. How to change the color of the border?

sns.set_context("paper", rc={"lines.linewidth": 0.8})

my_task

My code is as follows:

import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import style 
import pandas as pd
import numpy as np 

datasets = pd.read_csv("merged.csv", index_col=0);

df = datasets   

df.protocol = df.protocol.astype(str) 
f, ax = plt.subplots(figsize=(18, 6))

sns.violinplot(x="time", 
               y="values", 
               hue="protocol",
               data=df,
               bw=.5,
               scale="count" 
              )


sns.despine(left=True)

f.suptitle('Title', fontsize=22, fontweight='bold')
ax.set_xlabel("Time",size = 16,alpha=0.7)
ax.set_ylabel("Values",size = 16,alpha=0.7)   
ax.set_xticklabels(df.qber, rotation=90) 
ax.grid(True)
plt.legend(loc='upper right')
plt.grid(linestyle='--', alpha=0.7)

fig = ax.get_figure()
fig.savefig('time_v.pdf', bbox_inches='tight')

Thank you!

this should be very close to what you're looking for:

import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import style 
import pandas as pd
import numpy as np 

def patch_violinplot(palette, n):
    from matplotlib.collections import PolyCollection
    ax = plt.gca()
    violins = [art for art in ax.get_children() if isinstance(art, PolyCollection)]
    colors = sns.color_palette(palette, n_colors=n) * (len(violins)//n)
    for i in range(len(violins)):
        violins[i].set_edgecolor(colors[i])

datasets = pd.read_csv("merged.csv", index_col=0);

df = datasets   

df.protocol = df.protocol.astype(str)
num_cols = df['protocol'].nunique() 
f, ax = plt.subplots(figsize=(18, 6))

sns.violinplot(x="time", 
               y="values", 
               hue="protocol",
               data=df,
               bw=.5,
               scale="count",
               palette="deep"
              )
patch_violinplot("deep", num_cols)

sns.despine(left=True)

f.suptitle('Title', fontsize=22, fontweight='bold')
ax.set_xlabel("Time",size = 16,alpha=0.7)
ax.set_ylabel("Values",size = 16,alpha=0.7)   
ax.set_xticklabels(df.qber, rotation=90) 
ax.grid(True)
plt.legend(loc='upper right')
plt.grid(linestyle='--', alpha=0.7)

fig = ax.get_figure()
fig.savefig('time_v.pdf', bbox_inches='tight')

The patch_violin function came from here .

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