Skip to Content
Course content

8.3.1 Plotting with Matplotlib and Seaborn

Matplotlib and Seaborn are two of the most popular libraries in Python for creating a variety of static, animated, and interactive plots. These libraries provide powerful and flexible tools for visualizing data, helping you understand trends, relationships, and patterns.

1. Matplotlib: Overview and Plot Types

Matplotlib is the foundation for most plotting libraries in Python and is highly customizable. It is great for creating simple visualizations like line charts, bar charts, histograms, scatter plots, and more.

a. Line Plot

A line plot is often used to show trends over a continuous variable, such as time or numerical sequences.

import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

# Create a line plot
plt.plot(x, y)

# Add labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Line Plot Example')

# Display the plot
plt.show()

b. Bar Plot

Bar plots are used for comparing different categories by displaying bars of varying heights.

# Data
categories = ['A', 'B', 'C', 'D']
values = [5, 7, 3, 9]

# Create a bar plot
plt.bar(categories, values)

# Add labels and title
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Plot Example')

# Display the plot
plt.show()

c. Scatter Plot

A scatter plot shows the relationship between two continuous variables. It's helpful for identifying correlations or trends.

# Data
x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]

# Create a scatter plot
plt.scatter(x, y)

# Add labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Scatter Plot Example')

# Display the plot
plt.show()

2. Seaborn: Overview and Plot Types

Seaborn is built on top of Matplotlib and offers an easier interface for creating beautiful and complex visualizations. It is especially useful for statistical plotting.

a. Box Plot

Box plots show the distribution of a dataset based on a five-number summary: minimum, first quartile, median, third quartile, and maximum.

import seaborn as sns

# Data
data = [1, 2, 5, 6, 7, 8, 9, 10, 15, 20, 22, 25]

# Create a box plot
sns.boxplot(data=data)

# Add title
plt.title('Box Plot Example')

# Display the plot
plt.show()

b. Heatmap

Heatmaps represent data values in matrix form, with color coding to visualize the intensity of values across different dimensions. Seaborn makes it easy to plot heatmaps, especially useful for correlation matrices or data tables.

import numpy as np

# Data
data = np.random.rand(10, 12)

# Create a heatmap
sns.heatmap(data, annot=True, cmap='coolwarm')

# Add title
plt.title('Heatmap Example')

# Display the plot
plt.show()

c. Pair Plot

Pair plots are used to visualize relationships between several variables by plotting multiple scatter plots and histograms for each pair of variables.

# Sample data from Seaborn's built-in dataset
import seaborn as sns
data = sns.load_dataset('iris')

# Create a pair plot
sns.pairplot(data)

# Display the plot
plt.show()

3. Comparison: Matplotlib vs Seaborn

While Matplotlib is highly customizable and versatile, Seaborn makes it easier to create aesthetically pleasing, informative plots with fewer lines of code. Seaborn also provides specialized plots like violin plots, heatmaps, and pair plots, which are not directly available in Matplotlib.

Feature Matplotlib Seaborn
Customization Highly customizable (color, style, labels, etc.) Provides built-in themes and styles
Ease of Use More code needed for complex plots Simpler syntax for advanced plots
Plot Types Standard plots like line, bar, scatter, etc. Adds statistical plots like violin, pair, heatmap
Integration Works well with many libraries Works well with pandas and Matplotlib

4. Interactive Visualization with Seaborn and Matplotlib

While Matplotlib and Seaborn generate static images, interactive visualizations can be achieved by combining these libraries with others like Plotly or Bokeh. Plotly allows you to add hover effects, zooming, and other interactions to your plots.

5. Customizing Plots

a. Changing Plot Style

You can change the overall style of a plot in Matplotlib or Seaborn by using predefined styles.

# Seaborn style
sns.set(style="whitegrid")

# Create a plot
plt.plot([1, 2, 3], [1, 4, 9])
plt.show()

b. Adding Legends, Titles, and Labels

Both libraries allow adding titles, axis labels, and legends to plots.

# Adding labels and title in Matplotlib
plt.plot([1, 2, 3], [1, 4, 9])
plt.title('My Plot Title')
plt.xlabel('X-axis Label')
plt.ylabel('Y-axis Label')
plt.legend(['Line 1'])
plt.show()

6. Conclusion

Matplotlib and Seaborn are two of the most powerful Python libraries for data visualization. Matplotlib is highly flexible and can create virtually any type of plot, but it requires more lines of code and customization. Seaborn simplifies the creation of more complex, aesthetically pleasing plots, especially useful for statistical data analysis.

  • Matplotlib is excellent for basic plots and full control over plot customization.
  • Seaborn is great for quick, attractive statistical plots with less code.

Mastering both libraries will enable you to create highly effective visualizations to interpret and present data clearly and effectively.

Commenting is not enabled on this course.