Chapter 04 - Plotting Data with Matplotlib
4.1 Introduction
4.1.1 Why Data Visualisation is Needed (NCERT Context)
NCERT begins this chapter by emphasising an important idea:
Analysing numerical data alone is often not sufficient to understand patterns and trends.
In the previous chapters, you learnt to:
- Store data using Pandas
- Perform calculations and aggregations
- Summarise data using statistics
However, when data becomes large or complex:
- Tables of numbers are difficult to interpret
- Patterns are not immediately visible
- Comparisons become confusing
This creates the need for data visualisation.
4.1.2 What is Data Visualisation?
NCERT defines data visualisation as:
The graphical representation of data to understand patterns, trends, and relationships.
Instead of reading rows and columns, data is shown using:
- Graphs
- Charts
- Plots
This allows users to:
- Understand data quickly
- Identify trends and outliers
- Compare values easily
4.1.3 Examples of Where Graphs are Useful
NCERT gives practical motivation. Graphs are useful when we want to:
- Compare marks of students
- Observe sales trends over years
- Compare population of different cities
- Track temperature changes over days
In all these cases:
- Visual representation is more effective than raw numbers
4.1.4 Types of Graphs Used in Data Analysis
NCERT explains that different types of data require different graphs, such as:
- Line graphs → trends over time
- Bar graphs → comparison between categories
- Histograms → distribution of data
- Pie charts → proportion or percentage
These graph types will be studied in detail in this chapter.
4.1.5 Introduction to Matplotlib
NCERT introduces Matplotlib as the main Python library used for plotting graphs.
Key points highlighted by NCERT:
- Matplotlib is a Python plotting library
It is widely used for:
- Data visualisation
- Scientific computing
- Data analysis
It works well with:
- Python lists
- NumPy arrays
- Pandas Series and DataFrames
Matplotlib allows users to:
- Create simple graphs
- Customise plots extensively
- Display data in a clear visual form
4.1.6 Relationship Between Pandas and Matplotlib
NCERT makes an important connection:
- Pandas internally uses Matplotlib for plotting
- Pandas provides a simpler interface for basic plots
Matplotlib provides:
- Greater control
- More customisation
This chapter will show:
- Direct plotting using Matplotlib
- Plotting using Pandas’ built-in plot function
4.1.7 Why Matplotlib is Important for Students
NCERT highlights that learning Matplotlib helps students:
- Convert data into visual form
- Interpret data more effectively
- Answer data-based questions in exams
Build foundational skills for:
- Data science
- Analytics
- Scientific studies
Understanding graphs is as important as writing code.
4.1.8 Scope of This Chapter
NCERT clearly outlines what students will learn in this chapter:
- How to plot graphs using Matplotlib
- How to customise graphs (labels, titles, colors, legends)
- How to plot data directly from Pandas
- How to choose appropriate graphs for given data
Each concept will be supported with:
- Code examples
- Visual outputs
- Interpretation of graphs
4.1.9 Learning Outcome of Section 4.1
After studying this section, a student should be able to:
- Explain the need for data visualisation
- Define data visualisation
- Identify situations where graphs are useful
- Name Matplotlib as a plotting library
- Understand the role of visualisation in data analysis
4.2 Plotting using Matplotlib
4.2.1 Importing Matplotlib (Mandatory Step)
NCERT starts plotting by importing the pyplot module of Matplotlib.
import matplotlib.pyplot as plt
Explanation:
pyplotprovides functions to create plots easilypltis a commonly used alias- All plotting commands are issued through
plt
4.2.2 Basic Components of a Plot (NCERT Concept)
Before plotting, NCERT explains that every graph consists of:
- X-axis → horizontal axis
- Y-axis → vertical axis
- Data points
- Labels and title (optional but recommended)
These elements make a graph readable and meaningful.
4.2.3 Plotting a Simple Line Graph
NCERT introduces the line graph first, as it is the simplest and most commonly used plot.
Example: Marks over Days
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [50, 60, 55, 70, 65]
plt.plot(x, y)
plt.xlabel("Days")
plt.ylabel("Marks")
plt.title("Line Plot: Marks over Days")
plt.show()
Output:
- The line graph displayed above
Interpretation (Very Important)
NCERT expects students to be able to read the graph:
- X-axis → Days
- Y-axis → Marks
- The line shows how marks change over time
- Increase or decrease in slope indicates improvement or drop
Line graphs are best suited for:
- Trends
- Time-based data
4.2.4 Plotting a Bar Graph
NCERT next introduces the bar graph, used for comparison.
Example: Marks comparison
plt.bar(x, y)
plt.xlabel("Days")
plt.ylabel("Marks")
plt.title("Bar Plot: Marks over Days")
plt.show()
Output:
- The bar chart displayed above
Interpretation
- Each bar represents marks on a particular day
- Height of the bar indicates value
Bar graphs are useful for:
- Comparing categories
- Discrete data
4.2.5 Difference Between Line Graph and Bar Graph (NCERT Focus)
| Line Graph | Bar Graph |
|---|---|
| Shows trend | Shows comparison |
| Continuous data | Discrete data |
| Connected points | Separate bars |
Students are often asked:
Which graph is suitable for given data?
4.2.6 Plotting a Pie Chart
NCERT introduces pie charts to show proportions.
Example: Marks distribution
plt.pie(y, labels=x, autopct='%1.1f%%')
plt.title("Pie Chart: Marks Distribution")
plt.show()
Output:
- The pie chart displayed above
Interpretation
- Each slice represents contribution of marks
- Percentage shows share of each category
Pie charts are best for:
- Showing parts of a whole
- Percentage distribution
4.2.7 The show() Function (Important)
NCERT stresses the use of:
plt.show()
Explanation:
- Displays the graph
- Without
show(), the plot may not appear - Always required at the end of plotting commands
4.2.8 Order of Plotting Commands (Exam-Relevant)
NCERT expects this logical sequence:
- Import library
- Provide data
- Plot the graph
- Add labels and title
- Display using
show()
Incorrect order may lead to:
- Missing labels
- Incomplete graph
4.2.9 Common Errors Students Make (NCERT Awareness)
- Forgetting
plt.show() - Mismatch between x and y data lengths
- Not labelling axes
- Choosing wrong type of graph for data
Understanding these helps in exams and practice.
4.2.10 Learning Outcome of Section 4.2
After this section, a student should be able to:
- Import Matplotlib correctly
- Plot a line graph
- Plot a bar graph
- Plot a pie chart
- Label axes and title
- Interpret graphs correctly
- Choose appropriate graph type for given data
4.3 Customisation of Plots
4.3.1 Why Plot Customisation is Required (NCERT Context)
NCERT explains that simply plotting data is not sufficient.
A good graph should:
- Be clear
- Be readable
- Convey the message unambiguously
Customisation helps to:
- Highlight important information
- Differentiate between multiple data sets
- Make graphs suitable for presentation and interpretation
4.3.2 Adding Markers to a Line Plot
By default, line plots show only lines. NCERT introduces markers to show individual data points clearly.
Example: Line plot with markers
plt.plot(x, y1, marker='o')
Explanation:
marker='o'displays a circular marker at each data point- Helps identify exact values on the graph
In the first graph shown above:
- Each data point is clearly visible
- This improves readability
4.3.3 Changing Line Style
NCERT shows that line appearance can be changed using linestyle.
Common line styles:
'-'→ solid line'--'→ dashed line':'→ dotted line
Example:
plt.plot(x, y1, linestyle='--')
In the first graph:
- A dashed line is used
- This visually distinguishes it from a normal line plot
4.3.4 Displaying Grid Lines
NCERT recommends adding a grid for better readability.
plt.grid(True)
Explanation:
- Grid lines help estimate values accurately
- Especially useful when reading values from the graph
In all graphs shown above:
- Grid lines are visible
- This makes comparison easier
4.3.5 Plotting Multiple Lines on the Same Graph
NCERT introduces multiple plots in a single figure for comparison.
Example: Two students’ marks
plt.plot(x, y1, marker='o')
plt.plot(x, y2, marker='s')
Explanation:
- Both lines share the same axes
- Useful for comparing trends
This is shown in the second graph above.
4.3.6 Adding a Legend
When multiple lines are present, NCERT stresses the need for a legend.
plt.legend(["Student A", "Student B"])
Explanation:
- Legend explains which line represents which dataset
- Essential for multi-line graphs
In the second graph:
- Legend clearly distinguishes Student A and Student B
4.3.7 Customising Bar Graphs
Customisation also applies to bar graphs.
Example: Bar graph with grid
plt.bar(x, y1)
plt.grid(axis='y')
Explanation:
- Grid is added along Y-axis only
- Helps compare bar heights easily
This is shown in the third graph above.
4.3.8 Titles and Axis Labels (NCERT Emphasis)
Every graph should have:
- A title
- X-axis label
- Y-axis label
Example:
plt.xlabel("Days")
plt.ylabel("Marks")
plt.title("Bar Graph with Grid")
NCERT often deducts marks if:
- Labels or title are missing in answers
4.3.9 Using figure() for Separate Graphs
NCERT introduces plt.figure() to create new figures.
plt.figure()
Explanation:
- Ensures each graph appears separately
- Prevents overlapping of plots
- Very important when drawing multiple graphs in one program
4.3.10 Common Customisation Parameters (NCERT Level)
At CBSE level, students are expected to know:
markerlinestylegrid()legend()xlabel(),ylabel(),title()figure()show()
Advanced styling (colors, themes) is not required unless explicitly asked.
4.3.11 Exam-Oriented Tips (Very Important)
NCERT-based practical answers should always:
- Choose correct graph type
- Add axis labels
- Add title
- Add legend (if needed)
- Display the graph
Missing any of these may result in loss of marks.
4.3.12 Learning Outcomes of Section 4.3
After this section, a student should be able to:
- Add markers and line styles to plots
- Enable grid lines
- Plot multiple lines on the same graph
- Add legends for clarity
- Customise bar graphs
- Produce neat, exam-ready graphs
4.4 The Pandas Plot Function (Pandas Visualisation)
4.4.1 Why Pandas Provides a Plot Function (NCERT Context)
NCERT explains that:
- Pandas is built on top of Matplotlib
- To make plotting easier, Pandas provides the
.plot()method This allows:
- Direct plotting from Series
- Direct plotting from DataFrames
- Less code
- Faster visualisation
Internally:
Pandas uses Matplotlib to generate graphs.
So whatever you learnt in 4.2 and 4.3 still applies.
4.4.2 Sample DataFrame Used
import pandas as pd
data = {
"Year": [2019, 2020, 2021, 2022, 2023],
"Sales": [50, 60, 55, 70, 65],
"Profit": [20, 25, 22, 30, 28]
}
df = pd.DataFrame(data)
df.set_index("Year", inplace=True)
print(df)
4.4.3 Line Plot using Pandas
Python Code (Exam-Ready)
df["Sales"].plot(title="Sales Trend")
plt.xlabel("Year")
plt.ylabel("Sales")
plt.show()
Output Graph

📥 Download: 4_4_pandas_line.png
Interpretation (NCERT Expectation)
- X-axis → Year (index)
- Y-axis → Sales
- Shows sales trend over time
- Same as Matplotlib line plot, but less code
4.4.4 Bar Plot using Pandas
Python Code
df["Sales"].plot(kind="bar", title="Sales Bar Graph")
plt.xlabel("Year")
plt.ylabel("Sales")
plt.show()
Output Graph

📥 Download: 4_4_pandas_bar.png
Interpretation
- Each bar represents sales in a year
- Useful for comparison
- Pandas automatically uses index values on X-axis
4.4.5 Plotting Multiple Columns using Pandas
NCERT highlights that Pandas can plot multiple columns together easily.
Python Code
df.plot(title="Sales and Profit Comparison")
plt.xlabel("Year")
plt.ylabel("Amount")
plt.show()
Output Graph

📥 Download: 4_4_pandas_multi.png
Interpretation
Two lines:
- Sales
- Profit
Automatically adds legend
Best for quick comparison
4.4.6 Other Common Pandas Plot Types (NCERT Awareness)
| Graph Type | Code |
|---|---|
| Line | df.plot() |
| Bar | df.plot(kind='bar') |
| Horizontal Bar | df.plot(kind='barh') |
| Histogram | df.plot(kind='hist') |
| Box Plot | df.plot(kind='box') |
Only line and bar are usually required at CBSE level.
4.4.7 Pandas Plot vs Matplotlib Plot (Exam Question)
| Pandas Plot | Matplotlib Plot |
|---|---|
| Less code | More control |
| Easy to use | More flexible |
| Built on Matplotlib | Core plotting library |
| Good for quick plots | Good for detailed plots |
NCERT expects students to state this difference clearly.
4.4.8 When to Use Pandas Plot Function
NCERT guidance:
Use Pandas plot when:
- Data is already in DataFrame
- Quick visualisation is needed
Use Matplotlib when:
- High customisation is required
- Multiple complex plots are needed
4.4.9 Learning Outcomes of Section 4.4
After this section, students should be able to:
- Plot graphs directly from Pandas Series
- Plot graphs directly from Pandas DataFrames
- Create line and bar graphs using
.plot() - Interpret Pandas-generated graphs
- Compare Pandas plotting with Matplotlib plotting