Lab 10
Lab 10
Lab 10
10
Implementation:
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:
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
plt.show()
Output:
Output:
Output:
axes2 = plt.subplot2grid (
(7, 1), (2, 0), rowspan = 2, colspan = 1)
Output:
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
plt.xlabel('X-Axis')
plt.show()
Output:
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.
plt.show()
Output:
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
plt.show()
Output:
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
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
marker='D', alpha=0.5)
plt.title("Tips Dataset")
plt.ylabel('Total Bill')
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
data = pd.read_csv('tips.csv')
Output:
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: