import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
15 Data Visualisation with matplotlib and seaborn
- Be able to visualise biological data using Python
- Create a plots using ‘matplotlib’ and ‘seaborn’
- Customize the plots with titles, labels, and legends
15.1 Using matplotlib and seaborn
matplotlib
is a widely used plotting library in Python, providing a wide range of capabilities
seaborn
is built on top of matplotlib
and offers a high-level interface. It simplifies the process of creating plots and enhances their aesthetics.
Import Required Libraries
!pip install seaborn
Example Dataset
For the example:
42)
np.random.seed(= {
data 'height': np.random.normal(160, 10, 100), # Normal distribution (mean=160, std=10)
'weight': np.random.normal(65, 15, 100) # Normal distribution (mean=65, std=15)
}
= pd.DataFrame(data) df
Scatter Plot
Create a scatter plot to visualize the relationship between height and weight.
=(8, 5))
plt.figure(figsize'height'], df['weight'], color='blue', alpha=0.6)
plt.scatter(df['Scatter Plot of Height vs. Weight')
plt.title('Height (cm)')
plt.xlabel('Weight (kg)')
plt.ylabel(
plt.grid() plt.show()
Histogram
Create a histogram to show the distribution of weights.
=(8, 5))
plt.figure(figsize'weight'], bins=15, color='green', edgecolor='black', alpha=0.7)
plt.hist(df['Histogram of Weights')
plt.title('Weight (kg)')
plt.xlabel('Frequency')
plt.ylabel(
plt.grid() plt.show()
Boxplot with Seaborn
Use Seaborn to create a boxplot to visualize the distribution of heights.
=(8, 5))
plt.figure(figsize=df['height'], color='orange')
sns.boxplot(y'Boxplot of Heights')
plt.title('Height (cm)')
plt.ylabel(
plt.grid() plt.show()
15.2 Documentation
See the documentation for ‘seaborn’ here:
https://seaborn.pydata.org/
Waskom, M. L., (2021). seaborn: statistical data visualization. Journal of Open Source Software, 6(60), 3021, https://doi.org/10.21105/joss.03021.
J. D. Hunter, “Matplotlib: A 2D Graphics Environment”, Computing in Science & Engineering, vol. 9, no. 3, pp. 90-95, 2007.
See the documentation for ‘matplotlib’ here:
https://matplotlib.org/stable/index.html
15.3 Summary
Now you have all the tools you need!
Tomorrow we will use all you have learnt so far
- Plotting and visualisation is key to understanding and working with data
- ‘matplotlib’ and ‘seaborn’ can be used to plot your data in Python