0% found this document useful (0 votes)
2 views11 pages

Data Visualization Using Matplotlib in Python

Matplotlib is a Python library for creating various types of data visualizations, including line charts, bar charts, histograms, scatter plots, pie charts, box plots, and heatmaps. The document provides syntax and examples for each plot type using the Pyplot module, demonstrating how to visualize data effectively with minimal code. It covers essential parameters and includes sample code for practical implementation.

Uploaded by

hk37444045
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)
2 views11 pages

Data Visualization Using Matplotlib in Python

Matplotlib is a Python library for creating various types of data visualizations, including line charts, bar charts, histograms, scatter plots, pie charts, box plots, and heatmaps. The document provides syntax and examples for each plot type using the Pyplot module, demonstrating how to visualize data effectively with minimal code. It covers essential parameters and includes sample code for practical implementation.

Uploaded by

hk37444045
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

Data Visualization using Matplotlib in Python

Matplotlib is a Python library used for creating static, animated and


interactive data visualizations. It is built on the top of NumPy and it
can easily handles large datasets for creating various types of plots
such as line charts, bar charts, scatter plots, etc.
Visualizing Data with Pyplot using Matplotlib
Pyplot is a module in Matplotlib that provides a simple interface for
creating plots. It allows users to generate charts like line graphs,
bar charts and histograms with minimal code. Let’s explore some
examples with simple code to understand how to use it effectively.
1. Line Chart
Line chart is one of the basic plots and can be created using plot()
function. It is used to represent a relationship between two data X
and Y on a different axis.
Syntax:
[Link](x, y)
Parameter: x, y Coordinates for data points.
Example: This code plots a simple line chart with labeled axes and
a title using Matplotlib.
import [Link] as plt

x = [10, 20, 30, 40]


y = [20, 25, 35, 55]

[Link](x, y)
[Link]("Line Chart")
[Link]('Y-Axis')
[Link]('X-Axis')
[Link]()
Output

2. Bar Chart
Bar chart displays categorical data using rectangular bars whose
lengths are proportional to the values they represent. It can be
plotted vertically or horizontally to compare different categories.
Syntax:
[Link](x, height)
Parameter:
• x: Categories or positions on x-axis.
• height: Heights of the bars (y-axis values).
Example: This code creates a simple bar chart to show total bills
for different days. X-axis represents the days and Y-axis shows
total bill amount.
import [Link] as plt
x = ['Thur', 'Fri', 'Sat', 'Sun']
y = [170, 120, 250, 190]
[Link](x, y)
[Link]("Bar Chart")
[Link]("Day")
[Link]("Total Bill")
[Link]()
Output

3. Histogram
Histogram shows the distribution of data by grouping values into
bins. The hist() function is used to create it, with X-axis showing
bins and Y-axis showing frequencies.
Syntax:
[Link](x, bins=None)
Parameter:
• x: Input data.
• bins: Number of bins (intervals) to group data.
Example: This code plots a histogram to show frequency
distribution of total bill values from the list x. It uses 10 bins and
adds axis labels and a title for clarity.
import [Link] as plt

x = [7, 8, 9, 10, 10, 12, 12, 12, 13, 14, 14, 15, 16, 16, 17, 18, 18,
19, 20, 20,
21, 22, 23, 24, 25, 25, 26, 28, 30, 32, 35, 36, 38, 40, 42, 44, 48,
50]

[Link](x, bins=10, color='steelblue')


[Link]("Histogram")
[Link]("Total Bill")
[Link]("Frequency")
[Link]()
Output
4. Scatter Plot
Scatter plots are used to observe relationships between variables.
The scatter() method in the matplotlib library is used to draw a
scatter plot.
Syntax:
[Link](x, y)
Parameter: x, y Coordinates of the points.
Example: This code creates a scatter plot to visualize the
relationship between days and total bill amounts using scatter().
import [Link] as plt

x = ['Thur', 'Fri', 'Sat', 'Sun', 'Thur', 'Fri', 'Sat', 'Sun']


y = [170, 120, 250, 190, 160, 130, 240, 200]

[Link](x, y)
[Link]("Scatter Plot")
[Link]("Day")
[Link]("Total Bill")
[Link]()
Output

5. Pie Chart
Pie chart is a circular chart used to show data as proportions or
percentages. It is created using the pie(), where each slice (wedge)
represents a part of the whole.
Syntax:
[Link](x, labels=None, autopct=None)
Parameter:
• x: Data values for pie slices.
• labels: Names for each slice.
• autopct: Format to display percentage (e.g., '%1.1f%%').
Example: This code creates a simple pie chart to visualize
distribution of different car brands. Each slice of pie represents the
proportion of cars for each brand in the dataset.
import [Link] as plt
import pandas as pd

cars = ['AUDI', 'BMW', 'FORD','TESLA', 'JAGUAR',]


data = [23, 10, 35, 15, 12]

[Link](data, labels=cars)
[Link](" Pie Chart")
[Link]()
Output
Pie Chart
6. Box Plot
Box plot is a simple graph that shows how data is spread out. It
displays the minimum, maximum, median and quartiles and also
helps to spot outliers easily.
Syntax:
[Link](x, notch=False, vert=True)
Parameter:
• x: Data for which box plot is to be drawn (usually a list or
array).
• notch: If True, draws a notch to show the confidence interval
around the median.
• vert: If True, boxes are vertical. If False, they are horizontal.
Example: This code creates a box plot to show the data
distribution and compare three groups using matplotlib
import [Link] as plt

data = [ [10, 12, 14, 15, 18, 20, 22],


[8, 9, 11, 13, 17, 19, 21],
[14, 16, 18, 20, 23, 25, 27] ]

[Link](data)
[Link]("Groups")
[Link]("Values")
[Link]("Box Plot")
[Link]()
Output
7. Heatmap
Heatmap is a graphical representation of data where values are
shown as colors. It helps visualize patterns, correlations or intensity
in a matrix-like format. It is created using imshow() method in
Matplotlib.
Syntax:
[Link](X, cmap='viridis')
Parameter:
• X: 2D array (data to display as an image or heatmap).
• cmap: Sets the color map.
Example: This code creates a heatmap of random 10×10 data
using imshow(). It uses 'viridis' color map and colorbar() adds a
color scale.
import [Link] as plt
import numpy as np

[Link](0)
data = [Link](10, 10)

[Link](data, cmap='viridis', interpolation='nearest')

[Link]()
[Link]('X-axis Label')
[Link]('Y-axis Label')
[Link]('Heatmap')
[Link]()
Output

Heatmap
Explanation:
• [Link](0): Ensures same random values every
time (reproducibility).
• [Link](10, 10): Generates a 10×10 array of
random numbers between 0 and 1.

You might also like