Select a Subtopic
Day 17: Data Visualization with Matplotlib and Seaborn
Let's focus on Day 17, where we dive deep into Data Visualization with Matplotlib and Seaborn! I'll guide you through the concepts, and we'll work together interactively to create some visualizations.
- Advanced plots with Seaborn
- Customizing plots
Step 1: Installing the Required Libraries
Before we begin, you'll need to have Matplotlib and Seaborn installed. Run the following commands to install them if you haven't already:
1
pip install matplotlib seaborn
Step 2: Introduction to Matplotlib
Matplotlib is the foundational library for plotting in Python. It provides a wide variety of visualizations. It's often used for quick and simple plots.
Creating a Line Plot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import matplotlib.pyplot as plt
# Sample Data
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
# Creating the plot
plt.plot(x, y)
# Adding title and labels
plt.title('Simple Line Plot')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
# Display the plot
plt.show()
Creating a Bar Chart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Sample Data
categories = ['A', 'B', 'C', 'D']
values = [10, 24, 18, 30]
# Creating a bar chart
plt.bar(categories, values)
# Adding title and labels
plt.title('Simple Bar Chart')
plt.xlabel('Categories')
plt.ylabel('Values')
# Display the plot
plt.show()
Creating a Pie Chart
1
2
3
4
5
6
7
8
9
10
11
12
# Sample Data
labels = ['Red', 'Blue', 'Green', 'Yellow']
sizes = [15, 30, 45, 10]
# Creating a pie chart
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
# Adding title
plt.title('Simple Pie Chart')
# Display the plot
plt.show()
Step 5: Introduction to Seaborn
Seaborn is built on top of Matplotlib and offers easier-to-use functions for more advanced and beautiful plots. Let’s use Seaborn to create a Heatmap.
Creating a Heatmap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import seaborn as sns
import numpy as np
# Creating a 2D dataset
data = np.random.rand(10, 12) # Random values in a 10x12 array
# Creating a heatmap
sns.heatmap(data, annot=True)
# Adding title
plt.title('Heatmap Example')
# Display the plot
plt.show()
Creating a Pairplot
1
2
3
4
5
6
7
8
# Load an example dataset from Seaborn
iris = sns.load_dataset('iris')
# Create a pairplot
sns.pairplot(iris, hue='species')
# Display the plot
plt.show()
Interactive Exercise: Customizing Your Plots
Now, let's practice customizing these plots!
- 1. Line Plot Customization: Change the color of the line to red and make it dashed. Add gridlines.
- 2. Bar Chart Customization: Change the color of the bars to green and add edge color.
- 3. Pie Chart Customization: Explode the Blue slice slightly.
Make these changes in your code and let me know how it goes. If you need help, just ask, and I’ll guide you!