如何在与Seaborn相同的图上绘制多个直方图 [英] How To Plot Multiple Histograms On Same Plot With Seaborn

查看:68
本文介绍了如何在与Seaborn相同的图上绘制多个直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 matplotlib,我可以在一个图上制作包含两个数据集的直方图(一个挨着另一个,而不是重叠).

 将matplotlib.pyplot导入为plt随机导入x = [random.randrange(100) for i in range(100)]y = [i在range(100)中的random.randrange(100)]plt.hist([x,y])plt.show()

这将产生以下曲线.

但是,当我尝试使用seabron进行此操作时

导入 seaborn 为 snssns.distplot([x,y])

我收到以下错误:

  ValueError:每个数据集颜色kwarg必须具有一种颜色

因此,我尝试添加一些颜色值:

sns.distplot([x, y], color=['r', 'b'])

我得到了同样的错误.我看到了

更新:

看起来您想要的是季节性外观",而不是季节性绘图功能.为此,您只需要:

导入 seaborn 为 snsplt.hist([x,y],color = ['r','b'],alpha = 0.5)

哪个会产生:

With matplotlib, I can make a histogram with two datasets on one plot (one next to the other, not overlay).

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;

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 post on how to overlay graphs, but I would like these histograms to be side by side, not overlay.

And looking at the docs it doesn't specify how to include a list of lists as the first argument 'a'.

How can I achieve this style of histogram using seaborn?

解决方案

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:

import seaborn as sns
plt.hist([x, y], color=['r','b'], alpha=0.5)

Which will produce:

这篇关于如何在与Seaborn相同的图上绘制多个直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆