1
[Link] HONOURS
YEAR II SEM UNIT – V
PLOTTING AND VISUALIZATION
A Brief Matplot lib API PRIMER: Making plots and static or interactive visualizations is
one of the most important tasks in data analysis.
matplotlib is a (primarily 2D) desktop plotting package designed for creating publication-
quality plots. The project was started by John Hunter in 2002 to enable a MATLAB- like
plotting interface in Python. He, Fernando Pérez (of aIPython), and others have collaborated
for many years since then to make IPython combined with matplotlib a very functional and
productive environment for scientific computing. When used in tandem with a GUI toolkit
(for example, within IPython), matplotlib has interactive features like zooming and panning.
It supports many different GUI backends on all operating systems and additionally can
export graphics to all of the common vector and raster graphics formats: PDF, SVG, JPG,
PNG, BMP, GIF, etc.
Befor we start visualizing the graphs and charts first we have to install matplotlib module in
command prompt, to install this module use the below command as
C:>/pip install matplotlib
Then the following module will be installed in your system.
Pandas is a python library useful for data cleaning, modeling and
exploration.
Steps involved in data visualization
Data visualization is a fancy word which essentially comprises only of these 3 basic steps:
1. Importing the required libraries (such as matplotlib, seaborn, etc)
2. Getting the data ready — normally reading from a csv file or a json data and creating
a table ( dataframe is the technical word)
3. Graphical representation — Plotting — choose the type of plot.
Step 1: The libraries: Pandas visualization based on matplotlib API can be used to create
decent plots such as bar graphs, histograms, scatter plots, etc. There are other advanced
visualization libraries such as seaborn, bokeh, etc for advanced techniques such as 3D
modelling, live-streaming graphs, maps, etc. Let’s first master matplotlib .
2
[Link] HONOURS
YEAR II SEM
First we have to import matplotlib in the program, there are many sub function which
helps to plot the graphs and bars
Figures and Subplots: The Plots in matplotlib reside within a Figure object. You can
create a new figure with
[Link]:
eg: from matplotlib import pyplot as plt
fig1=[Link]()
In the below diagram we can see a figure which is used to draw graphs
NOTE: Every time if you want to execute the plots on the figure we have to use a
command in ipython as
From matplotlib import pyplot as plt
%matplotlib inline
When ever you have stopped working on ipython then we will not get any figure on screen
so when you have stopped working on ipyhon for some time and again comes back to
ipython to execute subplots then again we have to execute the above commands then only
subplots and charts will be seen to you.
3
[Link] HONOURS
YEAR II SEM
Now if you want to plot a figure on this empty screen there is method called sub_plot. This
method helps to plot a diagram on white screen with x-axis and
y-axis.
To draw a sub_plot on figure it will be as follows:
Eg: from matplotlib import pyplot as plt
Fig1=[Link]()
Ax1=fig1.add_subplot(2,2,1) then we will get below output as follows
4
[Link] HONOURS
YEAR II SEM
In the same way if you want to add second sub_plot it will be as follows
Ax2=fig1.add_subplot(2,2,2)
Like that if you want more images we plot no of subplots on the figure.
[Link](): this method draws some lines and dark spots on the list figure which we use on
the screen.
5
[Link] HONOURS
YEAR
NowII ISEM
would like to draw some black line on the figure in sub_plot then we use this as
follows
Eg: from [Link] import randn
Ax2=[Link](randn(50),cumsum(),’k—‘)
In the above query the k— is a style option which will gets the black dashed line on the
subplot
.hist(): the .hist method is mainly useful to draw a histogram on the subplot as follows
6
[Link] HONOURS
YEAR
ThereII SEM
are no ofoptions can be passed as an argument to the plot function to perform
different operations.
Argument Description
nrows: the number of rows the Figure should have.
ncols: the number of columns the Figure should have.
plot_number : which refers to a specific plot in the Figure.
.title(): this method helps to put heading in the plot
.set_xlabel(): this method helps to put heading on xlabel
.set_ylabel(): this method helps to put heading on y label
Using .subplot() we will create a two plots on the same canvas:
COLORS:
If you want to specify the subplot in colors then we can give different colors which we
required in a option called plot. This plot function takes three arguments it takes x-
argument,y-argument and the color can be specified as follows:
7
[Link] HONOURS
YEAR II SEMmatplotlib
Eg: from import pyplot as plt
Fig1=[Link]()
X=[1,2,3]
Y=[5,6,7]
Ax=fig1.add_axes([0,0,1,1])
[Link](x,y,’red’) then we will get the below figure as follows
And also we can specify color in the form of “g—“ which is green color and ‘r--“ which is
red color
[Link] this link will be helpful to us.
MARKERS: Line plots can additionally have markers to highlight the actual data points.
Since matplotlib creates a continuous line plot, interpolating between points, it can
occasionally be unclear where the points lie. The marker can be part of the style string,
which must have color followed by marker type. Different styles can be used for markers
through lines. Some of the options are as follows
Eg: [Link](range(10), linestyle='--', marker='o', color='b')
8
[Link] HONOURS
YEAR II SEM
TICKS: Ticks are the markers denoting data points on axes. Matplotlib's default tick
locators and formatters are designed to be generally sufficient in many common
situations. Position and labels of ticks can be explicitly mentioned to suit specific
requirements. Ticks are of two types [Link]() [Link]().
The xticks() and yticks() function takes a list object as argument. The elements in the list
denote the positions on corresponding action where ticks will be displayed.
Eg: ax.set_xticks([2,4,6,8])
9
[Link] HONOURS
YEAR II SEM
Ax.set_yticks([10,20,30])
This method will mark the data points at the given positions with ticks.
Eg: from matplotlib import pyplot as plt
Import pandas as pd
%matplotlib inline
Fig=[Link]()
Ax=fig.add_axes([0.1,0.1,0.8,0.8])
Ax.set_xticks([2,4,6,8])
Ax.set_yticks([10,20,30])
Output:
Similarly, labels corresponding to tick marks can be set
by set_xlabels() and set_ylabels() functions respectively.
ax.set_xlabels([‘two’, ‘four’,’six’, ‘eight’, ‘ten’])
This will display the text labels below the markers on the x axis.
10
[Link] HONOURS
YEAR
LINE II SEM
STYLES: Similarly, the line style can be adjusted using the line style keyword .
This linestyle specifies the style to be draw on the figure. There can be different values for
the keyword linestyle, some of the values are as follows
Linestyle=’solid/dashed/dashdot/dotted’ and this values can be set to figure as follows eg:
from matplotlib import pyplot as plt
Import pandas as pd
Fig=[Link]()
Ax=fig.add_axes([0.1,0.1,0.8,0.8])
X=[2,3,4]
Y=[11,12,13]
[Link](x,y,color=’purple’,linestyle=”dashed”)
Then we will get the following output as follows
And also we can set the line style still in different ways as follows
[Link](x, x + 4, linestyle='-') # solid
[Link](x, x + 5, linestyle='--') # dashed
[Link](x, x + 6, linestyle='-.') # dashdot
11
[Link] HONOURS
YEAR II SEM x + 7,
[Link](x, linestyle=':'); # dotted
and also one more way to set the linestyles are as follows:
[Link](x, x + 0, '-g') # solid green
[Link](x, x + 1, '--c') # dashed cyan
[Link](x, x + 2, '-.k') # dashdot black
[Link](x, x + 3, ':r'); # dotted red
DRAWING SHAPES ON SUBLOTS: we can also draw no of shapes on subplots like
rectangle, circle and pentagon with the help of built in function like
[Link],[Link],[Link] by using this function we can add to subplot with the
help of add_patch(). The add_patch() method helps to add images on to the figure.
Eg: fig = [Link]()
ax = fig.add_subplot(1, 1, 1)
rect = [Link]((0.2, 0.75), 0.4, 0.15, color='k', alpha=0.3)
circ = [Link]((0.7, 0.2), 0.15, color='b', alpha=0.3)
pgon = [Link]([[0.15, 0.15], [0.35, 0.4], [0.2, 0.6]],color='g', alpha=0.5)
ax.add_patch(rect)
ax.add_patch(circ)
ax.add_patch(pgon)
12
[Link] HONOURS
YEAR II SEM
SAVING PLOT TO FILE: We can also save the plot which we have drawn, we can save
this plot in any extension, before we save the plot we will draw a simple plot as follows
Eg: import matplotlib
import [Link] as plt
import numpy as np
y = [2,4,6,8,10,12,14,16,18,20]
x = [Link](10)
fig = [Link]()
ax = [Link](111)
[Link](x, y, label='$y = numbers')
[Link]('Legend inside')
[Link]()
#[Link]()
After executing the above code we will get the below diagram as follows
13
[Link] HONOURS
YEAR II SEM
Save figure:
savefig() method: this method saves the plot or figure to specified destination.
The method can be used like this:
Savefig(path)
Save as PNG File
[Link]('D://[Link]')
Now the above command when we have executed the plot will be saves in “D” drive as png
file.
Save as PDF File:
If you want to export a graph with matplotlib, you will always call .savefig(path).
matplotlib will figure out the file type based on the passed file path .
For example, if you want to save the above plot in a PDF file:
[Link]('D://[Link]')
This will save the plot in line_plot.pdf. You can view all output files here.
Save as SVG File
If you want to save the plot as a SVG file instead, you use the same .savefig(path) method,
but change the file ending to .svg:
14
[Link] HONOURS
YEAR II SEM
[Link]('D://[Link]')
Both PDF and SVG are vector-based file formats and save the plot in excellent quality.
However, some software does not easily support these modern formats (looking at you,
PowerPoint) and requires you to export plots as images. Luckily, this is not a problem with
matplotlib.
Save as JPG File
The final export options you should know about is JPG files, which offers better
compression and therefore smaller file sizes on some plots.
[Link]('line_plot.jpg', dpi=300)
Data aggregation
:
Data aggregation is the process where data is collected and presented in a summarized
format for statistical analysis and to effectively achieve business objectives. Data
aggregation is vital to data warehousing as it helps to make decisions based on vast
amounts of raw data. Data aggregation provides the ability to forecast future trends and
aids in predictive modeling. Effective data aggregation techniques help to minimize
performance problems.
Aggregation provides more information based on related clusters of data such as an
individual’s income or profession. For example, a store may want to look at the sales
performance for different regions, so they would aggregate the sales data based on region.
Queries with aggregation (with mathematical functions) provide faster results. For example,
the query for the sum of sales of a product in a month brings up faster results than the query
for sales of the product in general. This is because the aggregation is applied on the former
query and only the sum is displayed, while the latter query brings up individual records.
Faster queries imply the better performance of the system.
Types of aggregation with mathematical functions:
Sum—Adds together all the specified data to get a total.
Average—Computes the average value of the specific data.
Max—Displays the highest value for each category.
Min—Displays the lowest value for each category.
Count—Counts the total number of data entries for each category.
Python is a great language for doing data analysis, primarily because of the fantastic
ecosystem of data-centric python packages. Pandas is one of those packages and makes
importing and analyzing data much easier.
15
[Link] HONOURS
YEAR II SEM
Pandas groupbyis used for grouping the data according to the categories and apply a
function to the categories. It also helps to aggregate data efficiently.
Pandas [Link]() function is used to split the data into groups based on some
criteria. pandas objects can be split on any of their axes. The abstract definition of grouping
is to provide a mapping of labels to group names.
Syntax: [Link](by=None, axis=0, level=None, as_index=True, sort=True,
group_keys=True, squeeze=False, **kwargs)
importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.read_csv("[Link]")
# Print the dataframe
Df
# Applying aggregation across all the columns
# sum and min will be found for each
# numeric type column in df dataframe
[Link](['sum', 'min'])
output:
16
[Link] HONOURS
YEAR II SEM
# importing pandas package
import pandas as pd
# making data frame from csv file
df = pd.read_csv("[Link]")
# We are going to find aggregation for these columns
[Link]({"Number":['sum', 'min'],
"Age":['max', 'min'],
"Weight":['min', 'sum'],
"Salary":['sum']})
Output:
**********************UNIT – V OVER**********************