如何在 Seaborn 箱线图中加宽箱子?
How to widen boxes in Seaborn boxplot?
我正在尝试使用 Seaborn (Reference) 制作分组箱线图,但箱子都非常窄——太窄以至于看不到分组颜色。
g = seaborn.factorplot("project_code",y="num_mutations",hue="organ",
data=grouped_donor, kind="box", aspect=3)
如果我放大图形,或将图形拉伸到屏幕宽度的几倍,我可以看到方框,但显然这作为标准图形没有用。
这似乎与我的数据量有关;如果我只绘制前 500 个点(共 6000 个),我会得到可见但很小的框。它可能特别是我数据的高方差的函数;根据 matplotlib boxplot 文档,
The default [width] is 0.5, or 0.15x(distance between extreme positions) if that is smaller.
无论出于何种原因,图表本身都有足够的空间容纳更宽的框,如果我可以加宽它们的话。
不幸的是,控制框宽度的 boxplot 关键字 widths
不是有效的 factorplot
关键字,我找不到可以更改条形宽度的 matplotlib 函数或绘图函数本身之外的框。我什至找不到任何人讨论这个问题;我找到的最接近的是箱线图线宽。有什么建议吗?
为了将来参考,以下是使用图例制作正确图形的相关代码:(显然这缺少重要的东西,实际上不会 运行 原样,但希望它能显示棘手的部分)
import matplotlib.pylab as pyp
import seaborn as sns
def custom_legend(colors,labels, legend_location = 'upper left', legend_boundary = (1,1)):
# Create custom legend for colors
recs = []
for i in range(0,len(colors)):
recs.append(mpatches.Rectangle((0,0),1,1,fc=colors[i]))
pyp.legend(recs,labels,loc=legend_location, bbox_to_anchor=legend_boundary)
# Color boxplots by organ
organ_list = sorted(df_unique(grouped_samples,'type'))
colors = sns.color_palette("Paired", len(organ_list))
color_dict = dict(zip(organ_list, colors))
organ_palette = grouped_samples.drop_duplicates('id')['type'].map(color_dict)
# Plot grouped boxplot
g = sns.factorplot("id","num_mutations",data=grouped_samples, order=id_list, kind="box", size=7, aspect=3, palette=organ_palette)
sns.despine(left=True)
plot_setup_pre()
pyp.yscale('log')
custom_legend(colors,organ_list)
当使用 sns.boxplot
时添加 dodge=False
将在 0.9 版本中解决此问题。
sns.factorplot()
自 0.9 版起已被弃用,并已被替换为 catplot()
,后者也具有 dodge
参数。
我正在尝试使用 Seaborn (Reference) 制作分组箱线图,但箱子都非常窄——太窄以至于看不到分组颜色。
g = seaborn.factorplot("project_code",y="num_mutations",hue="organ",
data=grouped_donor, kind="box", aspect=3)
如果我放大图形,或将图形拉伸到屏幕宽度的几倍,我可以看到方框,但显然这作为标准图形没有用。
这似乎与我的数据量有关;如果我只绘制前 500 个点(共 6000 个),我会得到可见但很小的框。它可能特别是我数据的高方差的函数;根据 matplotlib boxplot 文档,
The default [width] is 0.5, or 0.15x(distance between extreme positions) if that is smaller.
无论出于何种原因,图表本身都有足够的空间容纳更宽的框,如果我可以加宽它们的话。
不幸的是,控制框宽度的 boxplot 关键字 widths
不是有效的 factorplot
关键字,我找不到可以更改条形宽度的 matplotlib 函数或绘图函数本身之外的框。我什至找不到任何人讨论这个问题;我找到的最接近的是箱线图线宽。有什么建议吗?
为了将来参考,以下是使用图例制作正确图形的相关代码:(显然这缺少重要的东西,实际上不会 运行 原样,但希望它能显示棘手的部分)
import matplotlib.pylab as pyp
import seaborn as sns
def custom_legend(colors,labels, legend_location = 'upper left', legend_boundary = (1,1)):
# Create custom legend for colors
recs = []
for i in range(0,len(colors)):
recs.append(mpatches.Rectangle((0,0),1,1,fc=colors[i]))
pyp.legend(recs,labels,loc=legend_location, bbox_to_anchor=legend_boundary)
# Color boxplots by organ
organ_list = sorted(df_unique(grouped_samples,'type'))
colors = sns.color_palette("Paired", len(organ_list))
color_dict = dict(zip(organ_list, colors))
organ_palette = grouped_samples.drop_duplicates('id')['type'].map(color_dict)
# Plot grouped boxplot
g = sns.factorplot("id","num_mutations",data=grouped_samples, order=id_list, kind="box", size=7, aspect=3, palette=organ_palette)
sns.despine(left=True)
plot_setup_pre()
pyp.yscale('log')
custom_legend(colors,organ_list)
当使用 sns.boxplot
时添加 dodge=False
将在 0.9 版本中解决此问题。
sns.factorplot()
自 0.9 版起已被弃用,并已被替换为 catplot()
,后者也具有 dodge
参数。