Informatics Practices

Line Plot, Bar Graph & Histogram using Matplotlib | Complete Notes with Programs | CBSE Class 12 Informatics Practices (2026–27)

Class 12 · Informatics Practices

Line Plot, Bar Graph & Histogram using Matplotlib

Matplotlib provides different types of graphs to represent data visually. In the CBSE Class 12 Informatics Practices syllabus, students are required to learn three commonly used graphs:

  • Line Plot
  • Bar Graph
  • Histogram

These graphs help present data clearly and make it easier to identify trends, comparisons, and distributions.


1. Line Plot

A Line Plot is used to show changes or trends over time by joining data points with straight lines.

Syntax

plt.plot(x, y)

Example

import matplotlib.pyplot as plt

months = ["Jan", "Feb", "Mar", "Apr"]
sales = [120, 150, 180, 200]

plt.plot(months, sales)

plt.show()

Use: Student performance over months, temperature changes, stock prices, rainfall trends.


2. Bar Graph

A Bar Graph compares values across different categories using rectangular bars.

Syntax

plt.bar(x, y)

Example

import matplotlib.pyplot as plt

subjects = ["Math", "Science", "English"]
marks = [92, 85, 88]

plt.bar(subjects, marks)

plt.show()

Use: Compare marks, sales of products, number of students in classes.


3. Histogram

A Histogram displays the frequency distribution of continuous numerical data.

Syntax

plt.hist(data)

Example

import matplotlib.pyplot as plt

marks = [45,55,60,65,70,75,80,85,90,95]

plt.hist(marks)

plt.show()

Use: Age distribution, examination marks, salary distribution.


Comparison of Graphs

Graph Purpose Data Type
Line Plot Shows trends Continuous
Bar Graph Compares categories Categorical
Histogram Shows frequency distribution Continuous

Customizing Graphs

Matplotlib provides functions to make graphs more informative and attractive.


Adding X-axis Label

plt.xlabel("Months")

Adding Y-axis Label

plt.ylabel("Sales")

Adding a Title

plt.title("Monthly Sales Report")

Adding a Legend

plt.plot(months, sales, label="Sales")

plt.legend()

The legend() function identifies different data series in a graph.


Saving a Graph

plt.savefig("sales_report.png")

This saves the graph as an image file in the current working directory.


Complete Example

import matplotlib.pyplot as plt

months = ["Jan", "Feb", "Mar", "Apr"]

sales = [120, 150, 180, 200]

plt.plot(months, sales, label="Sales")

plt.xlabel("Months")

plt.ylabel("Sales")

plt.title("Monthly Sales Report")

plt.legend()

plt.savefig("sales.png")

plt.show()

Expected Output

  • A line graph is displayed.
  • X-axis shows months.
  • Y-axis shows sales.
  • Title appears at the top.
  • Legend displays "Sales".
  • The graph is saved as sales.png.

When to Use Which Graph?

Situation Best Graph
Monthly Sales Trend Line Plot
Student Marks Comparison Bar Graph
Distribution of Marks Histogram
Daily Temperature Line Plot
Population of Cities Bar Graph
Employee Salary Distribution Histogram

Common Customization Functions

Function Purpose
plot() Draw line plot
bar() Draw bar graph
hist() Draw histogram
xlabel() Label X-axis
ylabel() Label Y-axis
title() Add graph title
legend() Display legend
savefig() Save graph
show() Display graph

Common Errors

Error Reason
ValueError X and Y lists have different lengths.
NameError Matplotlib not imported correctly.
Blank Window plt.show() not called.
File Not Saved savefig() called after closing the figure.

Quick Revision

Function Purpose
plot() Line Plot
bar() Bar Graph
hist() Histogram
xlabel() X-axis Label
ylabel() Y-axis Label
title() Graph Title
legend() Graph Legend
savefig() Save Graph
show() Display Graph

CBSE Exam Tips

  • Remember the correct function for each graph:
    • plot() → Line Plot
    • bar() → Bar Graph
    • hist() → Histogram
  • Always call plt.show() to display the graph.
  • Use xlabel(), ylabel(), and title() to make graphs meaningful.
  • Add a legend whenever multiple data series are plotted.
  • Practice complete plotting programs for the practical examination.

Summary

Matplotlib is a powerful library for visualizing data in Python. A Line Plot is used to show trends, a Bar Graph compares categories, and a Histogram displays frequency distributions. Functions such as xlabel(), ylabel(), title(), legend(), and savefig() help customize graphs and improve their presentation. These concepts are essential for both the CBSE theory and practical examinations.