Lab 10

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

EXPERIMENT NO.

10

Program Name: Data Visualization using Matplotlib in Python Programming Language.

Implementation:

Data Visualization using Matplotlib


Data Visualization is the process of presenting data in the form of graphs or charts. It helps
to understand large and complex amounts of data very easily. It allows the decision-makers to
make decisions very efficiently and also allows them in identifying new trends and patterns
very easily. It is also used in high-level data analysis for Machine Learning and Exploratory
Data Analysis (EDA). Data visualization can be done with various tools like Tableau, Power
BI, Python.
In this article, we will discuss how to visualize data with the help of the Matplotlib library of
Python.
Matplotlib
Matploptib is a low-level library of Python which is used for data visualization. It is easy to
use and emulates MATLAB like graphs and visualization. This library is built on the top of
NumPy arrays and consist of several plots like line chart, bar chart, histogram, etc. It provides
a lot of flexibility but at the cost of writing more code.
Installation
We will use the pip command to install this module.
To install Matplotlib type the below command in the terminal.
pip install matplotlib

Pyplot
Pyplot is a Matplotlib module that provides a MATLAB-like interface. Matplotlib is
designed to be as usable as MATLAB, with the ability to use Python and the advantage of
being free and open-source. Each pyplot function makes some change to a figure: e.g., creates
a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the
plot with labels, etc. The various plots we can utilize using Pyplot are Line Plot, Histogram,
Scatter, 3D Plot, Image, Contour, and Polar.
After knowing a brief about Matplotlib and pyplot let’s see how to create a simple plot.
Adding Title
The title() method in matplotlib module is used to specify the title of the visualization
depicted and displays the title using various attributes.
Syntax:
matplotlib.pyplot.title(label, fontdict=None, loc=’center’, pad=None, **kwargs)

Example:

Prince Yadav IT3 2100270130133


import matplotlib.pyplot as plt

# initializing the data

x = [10, 20, 30, 40]


y = [20, 25, 35, 55]

# plotting the data


plt.plot(x, y)

# Adding title to the plot


plt.title("Linear graph", fontsize=25, color="green")

plt.show()

Output:

Multiple Plots
We have learned about the basic components of a graph that can be added so that it can
convey more information. One method can be by calling the plot function again and again
with a different set of values as shown in the above example. Now let’s see how to plot
multiple graphs using some functions and also how to plot subplots.
Method 1: Using the add_axes() method
The add_axes() method is used to add axes to the figure. This is a method of figure class
Syntax:
add_axes(self, *args, **kwargs)
Example:
 Python3
# Python program to show pyplot module
import matplotlib.pyplot as plt
from matplotlib.figure import Figure

# initializing the data

Prince Yadav IT3 2100270130133


x = [10, 20, 30, 40]
y = [20, 25, 35, 55]
# Creating a new figure with width = 5 inches
# and height = 4 inches
fig = plt.figure(figsize =(5, 4))
# Creating first axes for the figure
ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8])

# Creating second axes for the figure


ax2 = fig.add_axes([1, 0.1, 0.8, 0.8])

# Adding the data to be plotted


ax1.plot(x, y)
ax2.plot(y, x)

plt.show()

Output:

Method 2: Using subplot() method.


This method adds another plot at the specified grid position in the current figure.
Syntax:
subplot(nrows, ncols, index, **kwargs)
subplot(pos, **kwargs)
subplot(ax)
Example:
 Python3
import matplotlib.pyplot as plt

# initializing the data


x = [10, 20, 30, 40]
y = [20, 25, 35, 55]

Prince Yadav IT3 2100270130133


# Creating figure object
plt.figure()

# addind first subplot


plt.subplot(121)
plt.plot(x, y)

# addding second subplot


plt.subplot(122)
plt.plot(y, x)

Output:

Method 3: Using subplots() method


This function is used to create figures and multiple subplots at the same time.
Syntax:
matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
subplot_kw=None, gridspec_kw=None, **fig_kw)
Example:
 Python3
import matplotlib.pyplot as plt

# initializing the data


x = [10, 20, 30, 40]
y = [20, 25, 35, 55]

# Creating the figure and subplots

# according the argument passed

fig, axes = plt.subplots(1, 2)

# plotting the data in the


# 1st subplot
axes[0].plot(x, y)

# plotting the data in the 1st


# subplot only
axes[0].plot(y, x)

Prince Yadav IT3 2100270130133


# plotting the data in the 2nd
# subplot only
axes[1].plot(x, y)

Output:

Method 4: Using subplot2 grid() method


This function creates axes object at a specified location inside a grid and also helps in
spanning the axes object across multiple rows or columns. In simpler words, this function is
used to create multiple charts within the same figure.
Syntax:
Plt.subplot2grid(shape, location, rowspan, colspan)
Example:
 Python3
import matplotlib.pyplot as plt

# initializing the data


x = [10, 20, 30, 40]
y = [20, 25, 35, 55]

# adding the subplots


axes1 = plt.subplot2grid (
(7, 1), (0, 0), rowspan = 2, colspan = 1)

axes2 = plt.subplot2grid (
(7, 1), (2, 0), rowspan = 2, colspan = 1)

# plotting the data


axes1.plot(x, y)
axes2.plot(y, x)

Output:

Prince Yadav IT3 2100270130133


Different types of Matplotlib Plots
Matplotlib supports a variety of plots including line charts, bar charts, histograms, scatter
plots, etc. We will discuss the most commonly used charts in this article with the help of
some good examples and will also see how to customize each plot.
Note: Some elements like axis, color are common to each plot whereas some elements are
pot specific.

Line Chart

Line chart is one of the basic plots and can be created using the plot() function. It is used to
represent a relationship between two data X and Y on a different axis.
Syntax:
matplotlib.pyplot.plot(\*args, scalex=True, scaley=True, data=None, \*\*kwargs)
Example:
 Python3
import matplotlib.pyplot as plt

# initializing the data


x = [10, 20, 30, 40]
y = [20, 25, 35, 55]

# plotting the data


plt.plot(x, y)
# Adding title to the plot
plt.title("Line Chart")
# Adding label on the y-axis
plt.ylabel('Y-Axis')

# Adding label on the x-axis

plt.xlabel('X-Axis')

plt.show()

Output:

Prince Yadav IT3 2100270130133


Let’s see how to customize the above-created line chart. We will be using the following
properties –
 color: Changing the color of the line
 linewidth: Cutomizing the width of the line
 marker: For changing the style of actual plotted point
 markersize: For changing the size of the markers
 linestyle: For defining the style of the plotted line
Example:
 Python3
import matplotlib.pyplot as plt

# initializing the data


x = [10, 20, 30, 40]
y = [20, 25, 35, 55]

# plotting the data


plt.plot(x, y, color='green', linewidth=3, marker='o',
markersize=15, linestyle='--')

# Adding title to the plot


plt.title("Line Chart")

# Adding label on the y-axis


plt.ylabel('Y-Axis')
# Adding label on the x-axis
plt.xlabel('X-Axis')

plt.show()

Output:

Bar Chart

A bar chart is a graph that represents the category of data with rectangular bars with lengths
and heights that is proportional to the values which they represent. The bar plots can be
plotted horizontally or vertically. A bar chart describes the comparisons between the discrete
categories. It can be created using the bar() method.

Prince Yadav IT3 2100270130133


In the below example, we will use the tips dataset. Tips database is the record of the tip given
by the customers in a restaurant for two and a half months in the early 1990s. It contains 6
columns as total_bill, tip, sex, smoker, day, time, size.
Example:
 Python3
import matplotlib.pyplot as plt
import pandas as pd
# Reading the tips.csv file
data = pd.read_csv('tips.csv')

# initializing the data


x = data['day']
y = data['total_bill']
# plotting the data
plt.bar(x, y)
# Adding title to the plot
plt.title("Tips Dataset")
# Adding label on the y-axis
plt.ylabel('Total Bill')
# Adding label on the x-axis
plt.xlabel('Day')

plt.show()

Output:

Customization that is available for the Bar Chart –


 color: For the bar faces
 edgecolor: Color of edges of the bar
 linewidth: Width of the bar edges
 width: Width of the bar
Example:
 Python3
import matplotlib.pyplot as plt
import pandas as pd

Prince Yadav IT3 2100270130133


# Reading the tips.csv file
data = pd.read_csv('tips.csv')
# initializing the data
x = data['day']
y = data['total_bill']

# plotting the data


plt.bar(x, y, color='green', edgecolor='blue',
linewidth=2)

# Adding title to the plot


plt.title("Tips Dataset")

# Adding label on the y-axis


plt.ylabel('Total Bill')

# Adding label on the x-axis


plt.xlabel('Day')

plt.show()

Output:

Note: The lines in between the bars refer to the different values in the Y-axis of the particular
value of the X-axis.

Histogram

A histogram is basically used to represent data provided in a form of some groups. It is a


type of bar plot where the X-axis represents the bin ranges while the Y-axis gives information
about frequency. The hist() function is used to compute and create histogram of x.
Syntax:
matplotlib.pyplot.hist(x, bins=None, range=None, density=False, weights=None,
cumulative=False, bottom=None, histtype=’bar’, align=’mid’, orientation=’vertical’,
rwidth=None, log=False, color=None, label=None, stacked=False, \*, data=None, \*\
*kwargs)
Example:

Prince Yadav IT3 2100270130133


 Python3
import matplotlib.pyplot as plt
import pandas as pd
# Reading the tips.csv file
data = pd.read_csv('tips.csv')
# initializing the data
x = data['total_bill']

# plotting the data


plt.hist(x)
# Adding title to the plot
plt.title("Tips Dataset")

# Adding label on the y-axis


plt.ylabel('Frequency')

# Adding label on the x-axis


plt.xlabel('Total Bill')

plt.show()

Output:

Customization that is available for the Histogram –


 bins: Number of equal-width bins
 color: For changing the face color
 edgecolor: Color of the edges
 linestyle: For the edgelines
 alpha: blending value, between 0 (transparent) and 1 (opaque)
Example:
 Python3
import matplotlib.pyplot as plt
import pandas as pd

# Reading the tips.csv file


data = pd.read_csv('tips.csv')

Prince Yadav IT3 2100270130133


# initializing the data
x = data['total_bill']

# plotting the data


plt.hist(x, bins=25, color='green', edgecolor='blue',
linestyle='--', alpha=0.5)

# Adding title to the plot


plt.title("Tips Dataset")

# Adding label on the y-axis


plt.ylabel('Frequency')

# Adding label on the x-axis


plt.xlabel('Total Bill')

plt.show()

Output:

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:
matplotlib.pyplot.scatter(x_axis_data, y_axis_data, s=None, c=None, marker=None,
cmap=None, vmin=None, vmax=None, alpha=None, linewidths=None, edgecolors=None
Example:
 Python3
import matplotlib.pyplot as plt
import pandas as pd

# Reading the tips.csv file


data = pd.read_csv('tips.csv')

Prince Yadav IT3 2100270130133


# initializing the data
x = data['day']
y = data['total_bill']

# plotting the data


plt.scatter(x, y)
# Adding title to the plot
plt.title("Tips Dataset")
# Adding label on the y-axis
plt.ylabel('Total Bill')
# Adding label on the x-axis
plt.xlabel('Day')
plt.show()

Output:
Customizations that are available for the scatter plot are –
 s: marker size (can be scalar or array of size equal to size of x or y)
 c: color of sequence of colors for markers
 marker: marker style
 linewidths: width of marker border
 edgecolor: marker border color
 alpha: blending value, between 0 (transparent) and 1 (opaque)
 Python3
import matplotlib.pyplot as plt
import pandas as pd

# Reading the tips.csv file


data = pd.read_csv('tips.csv')

# initializing the data


x = data['day']
y = data['total_bill']

# plotting the data

Prince Yadav IT3 2100270130133


plt.scatter(x, y, c=data['size'], s=data['total_bill'],

marker='D', alpha=0.5)

# Adding title to the plot

plt.title("Tips Dataset")

# Adding label on the y-axis

plt.ylabel('Total Bill')

# Adding label on the x-axis


plt.xlabel('Day')
plt.show()

Output:

Pie Chart

Pie chart is a circular chart used to display only one series of data. The area of slices of the
pie represents the percentage of the parts of the data. The slices of pie are called wedges. It
can be created using the pie() method.
Syntax:
matplotlib.pyplot.pie(data, explode=None, labels=None, colors=None, autopct=None,
shadow=False)
Example:
 Python3

import matplotlib.pyplot as plt

Prince Yadav IT3 2100270130133


import pandas as pd

# Reading the tips.csv file

data = pd.read_csv('tips.csv')

# initializing the data


cars = ['AUDI', 'BMW', 'FORD',
'TESLA', 'JAGUAR',]
data = [23, 10, 35, 15, 12]
# plotting the data
plt.pie(data, labels=cars)
# Adding title to the plot
plt.title("Car data")
plt.show()

Output:

Customizations that are available for the Pie chart are –


 explode: Moving the wedges of the plot
 autopct: Label the wedge with their numerical value.
 color: Attribute is used to provide color to the wedges.
 shadow: Used to create shadow of wedge.
Example:
 Python3
import matplotlib.pyplot as plt
import pandas as pd

# Reading the tips.csv file


data = pd.read_csv('tips.csv')
# initializing the data
cars = ['AUDI', 'BMW', 'FORD',
'TESLA', 'JAGUAR',]
data = [23, 13, 35, 15, 12]
explode = [0.1, 0.5, 0, 0, 0]
colors = ( "orange", "cyan", "yellow",

Prince Yadav IT3 2100270130133


"grey", "green",)
# plotting the data
plt.pie(data, labels=cars, explode=explode, autopct='%1.2f%%',
colors=colors, shadow=True)
plt.show()

Output:

Saving a Plot
For saving a plot in a file on storage disk, savefig() method is used. A file can be saved in
many formats like .png, .jpg, .pdf, etc.
Syntax:
pyplot.savefig(fname, dpi=None, facecolor=’w’, edgecolor=’w’, orientation=’portrait’,
papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1,
frameon=None, metadata=None)
Example:
 Python3
import matplotlib.pyplot as plt
# Creating data
year = ['2010', '2002', '2004', '2006', '2008']
production = [25, 15, 35, 30, 10]
# Plotting barchart
plt.bar(year, production)
# Saving the figure.
plt.savefig("output.jpg")
# Saving figure by changing parameter values
plt.savefig("output1", facecolor='y', bbox_inches="tight",
pad_inches=0.3, transparent=True)
OutPut:

Prince Yadav IT3 2100270130133


Prince Yadav IT3 2100270130133

You might also like