Data Visualization
1. What is data visualization
Data visualization basically refers to the graphical or visual representation of
information and data using visual elements like charts, graphs, and map etc
When the huge amount of data came in the picture it creates the confusion to
understand. Objective is that to present the data in presentable form so people will
easily understand the data
If the data is presenting in the form of charts, graphs and map so it will be easily
understood by any one
Data should be present in very easy way or presentable way
We only concentrate on necessary or meaning full data and our time should not be
waste to consider the unwanted or unnecessary data
Presentable and concise data provide the help to make proper decision to decision
makers
2. How to use Data visualization in Python
For data visualization in python pyplot interface is used. Which comes under
Matplotlib library.
The matplot is the python library that provides many interfaces and the
functionality for 2-D graphics
Matplotlib is a high quality plotting library of Python.
Matplotlib provides many interfaces or collection of methods. Pyplot is one of
them
Pyplot is the collection of methods within matplotlib library of python. Which
allow user to construct 2D plots easily and interactively.
In other words Pyplot is one the module which comes under Matplotlib library
Pyplot offers functions for fast calculation on array and matrices.
Before Actual Discussion of Pyplot we should discuss the Numpy Library of Python
There are some reason behind that
1. Numpy offers some other useful functions to create array of data, which prove useful
while plotting data.
2. NumPy offers many useful functions
Numpy library is not preinstalled library so we have to install before use the
NumPy
Installation Procedure of NumPy
if already install so it will show requirement already satisfied
Anees Alam[7007757045] Data Visualization In Python Page 1
Note Now we can see Numpy is successfully installed with confirmation message
How to use Numpy after installation
Example how to convert List into Array using Numpy
>>> import numpy as np
>>> lst=[1,2,3,4,5]
>>> n=np.array(lst)
>>> print("List ",lst)
List [1, 2, 3, 4, 5]
>>> print("Array ",n)
Array [1 2 3 4 5]
print(n.shape) //Shape mean dimension so in 1D array it print total number of element in form or tuple
(5,)
Example how to convert List into Double Dimension Array using Numpy
d=[[1,2,3,4],[5,6,7,8],[7,8,9,4]]
>>> num=np.array(d)
>>> print(d)
[[1, 2, 3, 4], [5, 6, 7, 8], [7, 8, 9, 4]]
>>> print(num)
[[1 2 3 4]
[5 6 7 8]
[7 8 9 4]]
>>> print(num.shape)
(3, 4) 3 Row and 4 Column
Anees Alam[7007757045] Data Visualization In Python Page 2
Display the type of array
type(num)
<class 'numpy.ndarray'> num is numpy array type
Display the Data type of array which type of element is stored in num
>>> print(num.dtype)
int32
Another way to create the Array using NumPy without list support
Arrange( ) function in NumPy it is similar to range( ) function but it return the
ndarray in place of python list
num=np.arange(1,10,2,np.int32)
Description : it will start from 1 to 10 with the gap or step 2 and all the element will be
int type
>>> print(num)
[1 3 5 7 9]
Note All three values we have to give in this last value is exclusive
num=np.arange(2, 12, 2, np.float32)
>>> print(num)
[ 2. 4. 6. 8. 10.]
Linspace( ) Some time we need evenly spaced elements between two given limits
For Example
num=np.linspace(1,20,8)
>>> print(num)
[ 1. 3.71428571 6.42857143 9.14285714 11.85714286 14.57142857
17.28571429 20. ]
It will generate the 8 value between 1 to 20 with equally space in this function first and
last value is inclusive
Example 2 of linspace
num=np.linspace(1,10,5)
>>> print(num)
[ 1. 3.25 5.5 7.75 10. ]
Anees Alam[7007757045] Data Visualization In Python Page 3
Basic of Simple Plotting
Basic of Simple Plotting
Data visualization essentially means graphical representation of compiled data.
Thus graphs and charts very effective tool for data visualization.
Some commonly used charts type are
1. Line chart
2. Bar chart
3. Pie chart
Question How to create the Chart ?
Ans For creating the charts we need pyplot module and pyplot module comes
under matplotlib so we have to install matplot lib then we can use pyplot module
Now install Matplotlib
C:\>pip install matplotlib
Collecting matplotlib
Matplotlib
Matplotlib helps in customizing your data plots, building 3D plots and tackling real-
world data with ease
Matplotlib is a multi-platform data visualization tool for creating advanced-level and
interactive data visualizations that showcase insights from your datasets
Line chart
Practical Constuctrion of Line Chart
import numpy as np
import matplotlib.pyplot as pl
x=[1,2,3,4]
y=[7,5,3,2]
pl.xlabel("x")
pl.ylabel("y")
pl.plot(x,y)
pl.show()---------------This function display the chart
Output
Below
Anees Alam[7007757045] Data Visualization In Python Page 4
On the below parameters diagonal straight line will be draw
x=[1,2,3,4]
y=[7,6,5,4]
After that now we can set the Line Style, Line Width, Line Color
Every modification will be done in pl.plot function
Example 2
import matplotlib.pyplot as pl
x=[1,2,3,4] Color
y=[7,6,5,4] Thickness
pl.xlabel("X-axis")
pl.ylabel("Y-axis")
pl.plot(x, y, 'r', linewidth=2, linestyle="dotted") Shape
pl.show()
Line styles
1. dashed
2. dashdot
3. solid
Colors
1. r
2. m
3. y
4. k
5. w
6. c
7. b
Anees Alam[7007757045] Data Visualization In Python Page 5
How to set the markers in the chart[Type, size and color]
Marker style
The style can be set from the above table. There are different kind of markers are available
Marker Size
The size of the marker can also be set like 2 3, 4 5
Marker Edge Color
The color of the marker can be set by MarkerEdge
Example 3
import matplotlib.pyplot as pl
x=[1,2,3,4]
y=[7,6,5,4]
pl.xlabel("x-axis")
pl.ylabel("y-axis")
pl.plot(x,y,marker='+',markersize='7',markeredgecolor='red')
pl.show()
Anees Alam[7007757045] Data Visualization In Python Page 6
Example 4
import matplotlib.pyplot as pl
x=[1,2,3,4]
y=[7,6,5,4]
pl.xlabel("x-axis")
pl.ylabel("y-axis")
pl.plot(x,y,'r',linewidth='2',marker='+',markersize='7',markeredgecolor='blue')
pl.show()
Note:
Blue and black line I
have drawn, its not
the part of output
Bar Chart
How to construct the Bar chart using python programming language
Example 1 of Barchart
import numpy as np
import matplotlib.pyplot as pl
x=[1,2,3,4]
y=[7,6,5,4]
pl.xlabel("x-axis")
pl.ylabel("y-axis")
pl.bar(x,y,width=0.2) Width is the thickness of bar
pl.show()
Note : Output below by default color of bars are blue
Anees Alam[7007757045] Data Visualization In Python Page 7
Example 2 of Barchart
Anees Alam[7007757045] Data Visualization In Python Page 8
Note : We can set the different width of every bar
P1.bar(x,y,width=[0.2, 0.3, 0.4, 0.5])
0.2 is the thickness of first bar
0.3 is the thickness of second bar
0.4 is the thickness of third bar
0.5 is the thickness of fourth bar
How to change the color of bar
Pl.bar(x,y,width= ‘0.4’, color= ‘r’)
Anees Alam[7007757045] Data Visualization In Python Page 9
We can set the different color of every bar
How to set the multiple bar chart by logic
Anees Alam[7007757045] Data Visualization In Python Page 10
Horizontal Bar chart construction
Pie-Chart
In Pie Chart a circle is divided into sectors that each represent a proportion of
the whole.
Pie charts are generally used to show percentage or proportional data and
usually the percentage represented by each category is provided next to the
corresponding slice of pie
Pie charts are used in data handling and are circular charts divided up into
segments which each represent a value. Pie charts are divided into sections (or
'slices') to represent values of different sizes
Every slice or section represent some part or percentage of circle
Anees Alam[7007757045] Data Visualization In Python Page 11
How to construct the pie chart in python
Anees Alam[7007757045] Data Visualization In Python Page 12
The percentage can also be display in slices or sections in PIE CHART
With the help of autopct percentage can be display on every section
Every slice or section will be display percentage form means how much percentage these slices
are occupying of entire radius
Some more values are there to show the percentage
Most commonly used value is %3d%%
If we remove the percentage so % sign will be remove from the slices
Anees Alam[7007757045] Data Visualization In Python Page 13
We can set the colors of slices or segment
We have to just make the list of colors and use in pl.pie function
Explode concept in Pie Chart
Explode means suppose we want to display any slice in special way little out from the circle.
So how much we want to get it will be decided by the value
Anees Alam[7007757045] Data Visualization In Python Page 14
Explanation of explode concept
In the above example of python
India will be explode 0.2
America will be explode 0.4
France will be explode 0.0 means no
Germany will be explode 0.5
Russia will be explode 0.1
Programs based on Charts
Wap to store the 5 employee salary and name and create a bar chart to represent the Salary and
name along with the title
We can set the x-limit and y-limit in the Graph how we will see
The by default values which is coming in x-axis and y-axis it can be set according
the programmer
Anees Alam[7007757045] Data Visualization In Python Page 15
Show the value on some specific bars only
By default values display in all the bars but we can specifically display the value on some bars
by ticks method
Anees Alam[7007757045] Data Visualization In Python Page 16
How to set the Legends in Charts
Legends Placing Option in Chart
We can save our created chart just click on the save the figure button appear in the left
hand side of Chart. It will be save in the save type of Portable Network Graphics Format(png)
Anees Alam[7007757045] Data Visualization In Python Page 17
How to construct the multiline Chart for comparing the data to multiple student in a particular
year or month
Anees Alam[7007757045] Data Visualization In Python Page 18