Only Matplotlib
Only Matplotlib
Matplotlib
• Matplotlib is an amazing visualization library in Python for 2D plots
of arrays. Matplotlib is a multi-platform data visualization library built
on NumPy arrays and designed to work with the broader SciPy stack.
It was introduced by John Hunter in the year 2002.
• One of the greatest benefits of visualization is that it allows us visual
access to huge amounts of data in easily digestible visuals. Matplotlib
consists of several plots like line, bar, scatter, histogram etc.
Continued…
• Basic plots in Matplotlib :
• Matplotlib comes with a wide variety of plots. Plots helps to understand trends, patterns, and to make
correlations. They’re typically instruments for reasoning about quantitative information. Some of the sample plots
are covered here.
• # importing matplotlib module
• from matplotlib import pyplot as plt
• # x-axis values
• x = [5, 2, 9, 4, 7]
• # Y-axis values
• y = [10, 5, 8, 4, 2]
• # Function to plot
• plt.plot(x,y)
• # x-axis values
• x = [5, 2, 9, 4, 7]
• # Y-axis values
• y = [10, 5, 8, 4, 2]
• # Y-axis values
• y = [10, 5, 8, 4, 2]
• # x-axis values
• x = [5, 2, 9, 4, 7]
• # Y-axis values
• y = [10, 5, 8, 4, 2]
• Installation
• To use Pyplot we must first download matplotlib module. The best way to do this is –
• Syntax :
• plt.legend()
• plt.show()
Continued…
• # Python Program to illustrate Linear Plotting • plt.plot(year, e_bangladesh, color ='g',
• linestyle ='dashed', linewidth = 2,label ='Bangladesh')
• Axes class
• Axes is the most basic and flexible unit for creating sub-plots. Axes allow placement of plots at any
location in the figure. A given figure can contain many axes, but a given axes object can only be in one
figure. The axes contain two axis objects 2D as well as, three-axis objects in the case of 3D. Let’s look at
some basic functions of this class.
• axes() function
• axes() function creates axes object with argument, where argument is a list of 4 elements [left, bottom,
width, height]. Let us now take a brief look to understand the axes() function.
• Syntax :
• Here in axes([0.1, 0.1, 0.8, 0.8]), the first ‘0.1’ refers to the distance
between the left side axis and border of the figure window is 10%, of
the total width of the figure window. The second ‘0.1’ refers to the
distance between the bottom side axis and the border of the figure
window is 10%, of the total height of the figure window. The first ‘0.8’
means the axes width from left to right is 80% and the latter ‘0.8’
means the axes height from the bottom to the top is 80%
Continued..
• add_axes() function
• Alternatively, you can also add the axes object to the figure by calling
the add_axes() method. It returns the axes object and adds axes at
position [left, bottom, width, height] where all quantities are in
fractions of figure width and height.
• Syntax :
• fig = plt.figure()
• Syntax :
• fig = plt.figure()
• Parameters:
• X is x-axis.
• Y is y-axis.
• ‘CLM’ stands for Color, Line and Marker.
• Note: Line can be of different styles such as dotted line (':'), dashed line ('—'), solid line ('-') and
many more.
Continued…
• Marker codes
• Characters Description
• ‘.’ Point Marker
• ‘o’ Circle Marker
• ‘+’ Plus Marker
• ‘s’ Square Marker
• ‘D’ Diamond Marker
• ‘H’ Hexagon Marker
Continued…
• import matplotlib.pyplot as plt •#'ro-' mentions red color, circle
• import numpy as np •# marker with solid line.
•ax2 = ax.plot(X, S, 'ro-')
•
• X = np.linspace(-np.pi, np.pi, 15) •ax.legend(labels = ('Cosine Function',
• C = np.cos(X) •'Sine Function'),
• S = np.sin(X) •loc = 'upper left')
•
• # [left, bottom, width, height] •ax.set_title("Trigonometric Functions")
• ax = plt.axes([0.1, 0.1, 0.8, 0.8]) •
•plt.show()
• # 'bs:' mentions blue color, square
• # marker with dotted line.
• ax1 = ax.plot(X, C, 'bs:')
Subplot
• To create multiple plots use matplotlib.pyplot.subplots method which returns the figure along
with Axes object or array of Axes object. nrows, ncols attributes of subplots() method
determine the number of rows and columns of the subplot grid.
• By default, it returns a figure with a single plot. For each axes object i.e plot we can set title
(set via set_title()), an x-label (set via set_xlabel()), and a y-label set via set_ylabel()).
• When we call the subplots() method by stacking only in one direction it returns a 1D array of
axes object i.e subplots.
• We can access these axes objects using indices just like we access elements of the array. To
create specific subplots, call matplotlib.pyplot.plot() on the corresponding index of the axes.
Refer to the following figure for a better understanding
Continued…
• # importing library
• import matplotlib.pyplot as plt
• # Creating 2 subplots
• fig, ax = plt.subplots(2)
• # Accessing each axes object to plot the data through returned array
• ax[0].plot(x, y)
• ax[1].plot(x, z)
Continued…
• # importing library
• import matplotlib.pyplot as plt
• import numpy as np
• Steps Needed
• Import Libraries
• Create/ Load data
• Make subplot
• Plot subplot
• Set title to subplots.
Example 1: (Using set_title() method)
We use matplotlib.axes._axes.Axes.set_title(label) method to set title (string label) for the current
subplot Axes.
• # set data with subplots and plot
• # importing packages • ax[0, 0].plot(x, x)
• import numpy as np • ax[0, 1].plot(x, x*2)
• ax[1, 0].plot(x, x*x)
• import • ax[1, 1].plot(x, x*x*x)
matplotlib.pyplot as plt
• # set the title to subplots
• # create data • ax[0, 0].set_title("Linear")
• ax[0, 1].set_title("Double")
• x=np.array([1, 2, 3, 4, • ax[1, 0].set_title("Square")
5]) • ax[1, 1].set_title("Cube")
We can also add title to subplots in Matplotlib using title.set_text() method, in similar way to
set_title() method.
• # set data with subplots and plot
• # importing packages
• ax[0, 0].plot(x, x)
• import numpy as np • ax[0, 1].plot(x, x*2)
• ax[1, 0].plot(x, x*x)
• import matplotlib.pyplot as • ax[1, 1].plot(x, x*x*x)
plt
• # set the title to subplots
• ax[0, 0].title.set_text("Linear")
• # create data • ax[0, 1].title.set_text("Double")
• x=np.array([1, 2, 3, 4, 5]) • ax[1, 0].title.set_text("Square")
• ax[1, 1].title.set_text("Cube")
• # set spacing
• # making subplots
• fig.tight_layout()
• fig, ax = plt.subplots(2, 2) • plt.show()
Thank You