What is Matplotlib?
Learn the fundamentals of Matplotlib, understand its features, and discover how Python can be used to create charts, graphs, and data visualizations.
1. Learning Objectives
By the end of this lesson, you will be able to:
- Understand what Matplotlib is.
- Explain why Matplotlib is used in Python.
- Identify the important features of Matplotlib.
- Understand the different types of plots.
- Recognize the relationship between Python and Matplotlib.
- Understand basic Matplotlib plotting workflow.
- Write a simple Python program to create a chart.
2. What Is Matplotlib?
Matplotlib is an open-source Python library used for creating static, animated, and interactive visualizations. It helps developers and data analysts represent numerical information through charts, graphs, and plots.
Matplotlib is widely used in data analysis, scientific computing, machine learning, research, and data visualization.
Example: Student Performance Analysis
Omkar has a student performance dataset containing Student Name, Subject, and Marks columns. He wants to understand how students perform in different subjects.
Using Matplotlib, he can create a bar chart to compare marks, a line chart to show performance changes, and a pie chart to represent category distributions.
3. Why Do We Use Matplotlib?
Raw data is often difficult to understand when it contains hundreds or thousands of values. Visualizations help users identify patterns, comparisons, and trends more easily.
Main Benefits
- Helps understand complex datasets.
- Makes comparisons easier.
- Helps identify patterns and trends.
- Supports different chart types.
- Allows customization of chart appearance.
- Works with popular Python data libraries.
4. Important Features of Matplotlib
Matplotlib provides several features that make it useful for creating different types of visualizations.
| Feature | Description | Example |
|---|---|---|
| Multiple Plot Types | Supports different types of charts and graphs. | Line, Bar, Scatter and Pie charts. |
| Customization | Allows users to customize colors, labels, lines and markers. | Changing chart colors and titles. |
| Subplots | Allows multiple plots in a single figure. | Displaying four charts together. |
| Annotations | Adds text, arrows and explanatory information. | Highlighting an important data point. |
| Animation | Supports animated visualizations. | Showing changing values over time. |
| File Export | Allows figures to be saved in different formats. | PNG, JPG, PDF and SVG. |
5. Different Types of Plots in Matplotlib
Matplotlib supports different visualization types. Each plot is useful for representing a particular type of data.
| Plot Type | Purpose | Example Use |
|---|---|---|
| Line Plot | Shows trends and changes over time. | Monthly sales analysis. |
| Bar Chart | Compares values across categories. | Student marks comparison. |
| Scatter Plot | Displays relationships between two numerical variables. | Study hours versus marks. |
| Histogram | Shows the distribution of numerical data. | Exam marks distribution. |
| Pie Chart | Shows proportions of a whole. | Course-wise student distribution. |
6. How Does Matplotlib Work?
Matplotlib uses Python code to receive data, create a figure, draw plots, customize the output, and display or save the result.
Basic Working Steps
- Prepare the data using Python.
- Import the Matplotlib library.
- Create a figure or plotting area.
- Choose a suitable plot type.
- Add titles, labels, colors, and other formatting.
- Display or save the visualization.
7. Your First Matplotlib Program
Let's create a simple line plot using Matplotlib. In this example, we will display student marks in three subjects.
Example: Student Marks
import matplotlib.pyplot as plt
subjects = ["Java", "Python", "SQL"]
marks = [85, 90, 78]
plt.plot(subjects, marks, marker="o")
plt.title("Student Subject Marks")
plt.xlabel("Subjects")
plt.ylabel("Marks")
plt.grid(True)
plt.show()Code Explanation
- import matplotlib.pyplot as plt: Imports the pyplot module.
- subjects: Stores the subject names.
- marks: Stores the corresponding marks.
- plt.plot(): Creates a line plot.
- plt.title(): Sets the chart title.
- plt.xlabel(): Sets the X-axis label.
- plt.ylabel(): Sets the Y-axis label.
- plt.grid(True): Displays grid lines.
- plt.show(): Displays the chart.
matplotlib.pyplot module provides a simple
interface for creating plots. We will explore it in more detail
in upcoming lessons.
8. Applications of Matplotlib
Matplotlib is used in different domains where data needs to be analyzed and presented visually.
| Domain | Application |
|---|---|
| Education | Student marks and attendance analysis. |
| Business | Sales, revenue, and profit reports. |
| Finance | Financial trends and investment data visualization. |
| Healthcare | Visualizing medical and research datasets. |
| Machine Learning | Visualizing model performance and training results. |
| Scientific Research | Analyzing experimental data and measurements. |
9. Matplotlib with Other Python Libraries
Matplotlib can work with other Python libraries to support data processing, analysis, and visualization.
Common Integrations
- NumPy: Provides numerical arrays and mathematical operations.
- Pandas: Helps load, clean, and organize tabular data.
- Seaborn: Provides a higher-level statistical visualization interface built on Matplotlib.
- SciPy: Supports scientific and statistical computing.
10. Classroom Practical Activity
Activity: Create Your First Chart
At CIIT Training Institute, students can complete this practical activity using Python and Matplotlib.
- Open Python or your preferred Python IDE.
- Make sure Matplotlib is installed.
- Import
matplotlib.pyplot. - Create a list of five student names.
- Create a corresponding list of student marks.
- Use
plt.plot()to create a line chart. - Add a chart title and axis labels.
- Display the chart using
plt.show().
Practice Questions
- What is Matplotlib?
- Why is Matplotlib used in Python?
- What is the purpose of the pyplot module?
- Name four different types of plots supported by Matplotlib.
- What is the difference between a line plot and a scatter plot?
- How can Matplotlib work with NumPy and Pandas?
- What is the purpose of
plt.show()?
Summary
In this lesson, you learned that Matplotlib is an open-source Python library used to create static, animated, and interactive visualizations. It helps represent numerical data through different types of charts and graphs.
You explored the benefits and important features of Matplotlib,
including line plots, bar charts, scatter plots, histograms,
and pie charts. You also understood the basic plotting workflow
and created your first line plot using
matplotlib.pyplot.
Matplotlib can work with libraries such as NumPy, Pandas, and Seaborn to support data analysis and visualization. In the next lesson, you will learn how to install Matplotlib and prepare your Python environment.