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

Data Visulation

Computer science
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Data Visulation

Computer science
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Data Visualization

What is Data Visualization ?


Data visualization is the technique to present the data in a pictorial or graphical format. It
enables stakeholders and decision makers to analyze data visually. The data in a graphical
format allows them to identify new trends and patterns easily.

The main benefits of data visualization are as follows:

 It simplifies the complex quantitative information


 It helps analyze and explore big data easily
 It identifies the areas that need attention or improvement
 It identifies the relationship between data points and variables
 It explores new patterns and reveals hidden patterns in the data

Purpose of Data visualization:


 Better analysis
 Quick action
 Identifying patterns
 Finding errors
 Understanding the story
 Exploring business insights
 Grasping the Latest Trends

matplotlib Library and pyplot Interface


• The matplotlib is a python library that provides many interfaces functionally for 2D graphics
• In short we can call mattplotlib as a high quality plotting library of Python.
• The matplotlib library offers many different named collections of methods, pyplot is one such
interface.
• pyplot is a collection of methods within matplotlib which allows user to construct
2D plots easily and interactively.
Installing matplotlib

It is done using pip command in Command Prompt

pip install matplotlib

Importing PyPlot
To import Pyplot following syntax is
import matplotlib.pyplot
or
import matplotlib.pyplot as plt

After importing matplotlib in the form of plt we can use plt for accessing any function of
matplotlib

Steps to plot in matplotlib:


• Create a .py file & import matplotlib library to it using import statement
import matplotlib.pyplot as plt
• Set data points in plot( ) method of plt object
• Customize plot by setting different parameters

• Call the show() method to display the plot

• Save the plot/graph if required

Types of plot using matplotlib


• LINE PLOT
• BAR GRAPH
• HISTOGRAM etc.

Line Plot:
A line plot/chart is a graph that shows the frequency of data occurring along a number line.
The line plot is represented by a series of data points called markers connected with a
straight line. Generally, line plots are used to display trends over time. A line plot or line
graph can be created using the plot () function available in pyplot library.

We can, not only just plot a line but we can explicitly define the grid, the x and y axis scale
and labels, title and display options etc.

Line chart: displaying data in form of lines.

• We can create line graph with x coordinate only or with x and y coordinates.
• Function to draw line chart – plot()
• Default colour of line- blue
 The default width for each bar is .0.8 units, which can be changed.

• Syntax: plt.plot(x,y)
Line Plot customization
• Custom line color
plt.plot(x,y,'red')
Change the value in color argument like ‘b’ for blue,’r’,’c’,…..

• Custom line style and line width


plt.plot(x,y, linestyle='solid' , linewidth=4).
set linestyle to solid/dashed/dotted/dashdot
set linewidth as required
• Title
plt.title('DAY – TEMP Graph ') – Change it as per requirement

• Label-
plt.xlabel(‘TIme') – to set the x axis label
plt.ylabel(‘Temp') – to set the y axis label
 Changing Marker Type, Size and Color
plt.plot(x,y,'blue',marker='*',markersize=10,markeredgecolor='magenta')

Order of methods used in plot() function:


Plt.plot(x,y,color,linewidth,linestyle,marker, markersize,markeredgecolor)

Function used to show the graph – show()


plt.show( )
PROGRAM
import matplotlib.pyplot as plt
X=[1,2,3,4,5]
Y=[2,4,6,8,10]
plt.title('Simple Line Graph')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.plot(X,Y,'r')
plt.show()
Bar Graph
A graph drawn using rectangular bars to show how large each value is. The bars can be
horizontal or vertical. A bar graph makes it easy to compare data between different groups
at a glance. Bar graph represents categories on one axis and a discrete value in the other.
The goal of bar graph is to show the relationship between the two axes. Bar graph can
also show big changes in data over time.
 Syntax : plt.bar(x,y)
Bar graph customization
• Custom bar color
plt.bar(x,y, color="color code/color name")
To se different colors for different bars
plt.bar(x,y, color="color code/color name sequence")
• Custom bar width
plt.bar(x,y, width=float value)
To set different widths for different bars
plt.bar(x,y, width=float value sequence)
• Title
plt.title(' Bar Graph ') – Change it as per requirement
• Label-
plt.xlabel(‘Overs') – to set the x axis label
plt.ylabel(‘Runs') – to set the y axis label
PROGRAM :
import matplotlib.pyplot as plt
overs=['1-10','11-20','21-30','31-40','41-50']
runs=[65,55,70,60,90]
plt.xlabel('Over Range')
plt.ylabel('Runs Scored')
plt.title('India Scoring Rate')
plt.bar(overs,runs)
plt.show( )

57
HISTOGRAM

A histogram is a graphical representation which organizes a group of data points into


user specified ranges.
Histogram provides a visual interpretation of numerical data by showing the number of data
points that fall within a specified range of values (“bins”). It is similar to a vertical bar graph
but without gaps between the bars.
Difference between a histogram and a bar chart / graph –
A bar chart majorly represents categorical data (data that has some labels
associated with it), they are usually represented using rectangular bars with lengths
proportional to the values that they represent. While histograms on the other hand, is used to
describe distributions.

Creating a Histogram :

 It is a type of bar plot where X-axis represents the bin ranges while Y-axis gives information
about frequency.

 To create a histogram the first step is to create bin of the ranges, then distribute the whole
range of the values into a series of intervals, and count the values which fall into each of
the intervals.
 Bins are clearly identified as consecutive, non-overlapping intervals of variables.

 The hist() function is used to create histogram

 Syntax:
plt.hist(x,other parameters)

Optional Parameters
x array or sequence of array

bins optional parameter contains integer or


sequence or strings

histtype optional parameter used to create type


of histogram [bar, barstacked, step,
stepfilled], default is “bar”

align optional parameter controls the plotting


of histogram [left, right, mid]
orientation Optional. Possible values are
‘horizontal’ or ‘vertical’

color optional parameter used to set color or


sequence of color specs

PROGRAM :

import matplotlib.pyplot as plt


data=[7,7,7,8,8,8,8,8,9,10,10,10,11,11,12,12,12,13]
plt.xlabel('Data')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.hist(data,bins=7,color='green')
plt.show()
• Title
plt.title('Histogram ') – Change it as per requirement
• Label-
plt.xlabel(‘Data') – to set the x axis label
plt.ylabel(‘Frequency') – to set the y axis label

• Legend - A legend is an area describing the elements of the graph. In the matplotlib library
there is a function named legend() which is used to place a legend on the axes .
When we plot multiple ranges in a single plot ,it becomes necessary that legends are specified.It
is a color or mark linked to a specific data range plotted .

To plot a legend you need to do two things.

i)In the plotting function like bar() or plot() , give a specific label to the data range using label

ii)Add legend to the plot using legend ( ) as per the sytax given below .

Syntax : - plt.legend((loc=position number or string)

position number can be u1,2,3,4 specifying the position strings upper right/'upper left/'lower
left/lower right respectively .
Default position is upper right or 1

Saving the Plot

To save any plot savefig() method is used. Plots can be saved in various formats like
pdf,png,eps etc .
plt.savefig('line_plot.pdf') // save plot in the current directory
plt.savefig('d:\\plot\\line_plot.pdf') // save plot in the given path

You might also like