Outer lines Seaborn violinplot/boxplot

2019-05-24 12:30发布

问题:

I am using the violinplot function from the Seaborn library. Sometimes the outer lines are visualized:

and sometimes they are not:

These examples are based on the same bit of code, running different times:

  df = pd.DataFrame(np.random.randn(100, 4), columns=list('ABCD'))
  sns.violinplot(data=df, order=list(df.columns), cut=0,inner='points', bw='silverman', split=True, color='limegreen')
  plt.show()

How can I manipulate the format of the outer lines?

回答1:

Credits to Serenity for pointing out that this is due to a matplotlib bug (see this reported issue).

It can be solved by using the following function:

def patch_violinplot():
     from matplotlib.collections import PolyCollection
     ax = plt.gca()
     for art in ax.get_children():
          if isinstance(art, PolyCollection):
              art.set_edgecolor((0.3, 0.3, 0.3))

Fixing the example can be done by:

df = pd.DataFrame(np.random.randn(100, 4), columns=list('ABCD'))
sns.violinplot(data=df, order=list(df.columns), cut=0,inner='points', bw='silverman', split=True, color='limegreen')
patch_violinplot()
plt.show()