如何使用 Seaborn 修改 Violinplot 的边缘颜色 [英] How to modify edge color of Violinplot using Seaborn

查看:60
本文介绍了如何使用 Seaborn 修改 Violinplot 的边缘颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改 Seaborn 中小提琴的边缘颜色.下面的代码对我有用.

ax=sns.violinplot(data=df, x="#", y="SleepAmount", Hue="Thr", Palette=my_pal, split=True,linewidth = 1,inner=None)ax2 = sns.pointplot(x="#", y="SleepAmount", Hue="Thr", data=df, dodge=0.3, join=False, Palette=['black'], ax=ax,errwidth=1、ci="sd",markers="_") plt.setp(ax.collections, edgecolor="k")plt.setp(ax2.collections, edgecolor="k")

但是当我使用 facetgrid 时,我不知道如何将 plt.setp(ax.collections, edgecolor="k") 用于下面的 facetgrid 映射.

g = sns.FacetGrid(df, col="温度",sharey=True)g=g.map(sns.violinplot,"#", "latency", "Thr", Palette=my_pal, split=True, linewidth = 1,inner=None,data=df)g=g.map(sns.pointplot, "#", "latency", "Thr", data=df, dodge=0.3, join=False, Palette=['black'],errwidth=1, ci="sd", 标记="_")

我尝试了很多东西.喜欢,

sns.set_edgecolor('k')sns.set_style({"lines.color": "k"})sns.set_collections({'edgecolor':'k'})g.fig.get_edgecolor()[0].set_edge("k")g.setp.get_collections()[0].set_edgecolor("k")

谁能帮帮我?

还有一个简单的问题,除了whitegrid和darkgrid之外,是否可以更改网格颜色?Facecolor 对我不起作用,因为它为所有背景着色,包括刻度和标签区域.我只想改变网格区域.谢谢!

解决方案

引用

然而,seaborn 的 FacetGrid 是基于 matplotlib 的 subplots.一旦准备好绘制,也许有一个改变图表的技巧.

I'm trying to change the edge color of violins in Seaborn. And the code below, worked for me.

ax=sns.violinplot(data=df, x="#", y="SleepAmount", hue="Thr", palette=my_pal, split=True,linewidth = 1, inner=None)
ax2 = sns.pointplot(x="#", y="SleepAmount", hue="Thr", data=df, dodge=0.3, join=False, palette=['black'], ax=ax,errwidth=1, ci="sd", markers="_") plt.setp(ax.collections, edgecolor="k") 
plt.setp(ax2.collections, edgecolor="k")

But when I use facetgrid, I have no idea how to adopt plt.setp(ax.collections, edgecolor="k") into facetgrid map below.

g = sns.FacetGrid(df, col="temperature",sharey=True)
g=g.map(sns.violinplot,"#", "latency", "Thr", palette=my_pal, split=True, linewidth = 1, inner=None,data=df)
g=g.map(sns.pointplot, "#", "latency", "Thr", data=df, dodge=0.3, join=False, palette=['black'],errwidth=1, ci="sd", markers="_")

I've tried so many thing. like,

sns.set_edgecolor('k')   
sns.set_style( {"lines.color": "k"})
sns.set_collections({'edgecolor':'k'})
g.fig.get_edgecolor()[0].set_edge("k")
g.setp.get_collections()[0].set_edgecolor("k")

can anyone help me out?

One more quick question, Is it possible to change grid color other than whitegrid nor darkgrid? Facecolor does not work for me since it colors all the background including ticks and labels area. I just want to change only the grid area. Thank you!

解决方案

Quoting seaborn's documentation for violinplot:

linewidth : float, optional Width of the gray lines that frame the plot elements.

So it looks pretty hardcoded.

As a matter of fact, quoting seaborn's draw violins code:

def draw_violins(self, ax):
    """Draw the violins onto `ax`."""
    fill_func = ax.fill_betweenx if self.orient == "v" else ax.fill_between
    for i, group_data in enumerate(self.plot_data):

        kws = dict(edgecolor=self.gray, linewidth=self.linewidth)

So I guess you can't do that easily with seaborn's API. Or if you're into hacking/monkey patching the seaborn's classes ...

import seaborn.categorical
seaborn.categorical._Old_Violin = seaborn.categorical._ViolinPlotter

class _My_ViolinPlotter(seaborn.categorical._Old_Violin):

    def __init__(self, *args, **kwargs):
        super(_My_ViolinPlotter, self).__init__(*args, **kwargs)
        self.gray='red'

seaborn.categorical._ViolinPlotter = _My_ViolinPlotter

import seaborn as sns
import matplotlib.pyplot as plt


sns.set(style="ticks", color_codes=True)
tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, col="time", row="smoker")
g.map(sns.violinplot, "tip")
plt.show()

However, seaborn's FacetGrid is based on matplotlib's subplots. Maybe there is a trick to change the diagram once it is prepared for drawing.

这篇关于如何使用 Seaborn 修改 Violinplot 的边缘颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆