Matplotlib_Functions
Matplotlib_Functions
Matplotlib_Functions
UNIT – IV
Topic : matplotlib
• Matplotlib is one of the most popular Python packages used for data
visualization.
• Data visualization can perform below tasks:
• It identifies areas that need improvement and attention.
• It clarifies the factors.
• It helps to understand which product to place where.
• Predict sales volumes.
Working of matplotlib: Matplotlib Architecture
• There are three different layers in the architecture of the matplotlib
which are the following:
• Backend Layer : The backend layer is the bottom layer of the figure,
which consists of the implementation of the various functions that are
necessary for plotting. There are three essential classes from the
backend layer FigureCanvas(The surface on which the figure will be
drawn), Renderer(The class that takes care of the drawing on the
surface), and Event(It handle the mouse and keyboard events).
• Artist layer : The artist layer is the second layer in the architecture. It is
responsible for the various plotting functions, like axis, which
coordinates on how to use the renderer on the figure canvas.
• Scripting layer : The scripting layer is the topmost layer on which most
of our code will run. The methods in the scripting layer, almost
automatically take care of the other layers, and all we need to care about
is the current state (figure & subplot).
Introduction to matplotlib:
• A Matplotlib figure can be categorized into various parts as below:
• Figure: It is a whole figure which may hold one or more axes (plots).
• Axis: Axises are the number of line like objects and responsible for
generating the graph limits.
• Artist: An artist is the all which we see on the graph like Text
objects, Line2D objects, and collection objects.
Introduction to matplotlib:
Important functions of matplotlib:
Important functions of matplotlib:
Important functions of matplotlib:
Types of Plots:
• There are various plots which can be created using python matplotlib.
• Some of them are listed below:
Types of Plots : Line Plot
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes()
x = np.linspace(0, 10, 1000)
ax.plot(x, np.sin(x),'r')
Bar Plot:
• Bar graphs are one of the most common types of graphs and are used to
show data associated with the categorical variables.
• Matplotlib provides a bar() to make bar graphs which accepts arguments
such as: categorical variables, their value and color.
• The scatter plots are mostly used for comparing variables when we need
to define how much one variable is affected by another variable.
• The data is displayed as a collection of points.
• Each point has the value of one variable, which defines the position on
the horizontal axes, and the value of other variable represents the
position on the vertical axis.
Scatter Plot:
import matplotlib.pyplot as plt
x = [2, 2.5, 3, 3.5, 4.5, 4.7, 5.0]
y = [7.5, 8, 8.5, 9, 9.5, 10, 10.5]
x1 = [9, 8.5, 9, 9.5, 10, 10.5, 12]
y1 = [3, 3.5, 4.7, 4, 4.5, 5, 5.2]
plt.scatter(x, y,
label='high income low saving’, color='g’)
plt.scatter(x1, y1,
label='low income high savings', color='r’)
plt.xlabel('saving*100’)
plt.ylabel('income*1000’)
plt.title('Scatter Plot’)
plt.legend()
plt.show()
Area Plot:
• Subplots are required when we want to show two or more plots in same
figure.
• The Matplotlib subplot() function is defined as to plot two or more plots in
one figure.
• We can use this method to separate two graphs which plotted in the same
axis Matplotlib supports all kinds of subplots, including 2x1 vertical, 2x1
horizontal, or a 2x2 grid.
• It accepts the three arguments: they are nrows, ncols, and index.
• It denote the number of rows, number of columns and the index.
Subplots:Example
from matplotlib import pyplot as plt
names = ['Abhishek', 'Himanshu', 'Devansh’]
marks= [87,50,98]
plt.figure(figsize=(9,3))
plt.subplot(131)
plt.bar(names, marks)
plt.subplot(132)
plt.scatter(names, marks)
plt.subplot(133)
plt.plot(names, marks)
plt.suptitle('Categorical Plotting’)
plt.show()
Subplots:Example2
import matplotlib.pyplot as plt
import numpy as np
def create_plot(ptype):
x = np.arange(-10, 10, 0.01) # x-axis
if ptype == 'linear’:
y=x
elif ptype == 'quadratic’:
y = x**2
elif ptype == 'cubic’:
y = x**3
elif ptype == 'quartic’:
y = x**4
return(x, y)
Subplots:Example2
fig = plt.figure()
plt1 = fig.add_subplot(221)
plt2 = fig.add_subplot(222)
plt3 = fig.add_subplot(223)
plt4 = fig.add_subplot(224)
x, y = create_plot('linear') plt1.plot(x, y, color ='r') plt1.set_title('y1=x’)
x, y = create_plot('quadratic') plt2.plot(x, y, color ='b') plt2.set_title('y2 = x2’)
x, y = create_plot('cubic') plt3.plot(x, y, color ='g') plt3.set_title('y3 = x3’)
x, y = create_plot('quartic') plt4.plot(x, y, color ='k') plt4.set_title('y4 = x4’)
# adjusting space between subplots
fig.subplots_adjust(hspace=.5,wspace=0.5)
plt.show()
Subplots: