2

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!

Jim Eisenberg
  • 1,490
  • 1
  • 9
  • 17
mickeyze2
  • 31
  • 5
  • refer [this](https://stackoverflow.com/questions/31626433/edgecolor-of-violinplot-in-seaborn-is-not-determined-by-the-hue) – Shijith Jul 30 '19 at 11:51

1 Answers1

2

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.

Arienrhod
  • 2,451
  • 1
  • 11
  • 19