gpt4 book ai didi

python - Seaborn 中的黑白箱线图

转载 作者:太空狗 更新时间:2023-10-29 22:24:56 24 4
gpt4 key购买 nike

我正在尝试使用 Python 的 Seaborn 包绘制多个黑白箱线图。默认情况下,绘图使用调色板。我想用纯黑色轮廓画出它们。我能想到的最好的是:

# figure styles
sns.set_style('white')
sns.set_context('paper', font_scale=2)
plt.figure(figsize=(3, 5))
sns.set_style('ticks', {'axes.edgecolor': '0',
'xtick.color': '0',
'ytick.color': '0'})

ax = sns.boxplot(x="test1", y="test2", data=dataset, color='white', width=.5)
sns.despine(offset=5, trim=True)
sns.plt.show()

产生类似的东西:

enter image description here

我希望框的轮廓是黑色的,没有任何填充或调色板的变化。

最佳答案

我只是在探索这个,现在似乎有另一种方法可以做到这一点。基本上,有关键字 boxpropsmedianpropswhiskerprops 和(你猜对了)capprops,所有这些是可以传递给箱线图函数的字典。我选择在上面定义它们,然后为了可读性解压它们:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

_to_plot = pd.DataFrame(
{
0: np.random.normal(0,1,100),
1: np.random.normal(0,2,100),
2: np.random.normal(0,-1,100),
3: np.random.normal(0,-2,100)
}
).melt()

PROPS = {
'boxprops':{'facecolor':'none', 'edgecolor':'red'},
'medianprops':{'color':'green'},
'whiskerprops':{'color':'blue'},
'capprops':{'color':'yellow'}
}

sns.boxplot(x='variable',y='value',
data=_to_plot,
showfliers=False,
linewidth=0.75,
**PROPS)

enter image description here

关于python - Seaborn 中的黑白箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43434020/

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