Visualization with Seaborn
Seaborn is a python library for generating statistical graphs. It is build on top of matplotlib and integrates closely with pandas. It helps us in data exploration and understanding its distribution.
Count Plot : A count plot is similar to histogram or a bar plot. It shows the number of occurrences of an item based on a certain type of category. It can be thought of as a histogram across a categorical, rather than quantitative, variable.
#importing library
import seaborn as sns#setting theme
sns.set_theme(style="darkgrid")#importing dataset
titanic = sns.load_dataset("titanic")#plotting chart
ax = sns.countplot(x="class", data=titanic, hue="sex", palette="Blues")plt.show()
Histogram : It a graphical representation that organizes a group of data points into user-specified buckets or ranges.
#importing library
import seaborn as sns#setting theme
sns.set_theme(style="darkgrid")#importing dataset
titanic = sns.load_dataset("titanic")#plotting chart
sns.histplot(data=titanic, x="age", kde=True, bins = 5, hue = 'sex')plt.show()
Box Plot : It displays the five-number summary of a set of data i.e. the minimum, first quartile, median, third quartile, and maximum.
#importing library
import seaborn as sns#setting theme
sns.set_theme(style="darkgrid")#importing dataset
titanic = sns.load_dataset("titanic")#plotting chart
sns.boxplot(data=titanic, x="class", y='age', hue ='sex', palette="Greens")plt.show()
Scatter Plot : A scatter plot uses Cartesian coordinates to display relationship for two variables for a set of data.
#importing library
import seaborn as sns#setting theme
sns.set_theme(style="darkgrid")#importing dataset
titanic = sns.load_dataset("titanic")#plotting chart
sns.scatterplot(data=titanic, x="age", y='fare', hue ='alive', palette="Greens")plt.show()
Joint Plot : It comprises three plots. One bivariate graph showing two variables, and two univariate graphs for each of the variables individually. Below chart will make it more clear.
#importing library
import seaborn as sns#setting theme
sns.set_theme(style="darkgrid")#importing dataset
titanic = sns.load_dataset("titanic")#plotting chart
sns.jointplot(data=titanic, x="age", y='fare', hue ='alive', palette="Greens")plt.show()
We can also add a linear regression fit to the joint plot by adding value to “kind” parameter.
#importing library
import seaborn as sns#setting theme
sns.set_theme(style="darkgrid")#importing dataset
titanic = sns.load_dataset("titanic")#plotting chart
sns.jointplot(data=titanic, x="age", y='fare', palette="Greens", kind = "reg")plt.show()
Pair Plot : It is useful in plotting pairwise relationships in a dataset.
#importing library
import seaborn as sns#setting theme
sns.set_theme(style="darkgrid")#importing dataset
titanic = sns.load_dataset("titanic")#plotting chart
sns.pairplot(titanic, hue="alive")plt.show()