gpt4 book ai didi

python - 如何使用 Seaborn 修改 Violinplot 的边缘颜色

转载 作者:太空狗 更新时间:2023-10-30 01:31:44 25 4
gpt4 key购买 nike

我正在尝试更改 Seaborn 中 fiddle 的边缘颜色。下面的代码对我有用。

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 map 中。

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="_")

我已经尝试了很多东西。喜欢,

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's documentation for violinplot :

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

所以它看起来很硬编码。

事实上,引用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)

所以我猜你不能用 seaborn 的 API 轻松做到这一点。或者如果你喜欢黑客/monkey patching seaborn 的类(class) ...

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

enter image description here

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

关于python - 如何使用 Seaborn 修改 Violinplot 的边缘颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49926147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com