Imagine I have a dataframe with 9 columns. I want to be able to achieve the same effect as df.hist(), but with sns.distplot().
In other words, I want to be able to plot the sns.distplot() for each column in the dataframe in a visualization of 3 rows and 3 columns where each sub figure represents the unique sns.distplot() of each column for the total number of columns in the dataframe.
I experimented a bit with using a for loop over axes and columns for the dataframe, but I'm only able to achieve results for specifying columns. I'm not sure how to represent the code to work for rows and columns.
I also looked into sns.FacetGrid, but I'm not sure how to go about solving this problem using FacetGrid.
I find the df.hist() function to exactly what I want, but I want to be able to do it with the sns.distplot for all the columns in that same representation as the output of df.hist().
If it helps to put the context of the dataframe, I'm essentially reading Google Colab's training and testing sets for the California Housing Dataset which contains all the columns except for the ocean_proximity. If you want to help me figure out this problem using that dataset, please get it from Kaggle and drop the ocean_proximity column.
My approach for 9 columns:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv('housing.csv')
df.drop('ocean_proximity', axis=1, inplace=True)
fig, axes = plt.subplots(ncols=len(df.columns), figsize=(30,15))
for ax, col in zip(axes, df.columns):
sns.distplot(df[col], ax=ax)
plt.tight_layout()
plt.show()

所有评论(0)