Learn JSX Expressions and Variables in React with Examples 2026
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:
It works perfectly with NumPy and Pandas,
making it the first choice for creating visual reports.
Matplotlib provides many benefits.
pip install matplotlib
Import the library:
import matplotlib.pyplot as plt
pyplot is the module used to create graphs.
Start
│
▼
Import matplotlib.pyplot
│
▼
Prepare Data
│
▼
Choose Chart Type
│
▼
Customize Chart
│
▼
Display Chart
│
▼
End
A Line Chart connects data points using straight
lines.
It is mainly used to show changes over time.
For example,
Whenever your data has a sequence like days, months, or
years, a line chart is usually the best choice.
import matplotlib.pyplot as plt
y = [20,35,30,40]
plt.show()
Example
import matplotlib.pyplot as plt
sales = [100,120,150,130,180]
Sales
^
| *
| *
| *
| *
| *
+----------------------> Months
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.
Bar Chart Syntax
plt.bar(x, y)
Example
import matplotlib.pyplot as plt
marks = [85,70,95,80]
Marks
95 | █
85 | █ █
80 | █ █ █
70 | █ █
█ █
+------------------
A B C D
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
Together these make 100%.
plt.pie(values)
import matplotlib.pyplot as plt
Output
Food
◜──────◝
Rent │ │ Travel
◟──────◞
Shopping
Advantages
Applications
A Histogram is a graph that shows the distribution of
continuous numerical data.
Although it looks similar to a bar chart, it is different.
Student marks
10-20
Instead of showing individual students, it shows how many
students fall within each range.
Histogram Syntax
plt.hist(data)
import matplotlib.pyplot as plt
Frequency
5 | ███
4 | █████
3 | ███████
2 | ███████
1 | ███████
+-------------------->
Marks
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 |
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.
Beginner
Intermediate
Advanced
Comments
Post a Comment