Blog

Practice makes perfect.

Data visualization with seaborn matplotlib in Python

Zhijun / 2022-11-05


Data visualization Examples : The weight of chicks from different months and feed diet

Import packages

import os as os
from stat import FILE_ATTRIBUTE_SPARSE_FILE
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

Line plot

Chick_data.head()
##    Unnamed: 0  weight  Time  Chick  Diet
## 0           1      42     0      1     1
## 1           2      51     2      1     1
## 2           3      59     4      1     1
## 3           4      64     6      1     1
## 4           5      76     8      1     1
Chick_data = Chick_data.iloc[: , 1:]
Chick_data.head()
##    weight  Time  Chick  Diet
## 0      42     0      1     1
## 1      51     2      1     1
## 2      59     4      1     1
## 3      64     6      1     1
## 4      76     8      1     1
plt.figure(figsize=(12,9))
sns.lineplot(data=Chick_data, x="Time", y="weight", hue="Diet", style="Diet")
plt.legend(title='Diet', fontsize=18)
plt.xlabel('Time', fontsize=18);
plt.ylabel('Weight', fontsize=18);
plt.title('Plot of Chick weight and Time', fontsize=24)

plt.show()

Violin plot

# set a grey background
plt.figure(figsize=(12,9))
sns.set(style="darkgrid")
sns.violinplot( x=Chick_data["Diet"], y=Chick_data["weight"])
plt.xlabel('Diet', fontsize=20);
plt.ylabel('Weight', fontsize=20);
plt.title('Violin plot of Chick weight and Diet', fontsize=28)

plt.show()

Density chart

# set a grey background
plt.figure(figsize=(12,9))
sns.set(style="darkgrid")
sns.kdeplot(data=Chick_data, x="weight", hue="Diet", cut=0, fill=True, common_norm=False, alpha=1)
plt.xlabel('Weight', fontsize=20);
plt.ylabel('Density', fontsize=20);
plt.title('Density chart of Chick weight and Diet', fontsize=28)

plt.show()

Scatter plot

plt.figure(figsize=(12,9))
sns.lmplot(data = Fruits, x="Glucose", y="Fiber", fit_reg=False, hue='Group', legend=False, palette="Set2")

plt.xlabel('Glucose', fontsize=20);
plt.ylabel('Fiber', fontsize=20);
plt.title('Scatter plot of fruits composition', fontsize=20) 
plt.legend(loc='best')
plt.tight_layout()
plt.show()

Histogram

sns.set_theme(context='notebook', style='darkgrid', palette='deep', font='sans-serif', font_scale=1, color_codes=True)
plt.figure(figsize=(12,9))
sns.histplot(data=Fruits, x="Vitamin", color="skyblue", label="Vitamin", kde=True)
sns.histplot(data=Fruits, x="Glucose", color="red", label="Glucose", kde=True)
sns.histplot(data=Fruits, x="Fiber", color="yellow", label="Fiber", kde=True).set(title='Histogram of fruits composition')
plt.xlabel('Fruits compositions', fontsize=20)
plt.ylabel('Numbers', fontsize=20)
plt.title('Histogram of fruits composition', fontsize=26)

plt.legend(loc='best', fontsize=18)
plt.tight_layout()
plt.show()

Box plot

sns.set(style="darkgrid")
sns.set_theme(context='notebook', style='darkgrid', palette='deep', font='sans-serif', font_scale=1, color_codes=True)
plt.figure(figsize=(12,9))
sns.boxplot(x=Fruits["Group"], y=Fruits["Glucose"]).set(title='Box plot of fruits composition')
plt.xlabel('Group', fontsize=20);
plt.ylabel('Glucose', fontsize=20);
plt.title('Box plot of fruits composition', fontsize=26) 
plt.legend(loc='best', fontsize=18)
plt.tight_layout()
plt.show()