繁体   English   中英

如何更改熊猫图中小提琴图的边框颜色?

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

我想更改小提琴线边框的颜色。 我可以将lines.linewidth设置为0但是我想显示边框而不是隐藏边框。 如何更改边框的颜色?

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

my_task

我的代码如下:

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')

谢谢!

这应该与您要寻找的非常接近:

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')

patch_violin函数来自此处

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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