0% found this document useful (0 votes)
7 views25 pages

Data Visualization with Matplotlib Guide

Unit 3 of the 'Mastering Data Science' course focuses on data visualization using the Matplotlib library in Python. It covers various topics including the creation of plots, bar charts, and the use of helper functions to streamline the plotting process. The unit emphasizes the importance of visualizing data for better decision-making and provides practical coding examples for effective data representation.

Uploaded by

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

Data Visualization with Matplotlib Guide

Unit 3 of the 'Mastering Data Science' course focuses on data visualization using the Matplotlib library in Python. It covers various topics including the creation of plots, bar charts, and the use of helper functions to streamline the plotting process. The unit emphasizes the importance of visualizing data for better decision-making and provides practical coding examples for effective data representation.

Uploaded by

devagravat67
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

I.M.C.

A Semester – 5

MASTERING DATA SCIENCE A


GUIDE TO PYTHON KEY LIBRARY

Unit – 3

1|P a g e
Unit -3: Data Visulazation

Contents
Matplotlib ............................................................................................................................................................................. 4
Making Helper Functions ...................................................................................................................................................... 7
Multiple figure and axes ....................................................................................................................................................... 9
Introduction to Pyplot ......................................................................................................................................................... 11
Bar Charts ........................................................................................................................................................................... 15
Plotting with keyword strings ............................................................................................................................................. 17
Controlling line properties .................................................................................................................................................. 20
Plotting with categorical variable ....................................................................................................................................... 22
Generate bio signal plotting using matplotlib and pandas ................................................................................................ 23

2|P a g e
• Data visualization is the graphical representation of information
and data.

• It uses visual elements like charts, graphs and maps to help


convey complex information in a way that is easy to understand
and interpret.

• By transforming large datasets into visuals, it allows decision-


makers to spot trends, relationships and outliers quickly which
helps in better analysis and faster decision-making

• In today’s world, where every industry generates large amounts


of data, visualizing that data is important for extracting insights
and making informed decisions.

3|P a g e
Matplotlib
• What is Matplotlib?

• Matplotlib is a low level graph plotting library in python that


serves as a visualization.

• Matplotlib was created by John D. Hunter.

• Matplotlib is open source and we can use it freely.

• Installation of Matplotlib

• If you have Python and PIP already installed on a system, then


installation of Matplotlib is very easy.

• Install it using this command:

• PIP INSTALL MATPLOTLIB

• Checking Matplotlib Version

• import matplotlib
print(matplotlib.__version__)

• Pyplot

• Most of the Matplotlib utilities lies under the pyplot submodule,


and are usually imported under the plt alias:

Matplotlib Key Terms/Parts of Figure

Term Meaning
A single point on the plot (like one (x, y)
Data Point
value)
X-axis / Y-axis Horizontal (X) and vertical (Y) reference lines

4|P a g e
on the chart

The entire window or page that holds your


Figure
plot(s)
Axes The actual area where data is drawn
The visual representation of data (line, bar,
Plot
pie, etc.)
Symbols used to highlight individual data
Marker
points (e.g., o, *, ^)
How the line looks — solid (-), dashed (--),
Line Style
dotted (:), etc.
The color of lines, markers, or bars (e.g., 'red',
Color
'blue', '#00FF00')

Term Meaning

A key that shows what each line or color


Legend
represents
Text that describes the axes or individual data
Label
lines
Heading of the graph that explains what the
Title
plot is about
Optional light lines to make the plot easier to
Grid
read
The numbers and marks along the X and Y
Ticks
axes
Function to save the plot as an image file (e.g.,
Savefig()
.png, .pdf)

5|P a g e
Types of Inputs to Plotting Functions
• When we call functions like [Link]() or [Link](), they need some
input data. These can be:
• [Link] → Example:
• x = [1, 2, 3, 4]
• y = [2, 4, 6, 8]
• [Link](x, y)
• [Link] Arrays → Faster & more powerful for large data.
• import numpy as np
• x = [Link]([1, 2, 3, 4])
• y = [Link]([1, 4, 9, 16])
• [Link](x, y)
• 3. Pandas Series / DataFrame columns → Very common in data
analysis.
• import pandas as pd
• data = [Link]({
• "x": [1, 2, 3, 4],
• "y": [10, 20, 15, 25]
• })
• [Link](data["x"], data["y"])
Coding Style in Data Visualization

6|P a g e
• When writing visualization code (e.g., with Matplotlib, Seaborn),
we should follow good coding style so our plots are:
• Readable → Easy for you or others to understand later.
• Reusable → Can use the same code for multiple datasets.
• Maintainable → Easy to fix or improve.
• Best practices (style rules):
• Import clearly
• [Link] descriptive variable names (not just a, b)
• 3. Label everything (title, axes, legend)
• 4. Keep code organized – break into sections:
• Prepare data
• Create plot
• Customize plot
• Show/save plot

Making Helper Functions


• A helper function is a small function you create to avoid
repeating the same code again and again.

• Example: Instead of writing the same plotting steps for every


dataset, make a helper function.

import [Link] as plt

# Helper function

def plot_line(x, y, title="Line Chart", xlabel="X", ylabel="Y",


label=None):

7|P a g e
[Link](x, y, label=label)

[Link](title)

[Link](xlabel)

[Link](ylabel)

if label:

[Link]()

[Link](True)

[Link]()

# Using helper function

x = [1, 2, 3, 4]

y1 = [2, 4, 6, 8]

y2 = [1, 4, 9, 16]

plot_line(x, y1, title="Comparison", xlabel="Input", ylabel="Output",


label="2x")

plot_line(x, y2, title="Comparison", xlabel="Input", ylabel="Output",


label="x²")

8|P a g e
Multiple figure and axes
• A figure = the whole canvas (like one page).
If you want to make more than one independent graph, you
create multiple figures.

import [Link] as plt

# First figure

[Link](1) # create figure 1

[Link]([1,2,3], [2,4,6])

[Link]("Figure 1: Simple Line")

# Second figure

[Link](2) # create figure 2

[Link]([1,2,3], [1,4,9])

[Link]("Figure 2: Square Numbers")

[Link]()

• Multiple Axes (Subplots)

• Sometimes, instead of separate figures, you want many small


plots inside one figure.
For this, we use subplots.

import [Link] as plt

# Create a figure with 1 row, 2 columns of subplots

fig, axes = [Link](1, 2)

# First axes (left side)


9|P a g e
axes[0].plot([1,2,3], [2,4,6])

axes[0].set_title("Line: y=2x")

# Second axes (right side)

axes[1].plot([1,2,3], [1,4,9])

axes[1].set_title("Line: y=x²")

[Link]()

Example 2

import [Link] as plt

fig, axes = [Link](2, 2, figsize=(8,6))

axes[0,0].plot([1,2,3], [1,2,3])

axes[0,0].set_title("y=x")

axes[0,1].plot([1,2,3], [1,4,9])

axes[0,1].set_title("y=x²")

axes[1,0].plot([1,2,3], [1,8,27])

axes[1,0].set_title("y=x³")

axes[1,1].plot([1,2,3], [1,16,81])

axes[1,1].set_title("y=x⁴")

plt.tight_layout() # adjusts spacing

[Link]()

10 | P a g e
Introduction to Pyplot
• Pyplot is a module in Matplotlib (a popular Python library for data
visualization).

• It provides simple functions to create graphs and charts like line


plots, bar charts, scatter plots, histograms, etc.

• It works like a drawing tool:

• First, you plot data.

• Then you can add titles, labels, legends.

• Finally, you show the chart.

• We usually import it like this:

• import [Link] as plt

• Here:

• matplotlib = the library

• pyplot = the module

• plt = short name (alias) we give (commonly used)

• Matplotlib Plotting Function

Function Purpose / Description


Draws a line graph by plotting y-values against x-
[Link]()
values.
[Link]() Sets the label for the x-axis.
[Link]() Sets the label for the y-axis.
11 | P a g e
[Link]() Adds a title to the entire plot.
[Link]() Displays a grid on the plot for better readability.
[Link]() Displays a legend describing the plotted lines.

Function Purpose / Description


Draws a line graph by plotting y-values against
[Link]()
x-values.
[Link]() Sets the label for the x-axis.
[Link]() Sets the label for the y-axis.
[Link]() Adds a title to the entire plot.

12 | P a g e
[Link]() Displays a grid on the plot for better readability.
[Link]() Displays a legend describing the plotted lines.
Function Purpose / Description
[Link]() Opens a window to display the final plot.
[Link]() Sets the range (min, max) of values for the x-axis.
Sets the range (min, max) of values for the y-
[Link]()
axis.
Sets custom tick positions and labels on the x-
[Link]()
axis.
Sets custom tick positions and labels on the y-
[Link]()
axis.
Example:

import [Link] as plt

x = [1, 2, 3, 4]

y = [10, 20, 25, 30]

[Link](x, y, label='Growth')

[Link]('Time')

[Link]('Value')

[Link]('Growth Over Time')

[Link](True)

[Link]()

[Link](0, 5)

[Link](0, 35)

[Link]([1, 2, 3, 4], ['M1', 'M2', 'M3', 'M4'])


13 | P a g e
[Link]([10, 20, 25, 30])

[Link]()

• Matplotlib Line

• Linestyle

• You can use the keyword argument linestyle, or shorter ls, to


change the style of the plotted line:

• Example

• Use a dotted line:

import [Link] as plt

import numpy as np

ypoints = [Link]([3, 8, 1, 10])

[Link](ypoints, linestyle='dotted')

[Link]()

14 | P a g e
Bar Charts
• A bar chart is used when:

• You want to compare categories or groups.

• You're showing discrete data, not continuous.

• You want to visualize frequency, count, or values across different


categories.

• Ex: city population, sales data, students marks, websites traffic


Etc,

• Syntax: [Link](x,height, colar=‘colar


name’,width=value,label=value)

import [Link] as plt

# Data

categories = ['Python', 'Java', 'C++', 'JavaScript']

values = [80, 60, 40, 70]

# Plotting bar chart

[Link](categories, values, color=‘green’ label='Popularity') # [Link]


is used for bar charts

# Labels and title

[Link]('Programming Languages')

[Link]('Popularity Score')

[Link]('Language Popularity in 2025')

# Grid, ticks, limits


15 | P a g e
[Link](axis='y') # Only show grid on y-axis

[Link](categories) # Optional (customize x-axis ticks)

[Link]([20, 40, 60, 80, 100]) # Customize y-axis ticks

[Link](0, 100) # Limit y-axis range

# Legend

[Link]()

# Show plot

[Link]()

16 | P a g e
Plotting with keyword strings
• This is a shortcut way of formatting plots.

• Plotting with Keyword String

• In [Link](x, y, "format"),

• the "format" string can include:

• Color (e.g., r, g, b, k)

• Marker (e.g., o, s, ^, *)

• Line style (e.g., -, --, :, -.)

• You can combine them in one string.

import [Link] as plt

x = [1, 2, 3, 4]

y = [2, 4, 6, 8]

# red dashed line with circle markers

[Link](x, y, "ro--")

[Link]("Plotting with Keyword String")

[Link]()

• Here:

• r → red

• o → circle marker

• -- → dashed line

17 | P a g e
• Colors

• r = red

• g = green

• b = blue

• c = cyan

• m = magenta

• y = yellow

• k = black

• w = white

• Line Styles

• - → solid

• -- → dashed

• : → dotted

• -. → dash-dot

• Markers

• o → circle

• s → square

• ^ → triangle up

• * → star

• d → diamond

18 | P a g e
• + → plus

• x → cross

• More Examples

[Link](x, y, "bs-") # blue solid line with square markers

[Link](x, y, "g^:") # green dotted line with triangle markers

[Link](x, y, "k*-.") # black dash-dot line with star markers

19 | P a g e
Controlling line properties
• When we use [Link](), the line’s appearance (color, width, style,
marker, etc.) can be controlled.

• Common Line Properties

Property Example Meaning

color (c) "red", "g", "#FF5733" Line color

Solid, dashed, dash-dot,


linestyle (ls) "-", "--", "-.", ":"
dotted

linewidth (lw) 2 Thickness of line

Marker at each data


marker "o", "s", "^", "*"
point

markersize (ms) 10 Size of markers

markeredgecolor
"black" Marker border color
(mec)

markerfacecolor (mfc) "yellow" Marker fill color

Example:

import [Link] as plt

# Data

x = [1, 2, 3, 4, 5]

y = [2, 5, 7, 8, 10]

# Plot with all line properties


20 | P a g e
[Link](

x, y,

color="purple", # Line color

linestyle="--", # Dashed line

linewidth=2.5, # Line thickness

marker="o", # Circle markers

markersize=12, # Marker size

markerfacecolor="yellow",# Fill color of marker

markeredgecolor="black", # Marker border color

markeredgewidth=2 # Border thickness of marker

# Add title and labels

[Link]("Controlling Line Properties")

[Link]("X-axis")

[Link]("Y-axis")

[Link]()

21 | P a g e
Plotting with categorical variable
• Normally, [Link]() is used for numeric x-
values.

• But we can also plot using categorical variables (like


names, days, products, etc.) on the x-axis.

import [Link] as plt

# categorical variable (x-axis)

fruits = ["Apple", "Banana", "Orange", "Mango"]

# numeric variable (y-axis)

sales = [40, 25, 30, 50]

[Link](fruits, sales, "ro-") # red circles with line

[Link]("Fruit Sales")

[Link]("Fruit")

[Link]("Sales")

[Link]()

• Here:

• fruits → categorical variable (x-axis).

• sales → numbers.

• Matplotlib automatically places categories at equal


spacing

22 | P a g e
Generate bio signal plotting using matplotlib and pandas

• What are Bio-signals?


• A bio-signal is any signal generated by the human body (or
other living organisms) that can be measured and analyzed.

• They are usually electrical, mechanical, or chemical in


nature.

• Bio-signals are widely used in medicine, healthcare, and


biomedical engineering to monitor body functions and
diagnose diseases
• Types of Bio-signals
• ECG (Electrocardiogram): records heart activity.
• EEG (Electroencephalogram): records brain activity.
• EMG (Electromyogram): records muscle activity.
• Generate csv file of bio siganal data with
• Columns:
• Time (s) → time in seconds (from 0 to 5 seconds, sampled at
250 Hz → 1250 points total).
• ECG (mV) → simulated ECG voltage values in millivolts.
Time (s),ECG (mV)
0.0,0.10899144224640718
0.0040032025620496394,0.8707128656883171
0.008006405124099279,0.3577416237311095

23 | P a g e
0.012009607686148917,-0.44756659129210896
0.016012810248198558,-0.4533402012195703
0.020016012810248198,0.14008664970696888
0.024019215372297835,1.0461722131493936
0.028022417934347475,0.6574841976311955
0.032025620496397116,0.04623135652856039
0.036028823058446756,0.05660974253534219
0.040032025620496396,0.617831344174252
0.044035228182546036,0.799810332183771
0.04803843074459567,0.76936739549471
0.05204163330664531,0.16347497535424993

Step 1 – Load the CSV and inspect


import pandas as pd

# Load CSV file


df = pd.read_csv("sample_data.csv")

# See first few rows


print([Link]())

Step 2 – Plot a single bio-signal

Suppose your file has Time and ECG columns:

import [Link] as plt

[Link](x="Time", y="ECG", figsize=(10,4), title="ECG Signal")


[Link]("Time (s)")
[Link]("Amplitude (mV)")
[Link](True)
[Link]()
24 | P a g e
Thank You
:: Any Query ::
Contact: Ruparel Education Pvt. Ltd.
Mobile No: 7600044051

25 | P a g e

You might also like