Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting seaborn histogram from each column in different subplots (facetgrid)

My structure is following pandas DataFrame:

n    X              Y          Z
0   1.000000    1.000000    1.014925    
1   1.000000    1.000000    1.000000    

I want to create M separate subplots (histogram) from each column. One histogram would be from X, one from Y and the last one from Z.

I would like it to have on separate plots. I was looking into https://seaborn.pydata.org/generated/seaborn.FacetGrid.html, but I don't understand the syntax/logic how to plot it from my data.

like image 761
BraveDistribution Avatar asked Mar 08 '19 18:03

BraveDistribution


People also ask

How do you make multiple subplots in Seaborn?

You can use the following basic syntax to create subplots in the seaborn data visualization library in Python: #define dimensions of subplots (rows, columns) fig, axes = plt. subplots(2, 2) #create chart in each subplot sns. boxplot(data=df, x='team', y='points', ax=axes[0,0]) sns.

How do you plot multiple columns in Seaborn?

As we have used the groupby function to show the barplot multiple columns. Just specify the three parameters x, y, and hue to generate the bar plot in multiple columns. So, let's begin with adding the python modules for plotting the multiple bars of the plot.

How do you use FacetGrid in Seaborn?

Plotting Small Multiples of Data SubsetsFacetGrid object takes a dataframe as input and the names of the variables that will form the row, column, or hue dimensions of the grid. The variables should be categorical and the data at each level of the variable will be used for a facet along that axis.

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.

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 create a facetgrid using Seaborn tips?

We will use the built-in “tips” dataset of seaborn. FacetGrid object is initialized by passing a dataframe and name of variables to create the structure of axes. The variables used to initialize FacetGrid object needs to be categorical or discrete. The grid structure is created according to the number of categories.

How do I plot a subset of a facetgrid?

Then one or more plotting functions can be applied to each subset by calling FacetGrid.map () or FacetGrid.map_dataframe (). Finally, the plot can be tweaked with other methods to do things like change the axis labels, use different ticks, or add a legend. See the detailed code examples below for more information.


1 Answers

You can use the inbuilt plot method of your pandas dataframe and the option subplots=True to plot by column

from io import StringIO
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('seaborn')

# Here I read your example data in
df = pd.read_fwf(StringIO("""
    X              Y          Z
0   1.000000    1.000000    1.014925    
1   1.000000    1.000000    1.000000
"""), header=1, index_col=0)

# Plotting as desired
df.plot.hist(subplots=True, legend=False)

enter image description here

df.plot takes lots of other arguments to allow you to easily alter your plot, eg

df.plot.hist(subplots=True, legend=True, layout=(1, 3))

enter image description here

like image 177
jwalton Avatar answered Oct 10 '22 20:10

jwalton