0% found this document useful (0 votes)
9 views33 pages

Introduction To Matplotlib

The document outlines the syllabus and learning objectives for a Python Programming course focusing on Matplotlib, a popular library for data visualization in Python. It covers the key components of Matplotlib, various plot types, and customization techniques for effective data representation. Additionally, it includes a quiz section to assess understanding and provides references for further learning.

Uploaded by

yashupadhyay8868
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views33 pages

Introduction To Matplotlib

The document outlines the syllabus and learning objectives for a Python Programming course focusing on Matplotlib, a popular library for data visualization in Python. It covers the key components of Matplotlib, various plot types, and customization techniques for effective data representation. Additionally, it includes a quiz section to assess understanding and provides references for further learning.

Uploaded by

yashupadhyay8868
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Academic Session 2025-26

ODD Semester Jul-Dec 2025

UNIVERSITY INSTITUTE OF COMPUTING


MCA DEPARTMENT
MCA/MCA(AIML)/MCA(CLOUD COMPUTING)/MCA(DATASCIENCE)
SEMESTER-1ST
PYTHON PROGERAMMING
(25CAH-606)
Unit No.II
Topic : Matplot lib

Mr. Ajay Yadav

Assistant Professor
1

Learning Objectives-
•Understand the Purpose of Matplotlib
Learn how Matplotlib helps in creating static, animated, and interactive visualizations in
Python.
•Familiarize with Matplotlib Components
Identify and understand key components such as Figure, Axes, Labels, Legends, and
Ticks.
•Create Basic Plot Types
Gain the ability to create various plots such as line plots, bar charts, histograms, scatter
plots, and pie charts.
•Customize Plots for Better Visualization
Learn how to enhance visualizations by modifying plot titles, axis labels, colors, markers,
and styles.
Syllabus

Unit-II
Introduction to Data Analytics, requirements of data analytics in Python.
Introduction to Numpy, features, environment setup, numpy Ndarray, array creation
data types, array attributes, numpy operations, mathematical and statistical
functions

Introduction to matplotlib, Figure class, Axes class, line plot, subplots, Bar plot,
histogram, scatter plot, pie chart, box plot, area chart, word cloud, Bee swarm plot,
violin graph, working with text, Customizing Plots: Titles, labels, and legends.

3
Matplotlib
• Matplotlib is one of the most popular Python packages used for data visualization.
• It is a cross-platform library for making 2D plots from data in arrays.

• Matplotlib is open source and use it freely.


• Matplotlib is mostly written in python, a few segments are written in C,
Objective-C and Javascript for Platform compatibility.
• Matplotlib has a procedural interface named the Pylab, which is designed to
resemble MATLAB, a proprietary programming language developed by
MathWorks. Matplotlib along with NumPy can be considered as the open source
equivalent of MATLAB.

4
Matplotlib
• Matplotlib Pyplot

5
Matplotlib
• Plotting Without Line

6
Matplotlib

7
Matplotlib
• Default X-Points :If we do not specify the points in the x-axis, they will get the
default values 0, 1, 2, 3, (etc. depending on the length of the y-points).

8
Matplotlib
• Line Properties: There are the following line properties:
• linestyle
• Color
• linewidth
• marker
• ms(marker size)
• mfc(marker color)
• title
• xlabel
• ylabel
• grid 9
Matplotlib
• Linestyle

10
Matplotlib
• Matplotlib Labels and Title

11
Matplotlib
• Add Grid Lines to a Plot

12
Matplotlib
• Display Multiple Plots: The subplot() function takes three arguments that
describes the layout of the figure. The layout is organized in rows and columns,
which are represented by the first and second argument. The third argument
represents the index of the current plot.

13
Matplotlib
• Python Matplotlib : Types of Plots

14
Matplotlib
• Types of Plots:
• Bar Graph: A bar graph uses bars to compare data among different categories. It
is well suited when you want to measure the changes over a period of time. It can
be represented horizontally or vertically.
• Histograms: Histograms are used to show a distribution whereas a bar chart is
used to compare different entities. Histograms are useful where arrays in very
large size.
• Scatter Plot : Usually scatter plots is used to compare variables, for example,
how much one variable is affected by another variable to build a relation out of it.
The data is displayed as a collection of points, each having the value of one
variable which determines the position on the horizontal axis and the value of
other variable determines the position on the vertical axis.
15
Matplotlib
• Area Plot : Area plots are pretty much similar to the line plot. They are also
known as stack plots. These plots can be used to track changes over time for two
or more related groups that make up one whole category.
• Pie Chart: A pie chart refers to a circular graph which is broken down into
segments i.e. slices of pie. It is basically used to show the percentage or
proportional data where each slice of pie represents a category.

16
Matplotlib
• Matplotlib Scatter

17
Matplotlib
• Matplotlib Bars

18
Matplotlib
• Matplotlib Histograms

19
Matplotlib
• Matplotlib Pie Charts

20
Matplotlib
• Matplotlib Area plot

21
violin plot and a box plot
• The box plot shows the median (line) and quartiles (box edges).
• The violin plot shows how the data is distributed — thick where many
values occur, thin where fewer exist.
Feature Box Plot Violin Plot

Purpose Shows summary statistics Shows both summary stats


(median, quartiles, outliers) and the data distribution

Rectangular box with Symmetrical “violin-


Shape whiskers shaped” plot (like a
smoothed density curve)
✅ Yes (using kernel density
Displays Distribution? ❌ No (only quartiles) estimate)
✅ Yes (horizontal line inside ✅ Yes (usually marked inside
Shows Median? box) the violin)

Shows Quartiles (IQR)? ✅ Yes ✅ Yes (inside the violin’s


thicker region)
✅ Yes (as dots beyond Optional — not always
Shows Outliers? whiskers) shown

When you want a summary When you want to see


When to Use distribution shape +
Box plot

• import matplotlib.pyplot as plt


• import numpy as np
• # Sample exam marks
• nos = [45, 48, 49, 52, 56, 58, 60, 61, 63, 64, 65, 66, 68, 70, 72, 74, 76, 78, 80, 95]
• # Create a box plot
• plt.figure(figsize=(6,5))
• plt.boxplot(nos, patch_artist=True, boxprops=dict(facecolor='skyblue', color='black'))
• # Add title and labels
• plt.title("Box Plot of Exam Marks")
• plt.ylabel("Marks")
• plt.grid(True)
• plt.show()
plt.figure() and plt.subplot()
• Matplotlib uses an object-oriented structure for plotting.
• The two most important objects are:

• Plt.figure()
• Creates a new figure window (a blank canvas) for your plots.
• plt.figure(figsize=(6,4))
2. Axes Object
• Created using: ax = fig.add_subplot(1,1,1)
• plt.subplot()
• Divides the figure into subplots (grids) and selects one to draw on.
• plt.subplot(2,1,1) creates a grid with 2 rows, 1 column, and activates the 1st plot.
plt.figure() and plt.subplot()
• plt.figure()
• Creates a new figure window (a blank canvas) for your plots.
plt.figure(figsize=(6,4))

• plt.subplot()
• Divides the figure into subplots (grids) and selects one to draw on.
• plt.subplot(2,1,1) creates a grid with 2 rows, 1 column, and activates the 1st
plot.
Broadcasting in NumPy
• Broadcasting means automatically expanding smaller arrays so that
they have compatible shapes during arithmetic operations — without
actually copying data.
• It allows NumPy to perform operations on arrays of different shapes
efficiently.
• Example:
import numpy as np
a = np.array([1, 2, 3]) output:
b=2
result = a + b [3 4 5]
print(result)
Text annotations in Matplotlib
• Text annotations is key to making your plots in Matplotlib more
informative and professional.
• Annotations in Matplotlib are used to add explanatory text or labels
directly on a graph — like marking a specific data point, labeling a
peak, or explaining trends.
• They make your plots easier to understand.
Function Purpose

Add text anywhere on the plot


plt.text() (x, y coordinates).

Add text linked to a specific data


plt.annotate() point, optionally with an arrow.
• Using plt.text()
• import matplotlib.pyplot as plt
• plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
• plt.title("Text Example")
• # Add text at (x=2, y=20)
• plt.text(2, 20, "Mid Point", color='blue', fontsize=12)
• plt.show()

• Using plt.annotate() — More Powerful


• plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
• plt.text(1, 10, "Start", color='purple', fontsize=10)
• plt.annotate("Peak", xy=(4, 30), xytext=(3, 32),
• arrowprops=dict(arrowstyle="->", color='red'))

• plt.show()
Summary-
•Matplotlib is a powerful Python library for creating static, interactive, and animated visualizations in 2D (and basic
3D).
•It provides an easy-to-use pyplot interface (similar to MATLAB) for quick plotting, along with an object-oriented
API for advanced customizations.
•The Figure is the overall container, and Axes are the areas where plots like line charts, bar graphs, histograms,
etc., are drawn.
•Matplotlib supports a wide range of plots including line plots, bar charts, histograms, scatter plots, pie charts,
and box plots.
•It offers rich customization options for titles, labels, legends, colors, grids, styles, and also integrates well with
NumPy and Pandas.
Quiz /Questions-
1. What is Matplotlib and why is it used in Python programming?
(Explain its purpose and features.)
2. Differentiate between Figure and Axes in Matplotlib.
(Test understanding of object structure.)
3. Write a code to plot a simple line graph with labels for X and Y axes and a
title.
(Basic usage of plt.plot() and formatting functions.)
4. What is the use of plt.subplot() and how does it differ from plt.subplots()?
(Test knowledge of subplots creation.)
5. Name and explain any three types of plots supported by Matplotlib.
(Like bar plot, histogram, scatter plot, etc.)
Class Wise Feedback
9

References/ Articles/ Videos


1.https://www.python.org/about/gettingstarted/
2. https://www.w3schools.com/python/python_intro.asp
3. https://www.geeksforgeeks.org/python-programming-language-tutorial/
4. https://en.wikipedia.org/wiki/Python_(programming_language)
5. https://www.youtube.com/watch?v=ix9cRaBkVe0
6. https://pythonprogramming.net/
7. https://www.codechef.com/learn/course/python
8. https://www.youtube.com/watch?v=n3KMrwmq_94
9. https://www.youtube.com/watch?v=ppsCxnNm-JI
THANK YOU

33

You might also like