Python Matplotlib Tutorial for Beginners (2026):
Python Matplotlib Tutorial for Beginners (2026)
What is Matplotlib in Python?
Matplotlib is one of the most popular Python libraries used
for data visualization. It helps convert numbers and data into graphs
and charts that are easy to understand.
Instead of reading thousands of rows of data in a table, you
can create colorful charts that show patterns, trends, comparisons, and
distributions in just a few seconds.
For example, if a company wants to know its monthly sales, a
line chart created with Matplotlib makes it much easier to understand than
reading a spreadsheet.
Matplotlib is widely used in:
- Data
Science
- Data
Analysis
- Machine
Learning
- Artificial
Intelligence
- Business
Analytics
- Finance
- Healthcare
- Research
- Education
It works perfectly with NumPy and Pandas,
making it the first choice for creating visual reports.
Why Use Matplotlib?
Matplotlib provides many benefits.
- Easy
to learn
- Free
and Open Source
- Creates
professional-quality charts
- Supports
many chart types
- Works
with NumPy and Pandas
- Used
in Data Science and Machine Learning
- Highly
customizable
- Can
save charts as PNG, JPG, PDF, and SVG
Install Matplotlib
pip install matplotlib
Import the library:
import matplotlib.pyplot as plt
pyplot is the module used to create graphs.
Flow of Matplotlib
Start
│
▼
Import matplotlib.pyplot
│
▼
Prepare Data
│
▼
Choose Chart Type
│
▼
Customize Chart
│
▼
Display Chart
│
▼
End
What is a Line Chart?
A Line Chart connects data points using straight
lines.
It is mainly used to show changes over time.
For example,
- Monthly
Sales
- Temperature
Changes
- Website
Visitors
- Stock
Prices
- Student
Progress
Whenever your data has a sequence like days, months, or
years, a line chart is usually the best choice.
Line Chart Syntax
import matplotlib.pyplot as plt
y = [20,35,30,40]
plt.show()
Example
import matplotlib.pyplot as plt
sales = [100,120,150,130,180]
Output
Sales
^
| *
| *
| *
| *
| *
+----------------------> Months
Advantages of Line Chart
- Shows
trends clearly
- Easy
to compare values
- Best
for time-series data
- Simple
to understand
- Used
in business reports
Where Line Charts are Used
- Stock
Market
- Sales
Reports
- Weather
Forecast
- Student
Marks
- Website
Analytics
What is a Bar Chart?
A Bar Chart represents data using rectangular bars.
Each bar represents one category.
The taller the bar, the larger the value.
Bar charts are mainly used to compare different groups.
Example:
- Product
Sales
- Student
Marks
- Company
Revenue
- Population
of Cities
Bar Chart Syntax
plt.bar(x, y)
Example
import matplotlib.pyplot as plt
marks = [85,70,95,80]
Output
Marks
95 | █
85 | █ █
80 | █ █ █
70 | █ █
█ █
+------------------
A B C D
Advantages
- Easy
comparison
- Attractive
visualization
- Perfect
for categories
- Easy
to read
- Business-friendly
Applications
- Sales
Comparison
- Election
Results
- Product
Performance
- Student
Results
- Survey
Reports
What is a Pie Chart?
A Pie Chart is a circular chart divided into slices.
Each slice represents a percentage of the whole.
It is best when you want to show how different parts
contribute to one total.
Example
Company Budget
- Marketing
- Salary
- Rent
- Transport
Together these make 100%.
Pie Chart Syntax
plt.pie(values)
What is a Pie Chart?
Example
import matplotlib.pyplot as plt
What is a Pie Chart?
Output
Food
◜──────◝
Rent │ │ Travel
◟──────◞
Shopping
What is a Pie Chart?
Advantages
- Shows
percentage distribution
- Attractive
presentation
- Easy
to understand
- Great
for reports
- Good
for presentations
What is a Pie Chart?
Applications
- Budget
Analysis
- Market
Share
- Population
Distribution
- Expense
Tracking
- Election
Vote Share
What is Histogram?
A Histogram is a graph that shows the distribution of
continuous numerical data.
Although it looks similar to a bar chart, it is different.
In a histogram:
- Bars
touch each other.
- Data
is divided into intervals called bins.
- It
shows how frequently values occur.
Student marks
10-20
Instead of showing individual students, it shows how many
students fall within each range.
Histogram Syntax
plt.hist(data)
What is Histogram? Example
import matplotlib.pyplot as plt
What is Histogram? Output
Frequency
5 | ███
4 | █████
3 | ███████
2 | ███████
1 | ███████
+-------------------->
Marks
What is Histogram?
Advantages
- Shows
data distribution
- Detects
outliers
- Easy
to analyze frequency
- Useful
in statistics
- Helps
understand patterns
What is Histogram?
Applications
- Exam
Result Analysis
- Salary
Distribution
- Age
Distribution
- Medical
Research
- Machine
Learning
Difference Between Line Chart, Bar Chart, Pie Chart and
Histogram
|
Chart |
Purpose |
Best Used For |
|
Line Chart |
Show trends |
Time-based data |
|
Bar Chart |
Compare categories |
Product sales, marks |
|
Pie Chart |
Show percentages |
Budget, market share |
|
Histogram |
Show frequency distribution |
Continuous numerical data |
Best Practices
- Choose
the correct chart for your data.
- Add
meaningful titles.
- Label
the X-axis and Y-axis.
- Use
readable colors and fonts.
- Avoid
cluttering charts with too much information.
- Keep
charts simple and easy to understand.
Real-Life Examples
- Line
Chart: Monthly electricity usage.
- Bar
Chart: Comparing sales of different products.
- Pie
Chart: Family monthly expenses.
- Histogram:
Distribution of student exam scores.
Frequently Asked Questions (FAQs)
1. What is Matplotlib in Python?
Matplotlib is a Python library used to create charts, graphs, and data visualizations.
2. Which module is used most often in Matplotlib?
The matplotlib.pyplot module is the most commonly used for plotting charts.
3. When should I use a Line Chart?
Use a line chart to show trends or changes over time, such as monthly sales or yearly profits.
4. What is the difference between a Bar Chart and a
Histogram?
A bar chart compares separate categories, while a histogram shows the frequency distribution of continuous numerical data using touching bars.
5. When should I use a Pie Chart?
Use a pie chart to show how different parts contribute to a whole, such as a budget or market share.
6. What are bins in a Histogram?
Bins are intervals or ranges that group numerical data for frequency analysis.
7. Is Matplotlib free to use?
Yes, Matplotlib is a free and open-source library.
8. Can Matplotlib work with Pandas?
Yes. Matplotlib integrates seamlessly with Pandas DataFrames
and Series for creating visualizations.
Python Interview Questions
Beginner
- What
is Matplotlib?
- Why
is Matplotlib used in Python?
- What
is pyplot?
- How
do you install Matplotlib?
- What
is the purpose of plt.show()?
Intermediate
- What
is the difference between plt.plot() and plt.bar()?
- Explain
the difference between a bar chart and a histogram.
- How
do you add a title and axis labels to a chart?
- What
is the purpose of the autopct parameter in a pie chart?
- How
do you customize the appearance of a Matplotlib chart?
Advanced
- How
does Matplotlib integrate with Pandas and NumPy?
- What
are subplots in Matplotlib?
- How
can you save a chart as an image using plt.savefig()?
- What
is the role of bins in a histogram?
- Which
chart type would you choose to visualize time-series data and why?
Comments
Post a Comment