Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Data Visualization with Matplotlib & Seaborn

1. Introduction

Data visualization is a crucial aspect of data science, allowing us to interpret complex data sets visually. This lesson will cover the basics of two popular libraries: Matplotlib and Seaborn.

2. Matplotlib Basics

Matplotlib is a widely used library for creating static, animated, and interactive visualizations in Python.

2.1 Installation

pip install matplotlib

2.2 Basic Plotting

Creating a simple line plot:

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

plt.plot(x, y)
plt.title('Simple Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()

2.3 Customizing Plots

Customizations can include changing colors, line styles, and adding legends:

plt.plot(x, y, color='red', linestyle='--', marker='o', label='Data Points')
plt.title('Customized Line Plot')
plt.legend()
plt.show()

3. Seaborn Basics

Seaborn is built on top of Matplotlib and provides a high-level interface for drawing attractive statistical graphics.

3.1 Installation

pip install seaborn

3.2 Basic Plotting

Creating a simple scatter plot:

import seaborn as sns

# Sample data
tips = sns.load_dataset('tips')

sns.scatterplot(data=tips, x='total_bill', y='tip', hue='day')
plt.title('Scatter Plot of Tips')
plt.show()

3.3 Advanced Visualizations

Seaborn makes it easy to create complex visualizations:

sns.boxplot(data=tips, x='day', y='total_bill', palette='Set3')
plt.title('Box Plot of Total Bills by Day')
plt.show()

4. Best Practices

Always label your axes and provide titles for clarity.
  • Choose the right type of visualization for your data.
  • Use color effectively to enhance readability.
  • Avoid clutter; keep visualizations simple.

5. FAQ

What is the difference between Matplotlib and Seaborn?

Matplotlib is a low-level library for creating basic plots, while Seaborn is a high-level interface that offers more appealing and complex visualizations with less code.

Can I use Matplotlib and Seaborn together?

Yes, Seaborn is built on top of Matplotlib, so you can use both in your visualizations.