0% found this document useful (0 votes)
14 views

Data Visual Iz

Uploaded by

Dhanesh S Guru
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Data Visual Iz

Uploaded by

Dhanesh S Guru
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

Data Visualization

Data Visualization refers to a graphical or visual representation of


information and data using visual elements like charts,graphs ,maps etc
It is useful in decision making.
It unveils patterns,trends,correlations.
It helps decision makers understand the meaning of data to drive
business decisions.
Using Pyplot of Matplotlib
Library
Matplotlib is a plotting library for the Python programming language.
 For Data Visualization in Python ,Matplotlib library’s Pyplot interface
is used.
Matplotlib is a python library that provides interfaces and functionality
for 2D graphics.
It visualize data from Python and publication –quality figures in many
formats.
MATLAB
MATLAB (an abbreviation of "matrix laboratory") is a high –
performance  programming language and numerical
computing environment developed by MathWorks.
MATLAB allows 
matrix manipulations,
plotting of functions and data,
 implementation of algorithms,
creation of user interfaces, and interfacing with programs written in
other languages.
Pyplot
Pyplot is a collection of methods
within matplotlib which allows user
to construct 2D plots easily and
interactively.
Importing Pyplot
import matplotlib.pyplot as pl
Types of Charts
 Line Chart
Bar Chart
Histogram Plot
Types of Charts
 Line Chart-A line chart displays information as a series of data points
called markers connected by straight line segments.It is created using
plot() function.
Types of Charts
 Bar chart:-A bar chart or bar graph is a chart that presents categorical
data with rectangular bars with heights or lengths proportional to the
values they represent.
It can be plotted vertically or horizontally.
It is created using bar() and barh()
Types of Charts
 Histogram plot:-A histogram is a type of graph that provides a visual
interpretation of numerical data by indicating the number of data points
that lie within a range of values.
It is created by hist() function.
import matplotlib.pyplot as pl
a=[1,2,3,4]
b=[2,4,6,8]
pl.plot(a,b)
pl.show()
import matplotlib.pyplot as pl
a=[1,2,3,4]
b=[2,4,6,8]
pl.plot(a,b)
pl.xlabel('find')
pl.ylabel('week')
pl.show()
To change the plot size as per your
requirements

pl.figure(figsize=(<width>,<length>))
import matplotlib.pyplot as pl
pl.figure(figsize=(15,7))
a=[1,2,3,4]
b=[2,4,6,8]
pl.plot(a,b)
pl.xlabel('find')
pl.ylabel('week')
pl.show()
import matplotlib.pyplot as pl
pl.grid(True)
a=[1,2,3,4]
b=[2,4,6,8]
pl.plot(a,b)
pl.xlabel('find')
pl.ylabel('week')
pl.show()
To change line color and style

<matplotlib.pyplot>.plot(<data1>,[,data2],<color code>)
To change line color and style

If you skip the color information in plot(),python will


plot multiple lines in the same plot with different colors
but these colors are decided internally by Python.
Character Color
‘b’ blue
‘g’ green
‘r’ red
‘m’ magenta
‘y’ yellow
‘k’ black
‘c’ cyan
‘w’ white
To change line color and style

import matplotlib.pyplot as pl
pl.grid(True)
week=[1,2,3,4]
prices=[80,90,100,120]
pl.plot(week,prices,'y')
pl.xlabel('week')
pl.ylabel('Onion prices in Rs')
pl.show()
To change line width and line
style
linewidth=<width> specifies the width value in points
Linestyle or ls=[solid | ‘dashed’ , ’dashdot’ , ’dotted’ ]
Line styles
solid Solid line
Dashed Dashed line
Dotted Dotted line
dashdot Dashdotted line
Change marker type ,size and
color
The datapoints being plotted on a graph/chart are called markers.
marker=<valid marker type>,markersize=<in points> , markeredgecolor = <valid
color>
If you do not specify marker type,then data points will not be marked specifically
on the line chart and its default type will be the same as that of the line type.
markersize is specified in points
Markeredgecolor is a valid color.
Marker Description

‘.‘ Point marker

‘ ,‘ Pixel marker

‘o‘ Circle marker

‘+‘ Plus marker

‘x‘ x marker

‘D‘ Diamond marker

‘d’ thin_diamond marker

‘s‘ Square marker

‘p‘ Pentagon marker

‘*‘ Star marker

‘ h’ Hexagon 1 marker

‘H‘ Hexagon 2 marker

‘1‘ tri_down marker


Marker Description

‘2‘ tri_up marker

‘3‘ tri_left marker

‘4‘ tri_right marker

‘v’ triangle_down marker

‘^‘ triangle_up marker

‘<‘ triangle_left marker

‘>‘ triangle_right marker

‘|‘,‘_‘ vline , hline markers


When u do not specify markeredgecolor in plot(), marker takes the
same color as the line.
If you do not specify the linestyle separately along with linecolor and
markstyle combination string,Python will only plot the markers and not
the line.
To get the line linestyle argument must be specified.
Adding Legends
Legend is a color or mark linked to a specific data range plotted.
<matplotlib.pyplot>.legend(loc=<position number or string>)
Loc arguments takes values 1,2,3,4 signifying position strings
‘upper right’,’upper left’,’lower left’,’lower right’
subplot()-to plot two lines in two different
views of the same window
subplot(2,1,2)- function specifies 2 the number of rows ,1 the number
of columns of the subplot grid along with the figure number.
import matplotlib.pyplot as pl
x=[1,2,3]
y=[5,7,4]
x2=[1,2,3]
y2=[10,11,14]
pl.subplot(2,1,1)
pl.plot(x,y,label='s1')
pl.title('performance')
pl.xlabel('week')
pl.ylabel('mark')
pl.subplot(2,1,2)
pl.plot(x2,y2,'r',label='s2')
pl.title('performance')
pl.xlabel('week')
pl.ylabel('mark')
pl.show()
arange()
Numpy arange function (np.arange) is a tool for creating numerical
sequences in Python
It returns evenly spaced numeric values within an interval stored as a
Numpy array or a list.
arange(start,stop,step,dtype)
start-indicates the beginning of range,if omitted it will be default ie 0
stop-indicates the end of range.This value will not be included in the
range.
step-specifies spacing between values in the sequence.
dtype(optional)-specifies the data type.
np.arange(0,8,2) //0,2,4,6
np.arange(12,20) //12,13,14,15,16,17,18,19
Sine wave is formed by using
sin()
import numpy as np
import matplotlib.pyplot as plt
x=np.arange(-2,1,0.01)
y=np.sin(x)
pl.plot(x,y,'r')
pl.show()
import numpy as np
import matplotlib.pyplot as plt
x=np.arange(12,20)
y=10*x+14
pl.plot(x,y,'r')
pl.show()
Bar chart
It is a graphical display of data using bars of different heights.
It can be drawn vertically and horizontally using rectangles or bars of
different height or widths.
In Pyplot bar() function is used to create a bar chart.
We can specify the sequence for x-axis and corresponding sequence to
be plotted on y-axis.
Each y value is plotted as bar on corresponding x-value on x-axis.
import matplotlib.pyplot as pl
cities=['Delhi','Mumbai','Bangalore','Hyde
rabad']
population=[23444,34566,54321,65656]
pl.bar(cities,population)
pl.title('Population in different cities')
pl.xlabel('cities')
pl.ylabel('population')
pl.show()
The first sequence given in the bar() forms the x-axis.
Second sequence values are plotted on y-axis.
Order of bars plotted may be different from the order in actual data
sequence.
Changing widths of the bar
chart
By default bar chart draws bars with equal widths of 0.8 units
To specify a common width for all
bars other than default value.
import matplotlib.pyplot as pl
cities=['Delhi','Mumbai','Bangalore','Hyderabad']
population=[23444,34566,54321,65656]
pl.bar(cities,population,width=0.4)
pl.title('Population in different cities')
pl.xlabel('cities')
pl.ylabel('population')
pl.show()
To specify different width for
different bars of a chart.
import matplotlib.pyplot as pl
cities=['Delhi','Mumbai','Bangalore','Hyderabad']
population=[23444,34566,54321,65656]
pl.bar(cities,population,width=[0.5,0.3,0.2,0.5])
pl.title('Population in different cities')
pl.xlabel('cities')
pl.ylabel('population')
pl.show()
Width values are float values
Width argument is specified as a sequence such as list or tuples
containing widths for each of the bars.
Width given in the sequence are applied from left to right but the bars
appear in the sorted order in bar chart.
The width values sequence in a bar() must have widths for all the bars
otherwise Python will report a value error.
Change colors of the bars in a
bar chart
By default a bar chart draws bars with same default colour.
To specify common color for all
bars other than default color
pl.bar(cities,population,color=‘red’)
pl.bar(cities,population,color=‘g’)
To specify different color for
different bars
pl.bar(cities,population,color=[‘red’,’b’,’m’,’yellow’])
Color values are valid color codes or names
color argument is specified as a sequence such as list or tuples
containing colors for each of the bars.
colors given in the sequence are applied from left to right
The color sequence in a bar() must have values for all the bars
otherwise Python will report value error.(shape mismatch error)
Creating a horizontal bar chart
barh() is used to create a horizontal bar chart.
Creating Multiple Bars Chart
Decide number of X points
Decide thickness of each bar and accordingly adjust X points on X-axis.
Give different color to different data ranges
The width argument remains the same for all ranges being plotted.
Plot using bar() for each range separately.
import matplotlib.pyplot as pl
import numpy as np
Info=['Gold','Silver','Bronze','Total']
India=[26,20,20,66]
Australia=[80,59,59,198]
England=[45,45,46,136]
X=np.arange(len(Info))
pl.bar(Info,India,width=0.15)
pl.bar(X+0.15,Australia,width=0.15)
pl.bar(X+0.30,England,width=0.15)
pl.show()
Setting Limits
Range of values marked and number of values marked on X-axis and Y-
axis.
Pyplot automatically finds best fitting range for X-axis and Y-axis
depending on the data being plotted.
<matplotlib.pyplot>.xlim(<xmin>,<xmax>)
<matplotlib.pyplot>.ylim(<ymin>,<ymax>)
import matplotlib.pyplot as pl
import numpy as np
X=np.arange(4)
Y=[5.0,25.0,45.0,20.0]
pl.xlim(-2.0,4.0)
pl.bar(X,Y)
pl.show()
If you swap the limits(min,max) as
(max,min) then plot gets flipped.
pl.xlim(4.0,-2.0)
Setting ticks for axes
Tick marks are individual points marked on X-axis or Y-axis.
By default Pyplot will automatically decide which data points will have
ticks on the axes.
We can decide which data points will have tick marks on X and Y axes.
xticks(<sequence containing tick data points>,[<optional sequence
containing tick labels>])
yticks(<sequence containing tick data points>,[<optional sequence
containing tick labels>])
import matplotlib.pyplot as pl
import numpy as np
X=np.arange(4)
Y=[5.0,25.0,45.0,20.0]
pl.xticks([0,2,3,4])
pl.bar(X,Y)
pl.show()
pl.bar(Info,India,width=0.15,label='India')
pl.bar(X+0.15,Australia,width=0.15,label='Australia')
pl.bar(X+0.30,England,width=0.15,label='England')
pl.legend(loc='upper left')
Saving a Figure –savefig()
pl.savefig(“multibar.pdf”) #save the plot in current directory
pl.savefig("/content/drive/My Drive/python/diagram.pdf") #save in the given path

pl.savefig("/content/drive/My Drive/python/diagram.png")

You might also like