Seaborn 中的黑白箱线图
Black and white boxplots in Seaborn
我正在尝试使用 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()
产生类似的东西:
我希望框的轮廓是黑色的,没有任何填充或调色板中的更改。
您必须设置每个框的 edgecolor
并使用 set_color
与每个框关联的六行(胡须和中位数):
ax = sns.boxplot(x="day", y="total_bill", data=tips, color='white', width=.5, fliersize=0)
# iterate over boxes
for i,box in enumerate(ax.artists):
box.set_edgecolor('black')
box.set_facecolor('white')
# iterate over whiskers and median lines
for j in range(6*i,6*(i+1)):
ax.lines[j].set_color('black')
如果最后一个周期适用于所有艺术家和台词,那么它可能会减少到:
plt.setp(ax.artists, edgecolor = 'k', facecolor='w')
plt.setp(ax.lines, color='k')
其中 ax
根据 boxplot
.
如果您还需要设置传单的颜色,请按照此设置。
我只是在探索这个,现在似乎有另一种方法可以做到这一点。基本上,有关键字 boxprops
、medianprops
、whiskerprops
和(你猜对了)capprops
,所有这些都是可以传递给 boxplot 函数的字典。我选择在上面定义它们,然后为了可读性将它们解包:
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)
我正在尝试使用 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()
产生类似的东西:
我希望框的轮廓是黑色的,没有任何填充或调色板中的更改。
您必须设置每个框的 edgecolor
并使用 set_color
与每个框关联的六行(胡须和中位数):
ax = sns.boxplot(x="day", y="total_bill", data=tips, color='white', width=.5, fliersize=0)
# iterate over boxes
for i,box in enumerate(ax.artists):
box.set_edgecolor('black')
box.set_facecolor('white')
# iterate over whiskers and median lines
for j in range(6*i,6*(i+1)):
ax.lines[j].set_color('black')
如果最后一个周期适用于所有艺术家和台词,那么它可能会减少到:
plt.setp(ax.artists, edgecolor = 'k', facecolor='w')
plt.setp(ax.lines, color='k')
其中 ax
根据 boxplot
.
如果您还需要设置传单的颜色,请按照此设置
我只是在探索这个,现在似乎有另一种方法可以做到这一点。基本上,有关键字 boxprops
、medianprops
、whiskerprops
和(你猜对了)capprops
,所有这些都是可以传递给 boxplot 函数的字典。我选择在上面定义它们,然后为了可读性将它们解包:
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)