Python 如何使用 Seaborn 在同一图上绘制多个直方图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36362624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To Plot Multiple Histograms On Same Plot With Seaborn
提问by Malonge
With matplotlib, I can make a histogram with two datasets on one plot (one next to the other, not overlay).
使用 matplotlib,我可以在一个图上使用两个数据集制作直方图(一个挨着另一个,而不是重叠)。
import matplotlib.pyplot as plt
import random
x = [random.randrange(100) for i in range(100)]
y = [random.randrange(100) for i in range(100)]
plt.hist([x, y])
plt.show()
This yields the following plot.
这产生了以下情节。
However, when I try to do this with seabron;
但是,当我尝试使用 seabron 执行此操作时;
import seaborn as sns
sns.distplot([x, y])
I get the following error:
我收到以下错误:
ValueError: color kwarg must have one color per dataset
So then I try to add some color values:
那么我尝试添加一些颜色值:
sns.distplot([x, y], color=['r', 'b'])
And I get the same error. I saw this poston how to overlay graphs, but I would like these histograms to be side by side, not overlay.
我得到了同样的错误。我看到这篇关于如何叠加图形的帖子,但我希望这些直方图并排放置,而不是叠加。
And looking at the docsit doesn't specify how to include a list of lists as the first argument 'a'.
查看文档,它没有指定如何将列表列表包含为第一个参数“a”。
How can I achieve this style of histogram using seaborn?
如何使用 seaborn 实现这种直方图风格?
回答by Primer
If I understand you correctly you may want to try something this:
如果我理解正确,您可能想尝试以下操作:
fig, ax = plt.subplots()
for a in [x, y]:
sns.distplot(a, bins=range(1, 110, 10), ax=ax, kde=False)
ax.set_xlim([0, 100])
Which should yield a plot like this:
这应该产生这样的情节:
UPDATE:
更新:
Looks like you want 'seaborn look' rather than seaborn plotting functionality. For this you only need to:
看起来您想要“seaborn 外观”而不是 seaborn 绘图功能。为此,您只需要:
import seaborn as sns
plt.hist([x, y], color=['r','b'], alpha=0.5)
Which will produce:
这将产生: