UNIT-5 Data Visualization Using Dataframe
UNIT-5 Data Visualization Using Dataframe
Data Visualization:
Using visualization elements like graphs, charts, maps, etc., it becomes easier for
clients to understand the underlying structure, trends, patterns and relationships among
variables within the dataset.
Installing matplotlib
Importing matplotlib
Import matplotlib.pyplot as pt
Plotting a Line
Example: (plot4.py)
plot():
Syntax:
Pt.plot(parameter1, parameter2)
title():
Syntax:
Pt.title(“Title of a Graph”)
xlabel() and ylabel() functions are used to provide a label of x-axis and y-
axis respectively.
Syntax:
Pt.xlabel(“x-axis name”)
show():
Syntax:
Pt.show()
Example: (plot1.py)
Multple Points:
Example: (plot2.py)
Markers:
You can use the keyword argument marker to emphasize each point with a
specified marker:
Example: (plot3.py)
Marker Description
‘o’ Circle
‘*’ Star
‘.’ Point
‘,’ Pixel
‘+’ Plus
‘s’ Square
You can Specify Color for the line. You can select Color from the following list:
Color Description
‘r’ Red
‘g’ Green
‘b’ Blue
‘c’ Cyan
‘m’ Magenta
‘y’ Yellow
‘k’ Black
Line Description
Example: (plot5.py)
Example: (plot6.py)
o Syntax:
Plt.plot(x,y,ms=size)
You can use the keyword argument markeredgecolor or the shorter mec to
set the color of the edge of the markers:
o Syntax:
Plt.plot(x,y,mec=’color’)
You can use the keyword argument markerfacecolor or the shorter mfc to
set the color inside the edge of the markers:
o Syntax:
Plt.plot(x,y,mfc=’color’)
Example: (plot7.py)
Functions:
1) range():
2) legend():
A legend is an area describing the elements of the graph.
Syntax:
Plt.legend(*‘string’+,loc,ncol)
Attributes:
loc:
ncol:
Example: (plot8.py)
3) subplot():
With the subplots() function you can draw multiple plots in one figure. The
subplots() function takes three arguments that describes the layout of the figure.
The layout is organized in rows and columns, which are represented by the first
and second argument. The third argument represents the index of the current
plot.
Syntax:
Plt.subplot(rows,columns,index_current_plot)
Example:
plt.subplot(1, 2, 1)
#the figure has 1 row, 2 columns, and this plot is the first plot.
BY: Heta S. Desai Shri S.V.Patel College of CS & BM Page 14
Unit-5: Data Visualization using DataFrame
Example: (plot9.py)
Example: (plot10.py)
You can Specify Title and Supertitle of graphs using title() and suptitle() function.
Example: (plot11.py)
The bar() function takes arguments that describes the layout of the bars.
The categories and their values represented by the first and second argument as
arrays.
Syntax:
Plt.bar(x,y)
Example: (plot12.py)
Horizontal Bar:
If you want the bars to be displayed horizontally instead of vertically, use the barh()
function.
Syntax:
barh(x,y)
Note:
o You can adjust width of the bar using width parameter. Default width is
0.8.
Plt.bar(x,y,color=’r’,width=0.15)
o For horizontal bars, use height instead of width. You can also adjust the
height of the bar using height parameter. Default height is 0.8
Plt.barh(x,y,color=’k’,height=0.2)
Example: (plo13.py)
Example: (plot14.py)
Example:
You can read from the histogram that there are approximately:
The hist() function will use an array of numbers to create a histogram, the array is sent
into the function as an argument.
Syntax:
Plt.hist(data)
Example: (plot15.py)
xlim():
The xlim() function in pyplot module of matplotlib library is used to get or set the
x-limits of the current axes.
Syntax:
plt.xlim(left,right)
Parameter:
ylim():
The ylim() function in pyplot module of matplotlib library is used to get or set the
y-limits of the current axes.
Syntax:
plt.ylim(left,right)
Parameter:
Example: (plot16.py)
With Pyplot, you can use the grid() function to add grid lines to the plot. You can
use the axis parameter in the grid() function to specify which grid lines to display.
Legal values are: 'x', 'y', and 'both'. Default value is 'both'.
Syntax:
bins:
histogram displays numerical data by grouping data into "bins" of equal width.
Each bin is plotted as a bar whose height corresponds to how many data points are
in that bin. Bins are also sometimes called "intervals", "classes", or "buckets".
Example: (plot17.py)
With Pyplot, you can use the scatter() function to draw a scatter plot.
The scatter() function plots one dot for each observation. It needs two arrays of the
same length, one for the values of the x-axis, and one for values on the y-axis.
You can set your own color for each scatter plot with the color or the c argument.
colormap:
In this case 'viridis' which is one of the built-in colormaps available in Matplotlib.
You can specify the colormap with the keyword argument cmap with the value of
the colormap.
You can include the colormap in the drawing by including the plt.colorbar()
statement.
size:
You can change the size of the dots with the s argument.
BY: Heta S. Desai Shri S.V.Patel College of CS & BM Page 29
Unit-5: Data Visualization using DataFrame
alpha:
You can adjust the transparency of the dots with the alpha argument.
Syntax:
Plt.scatter(x,y,c=”color”,s=size,alpha=transparency)
Example: (plot18.py)
Example: (plot19.py)
Note:
The two plots are plotted with two different colors, by default blue and orange,
you can change colors.
Example: (plot20.py)
Example: (plot21.py)
Example: (plot22.py)
Example: (plot23.py)
1) Import module
4) Display graph
Example: (plot25.py)
Student_details.csv