codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

54 / 68 topics
51NumPy Tutorial52Pandas Tutorial53SciPy Tutorial54Matplotlib & Seaborn Basics55Machine Learning Basics (Stats & Data Distribution)56Linear & Polynomial Regression57Classification & Clustering (Decision Trees, K-Means)58TensorFlow & PyTorch Basics
Tutorials/Python Programming/Matplotlib & Seaborn Basics
🐍Python Programming

Matplotlib & Seaborn Basics

Updated 2026-05-15
30 min read

Matplotlib & Seaborn Basics

Data visualization is a crucial aspect of data science and machine learning. It helps us understand patterns, trends, and relationships in data more effectively. In this tutorial, we'll explore the basics of two popular Python libraries for creating visualizations: Matplotlib and Seaborn.

Introduction

Matplotlib is a comprehensive library for creating static, interactive, and animated visualizations in Python. It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits like Tkinter, wxPython, Qt, or GTK.

Seaborn, on the other hand, is built on top of Matplotlib and simplifies the creation of attractive and informative statistical graphics. It provides a high-level interface for drawing attractive and informative statistical graphics.

Core Content

Matplotlib Basics

Line Plots

A line plot is one of the simplest types of plots. It displays data points connected by straight lines, which can help visualize trends over time or ordered categories.

line_plot.py
1import matplotlib.pyplot as plt
2
3# Sample data
4x = [1, 2, 3, 4, 5]
5y = [2, 3, 5, 7, 11]
6
7# Create a line plot
8plt.plot(x, y)
9
10# Add title and labels
11plt.title('Simple Line Plot')
12plt.xlabel('X-axis Label')
13plt.ylabel('Y-axis Label')
14
15# Show the plot
16plt.show()
Output
[Graph of a simple line plot with x values 1 to 5 and y values 2, 3, 5, 7, 11]

Bar Charts

Bar charts are useful for comparing quantities across different categories. They can be vertical or horizontal.

bar_chart.py
1import matplotlib.pyplot as plt
2
3# Sample data
4categories = ['A', 'B', 'C', 'D']
5values = [10, 15, 7, 10]
6
7# Create a bar chart
8plt.bar(categories, values)
9
10# Add title and labels
11plt.title('Simple Bar Chart')
12plt.xlabel('Categories')
13plt.ylabel('Values')
14
15# Show the plot
16plt.show()
Output
[Graph of a simple bar chart with categories A, B, C, D and corresponding values 10, 15, 7, 10]

Scatter Plots

Scatter plots are used to display the relationship between two variables. Each point represents an observation.

scatter_plot.py
1import matplotlib.pyplot as plt
2
3# Sample data
4x = [1, 2, 3, 4, 5]
5y = [2, 3, 5, 7, 11]
6
7# Create a scatter plot
8plt.scatter(x, y)
9
10# Add title and labels
11plt.title('Simple Scatter Plot')
12plt.xlabel('X-axis Label')
13plt.ylabel('Y-axis Label')
14
15# Show the plot
16plt.show()
Output
[Graph of a simple scatter plot with x values 1 to 5 and y values 2, 3, 5, 7, 11]

Histograms

Histograms are used to show the distribution of a single variable. They divide the data into bins and display the frequency of observations in each bin.

histogram.py
1import matplotlib.pyplot as plt
2
3# Sample data
4data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
5
6# Create a histogram
7plt.hist(data, bins=5)
8
9# Add title and labels
10plt.title('Simple Histogram')
11plt.xlabel('Data Values')
12plt.ylabel('Frequency')
13
14# Show the plot
15plt.show()
Output
[Graph of a simple histogram with data values 1 to 4 and corresponding frequencies]

Pie Charts

Pie charts are used to show the proportion of each category in a whole. They are useful for displaying parts-to-whole relationships.

pie_chart.py
1import matplotlib.pyplot as plt
2
3# Sample data
4labels = ['A', 'B', 'C', 'D']
5sizes = [15, 30, 45, 10]
6
7# Create a pie chart
8plt.pie(sizes, labels=labels, autopct='%1.1f%%')
9
10# Add title
11plt.title('Simple Pie Chart')
12
13# Show the plot
14plt.show()
Output
[Graph of a simple pie chart with categories A, B, C, D and corresponding sizes]

Subplots

Subplots allow you to create multiple plots in a single figure. This is useful for comparing different datasets or variables.

subplots.py
1import matplotlib.pyplot as plt
2
3# Sample data
4x = [1, 2, 3, 4, 5]
5y1 = [2, 3, 5, 7, 11]
6y2 = [1, 4, 9, 16, 25]
7
8# Create a figure and subplots
9fig, axs = plt.subplots(1, 2)
10
11# Plot on the first subplot
12axs[0].plot(x, y1)
13axs[0].set_title('Line Plot')
14axs[0].set_xlabel('X-axis Label')
15axs[0].set_ylabel('Y-axis Label')
16
17# Plot on the second subplot
18axs[1].bar(x, y2)
19axs[1].set_title('Bar Chart')
20axs[1].set_xlabel('X-axis Label')
21axs[1].set_ylabel('Y-axis Label')
22
23# Show the figure
24plt.show()
Output
[Graph with two subplots: a line plot on the left and a bar chart on the right]

Labels, Titles, and Legends

Adding labels, titles, and legends to your plots enhances their readability and clarity.

labels_titles_legends.py
1import matplotlib.pyplot as plt
2
3# Sample data
4x = [1, 2, 3, 4, 5]
5y1 = [2, 3, 5, 7, 11]
6y2 = [1, 4, 9, 16, 25]
7
8# Create a line plot
9plt.plot(x, y1, label='Line 1')
10plt.plot(x, y2, label='Line 2')
11
12# Add title and labels
13plt.title('Line Plot with Labels and Legend')
14plt.xlabel('X-axis Label')
15plt.ylabel('Y-axis Label')
16
17# Add legend
18plt.legend()
19
20# Show the plot
21plt.show()
Output
[Graph of a line plot with two lines labeled 'Line 1' and 'Line 2', title, labels, and legend]

Seaborn Basics

Heatmaps

Heatmaps are used to visualize data in a matrix form. They color-code the cells based on their values, making it easy to identify patterns.

heatmap.py
1import seaborn as sns
2import matplotlib.pyplot as plt
3import numpy as np
4
5# Sample data
6data = np.random.rand(10, 12)
7
8# Create a heatmap
9sns.heatmap(data, annot=True, cmap='YlGnBu')
10
11# Add title and labels
12plt.title('Simple Heatmap')
13plt.xlabel('Columns')
14plt.ylabel('Rows')
15
16# Show the plot
17plt.show()
Output
[Graph of a simple heatmap with annotated values]

Pair Plots

Pair plots are used to visualize pairwise relationships in a dataset. They create a grid of scatter plots for each pair of variables.

pair_plot.py
1import seaborn as sns
2import matplotlib.pyplot as plt
3
4# Load sample data
5iris = sns.load_dataset('iris')
6
7# Create a pair plot
8sns.pairplot(iris, hue='species', markers=["o", "s", "D"])
9
10# Add title
11plt.suptitle('Pair Plot of Iris Dataset', y=1.02)
12
13# Show the plot
14plt.show()
Output
[Graph of a pair plot with scatter plots for each pair of variables in the iris dataset]

Distribution Plots

Distribution plots are used to visualize the distribution of a single variable. Seaborn provides several types of distribution plots, such as histograms and KDE plots.

distribution_plot.py
1import seaborn as sns
2import matplotlib.pyplot as plt
3
4# Load sample data
5tips = sns.load_dataset('tips')
6
7# Create a distribution plot
8sns.histplot(tips['total_bill'], kde=True)
9
10# Add title and labels
11plt.title('Distribution Plot of Total Bill')
12plt.xlabel('Total Bill')
13plt.ylabel('Frequency')
14
15# Show the plot
16plt.show()
Output
[Graph of a distribution plot with histogram and KDE for total bill amounts]

Practical Example

Let's create a practical example that combines Matplotlib and Seaborn to visualize a dataset. We'll use the famous Iris dataset, which contains measurements of iris flowers.

practical_example.py
1import seaborn as sns
2import matplotlib.pyplot as plt
3
4# Load sample data
5iris = sns.load_dataset('iris')
6
7# Create a pair plot
8sns.pairplot(iris, hue='species', markers=["o", "s", "D"])
9
10# Add title
11plt.suptitle('Pair Plot of Iris Dataset', y=1.02)
12
13# Show the plot
14plt.show()
Output
[Graph of a pair plot with scatter plots for each pair of variables in the iris dataset]

Summary

In this tutorial, we covered the basics of Matplotlib and Seaborn, two powerful libraries for data visualization in Python. We learned how to create various types of plots, including line plots, bar charts, scatter plots, histograms, pie charts, subplots, heatmaps, pair plots, and distribution plots. These tools are essential for exploratory data analysis and communicating insights effectively.

What's Next?

Now that you have a solid understanding of Matplotlib and Seaborn, the next step is to dive into machine learning basics, focusing on statistics and data distributions. This will provide you with the foundational knowledge needed to build predictive models and analyze data more deeply.


PreviousSciPy TutorialNext Machine Learning Basics (Stats & Data Distribution)

Recommended Gear

SciPy TutorialMachine Learning Basics (Stats & Data Distribution)