Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Plot Multiple Histograms On Same Plot With Seaborn

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.

enter image description here

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?

like image 676
Malonge Avatar asked Apr 01 '16 17:04

Malonge


People also ask

How do you plot two graphs on the same plot in Seaborn?

In Seaborn, we will plot multiple graphs in a single window in two ways. First with the help of Facetgrid() function and other by implicit with the help of matplotlib. data: Tidy dataframe where each column is a variable and each row is an observation.

How do you plot two histograms together?

For plotting two histograms together, we have to use hist() function separately with two datasets by giving some setting. Used to represent the label of the histogram it is of string type. Used for setting amount of transparency. Used to represent the name or label of the histogram.

How do you show two histograms in Python?

Make two dataframes, df1 and df2, of two-dimensional, size-mutable, potentially heterogeneous tabular data. Create a figure and a set of subplots. Make a histogram of the DataFrame's, df1 and df2. To display the figure, use show() method.

What is Seaborn histplot?

seaborn.histplot. ¶. Plot univariate or bivariate histograms to show distributions of datasets. A histogram is a classic visualization tool that represents the distribution of one or more variables by counting the number of observations that fall within disrete bins.

What is multi-dimensional plot data in Seaborn?

In this article, we are going to see multi-dimensional plot data, It is a useful approach to draw multiple instances of the same plot on different subsets of your dataset. It allows a viewer to quickly extract a large amount of information about a complex dataset. In Seaborn, we will plot multiple graphs in a single window in two ways.

How to plot multiple graphs in a single window in Seaborn?

In Seaborn, we will plot multiple graphs in a single window in two ways. First with the help of Facetgrid () function and other by implicit with the help of matplotlib. FacetGrid: FacetGrid is a general way of plotting grids based on a function.

Can I make a histogram with two datasets on one plot?

With matplotlib, I can make a histogram with two datasets on one plot (one next to the other, not overlay). This yields the following plot. 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.


2 Answers

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:

enter image description here

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:

enter image description here

like image 196
Primer Avatar answered Sep 21 '22 01:09

Primer


Merge x and y to DataFrame, then use histplot with multiple='dodge' and hue option:

import random  import matplotlib.pyplot as plt import pandas as pd import seaborn as sns  x = [random.randrange(100) for _ in range(100)] y = [random.randrange(100) for _ in range(100)] df = pd.concat(axis=0, ignore_index=True, objs=[     pd.DataFrame.from_dict({'value': x, 'name': 'x'}),     pd.DataFrame.from_dict({'value': y, 'name': 'y'}) ]) fig, ax = plt.subplots() sns.histplot(     data=df, x='value', hue='name', multiple='dodge',     bins=range(1, 110, 10), ax=ax ) ax.set_xlim([0, 100]) 

Resulting Plot

like image 40
koji8y Avatar answered Sep 22 '22 01:09

koji8y