📅  最后修改于: 2023-12-03 15:20:03.127000             🧑  作者: Mango
seaborn
is a popular data visualization library in Python designed to work with pandas
dataframes. One of the most commonly used function in seaborn
is pairplot()
which allows us to plot pairs of variables in a dataset.
seaborn.pairplot(data, hue=None, hue_order=None, palette=None, diag_kind='auto', diag_kws=None, plot_kws=None, size=None)
import seaborn as sns
import pandas as pd
# Load iris dataset
iris = sns.load_dataset("iris")
# Plot the pairwise relationships between all variables in the dataset
sns.pairplot(iris)
Suppose we have a pandas
DataFrame df
as shown below:
import pandas as pd
import seaborn as sns
df = sns.load_dataset('titanic')
df.head()
| | survived | pclass | sex | age | sibsp | parch | fare | embarked | class | who | adult_male | deck | embark_town | alive | alone | |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---| | 0 | 0 | 3 | male | 22.0 | 1 | 0 | 7.2500 | S | Third | man | True | NaN | Southampton | no | False | | 1 | 1 | 1 | female | 38.0 | 1 | 0 | 71.2833 | C | First | woman | False | C | Cherbourg | yes | False | | 2 | 1 | 3 | female | 26.0 | 0 | 0 | 7.9250 | S | Third | woman | False | NaN | Southampton | yes | True | | 3 | 1 | 1 | female | 35.0 | 1 | 0 | 53.1000 | S | First | woman | False | C | Southampton | yes | False | | 4 | 0 | 3 | male | 35.0 | 0 | 0 | 8.0500 | S | Third | man | True | NaN | Southampton | no | True |
We can use pairplot()
to visualize the pairwise relationships between numerical variables in the dataset.
sns.pairplot(df[['survived', 'age', 'fare']])
We can also use hue
parameter to add a categorical variable to our visualization.
sns.pairplot(df[['survived', 'age', 'fare', 'sex']], hue='sex')
In this article, we learned about seaborn
pairplot()
function and how to use it to visualize pairwise relationships between variables in a dataset. pairplot()
is a powerful and versatile tool that can help us explore different aspects of our data.