0% found this document useful (0 votes)
8 views27 pages

Data Visualization with Matplotlib

The document outlines a unit on Data Visualization using MatPlotLib in Python, covering various topics such as graph types, plotting techniques, and customization options. It explains how to create and export different types of plots, including line charts, bar charts, histograms, and scatter plots, while emphasizing the importance of choosing the right graph for data representation. Additionally, it includes code examples for practical implementation of these concepts.

Uploaded by

Mahesh Kumawat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views27 pages

Data Visualization with Matplotlib

The document outlines a unit on Data Visualization using MatPlotLib in Python, covering various topics such as graph types, plotting techniques, and customization options. It explains how to create and export different types of plots, including line charts, bar charts, histograms, and scatter plots, while emphasizing the importance of choosing the right graph for data representation. Additionally, it includes code examples for practical implementation of these concepts.

Uploaded by

Mahesh Kumawat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python for Data Science (PDS) (3150713)

Unit-04
Data Visualization
 Outline
Looping

✓ Introduction to MatPlotLib
✓ Graph
✓ Plot
✓ Drawing Multiple Lines and Plots
✓ Export graphs/plots to Image/PDF/SVG
✓ Axis, Ticks ad Grids
✓ Line Appearance
✓ Labels, Annotation, Legends
✓ Types of Graphs
✓ Pie Chart
✓ Bar Chart
✓ Histograms
✓ Boxplots
✓ Scatterplots
✓ Time Series
✓ Plotting Geographical data
Introduction to MatPlotLib
 Most people visualize information better when they see it in graphic versus textual format.
 Graphics help people see relationships and make comparisons with greater ease.
 Fortunately, python makes the task of converting textual data into graphics relatively easy
using libraries, one of most commonly used library for this is MatPlotLib.
 Matplotlib is a comprehensive library for creating static, animated, and interactive
visualizations in Python.

#3150713 (PDS)  Unit 04 – Data Visualization 3


Graph
 A Graph or chart is simply a visual representation of numeric data.
 MatPlotLib makes a large number of graph and chart types.
 We can choose any of the common graph such as line charts, histogram, scatter plots etc....

Line Chart Histogram Scatter Plot 3D Plot Images Bar Chart Pie Chart

Etc.......

#3150713 (PDS)  Unit 04 – Data Visualization 4


Plot
 To define a plot, we need some values, the [Link] module and an idea of
what we want to display.
[Link]
1 import [Link] as plt
2 %matplotlib inline
3 values = [5,8,9,4,1,6,7,2,3,8]
4 [Link](range(1,11),values)
5 [Link]()

 In this case, the code tells the [Link]() function to create a plot using x-axis between 1
and 11 and y-axis as per values list.

#3150713 (PDS)  Unit 04 – Data Visualization 5


Plot – Drawing multiple lines
 We can draw multiple lines in a plot by making multiple [Link]() calls.
[Link]
1 import [Link] as plt
2 %matplotlib inline
3 values1 = [5,8,9,4,1,6,7,2,3,8]
4 values2 = [8,3,2,7,6,1,4,9,8,5]
5 [Link](range(1,11),values1)
6 [Link](range(1,11),values2)
7 [Link]()

#3150713 (PDS)  Unit 04 – Data Visualization 6


Plot – Export graphs/plots
 We can export/save our plots on a drive using savefig() method.
[Link]
1 import [Link] as plt
2 %matplotlib inline
3 values1 = [5,8,9,4,1,6,7,2,3,8]
4 values2 = [8,3,2,7,6,1,4,9,8,5]
5 [Link](range(1,11),values1)
6 [Link](range(1,11),values2)
7 #[Link]()
8 [Link]('[Link]',format='png')
[Link]
 Possible values for the format parameters are
 png
 svg
 pdf
 Etc...

#3150713 (PDS)  Unit 04 – Data Visualization 7


Plot – Axis, Ticks and Grid
 We can access and format the axis, ticks and grid on the plot using the axis() method of the
[Link]
[Link]
1 import [Link] as plt
2 %matplotlib notebook
3 values = [5,8,9,4,1,6,7,2,3,8]
4 ax = [Link]()
5 ax.set_xlim([0,50])
6 ax.set_ylim([-10,10])
7 ax.set_xticks([0,5,10,15,20,25,30,35,40,45,50])
8 ax.set_yticks([-10,-8,-6,-4,-2,0,2,4,6,8,10])
9 [Link]()
10 [Link](range(1,11),values)

#3150713 (PDS)  Unit 04 – Data Visualization 8


Plot – Line Appearance
 We need different line styles in order to differentiate when having multiple lines in the same
plot, we can achieve this using many parameters, some of them are listed below.
 Line style (linestyle or ls)
 Line width (linewidth or lw)
 Line color (color or c)
 Markers (marker)
[Link]
1 import [Link] as plt
2 %matplotlib inline
3 values1 = [5,8,9,4,1,6,7,2,3,8]
4 values2 = [8,3,2,7,6,1,4,9,8,5]
5 [Link](range(1,11),values1,c='r',lw=1,ls='--',marker='>')
6 [Link](range(1,11),values2,c='b',lw=2,ls=':',marker='o')
7 [Link]()

#3150713 (PDS)  Unit 04 – Data Visualization 9


Plot – Line Appearance (Cont.)
 Possible Values for each parameters are,
Values Line Style Values Color Values Marker
‘-’ Solid line ‘b’ Blue ‘.’ Point
‘--’ Dashed line ‘g’ Green ‘,’ Pixel
‘-.’ Dash-dot line ‘r’ Red ‘o’ Circle
‘:’ Dotted line ‘c’ Cyan ‘v’ Triangle down
‘m’ Magenta ‘^’ Triangle up
‘y’ Yellow ‘>’ Triangle right
‘k’ Black ‘<’ Triangle left
‘w’ White ‘*’ Star
‘+’ Plus
‘x’ X
Etc.......

#3150713 (PDS)  Unit 04 – Data Visualization 10


Plot – Labels, Annotation and Legends
 To fully document our graph, we have to resort
the labels, annotation and legends.
 Each of this elements has a different purpose
as follows,
 Label : provides identification of a particular data
element or grouping, it will make easy for viewer to Y Label
know the name or kind of data illustrated.
 Annotation : augments the information the viewer
can immediately see about the data with notes,
sources or other useful information.
 Legend : presents a listing of the data groups within
the graph and often provides cues ( such as line
type or color) to identify the line with the data.
Annotation

Legend
X Label

#3150713 (PDS)  Unit 04 – Data Visualization 11


Plot – Labels, Annotation and Legends (Example)
[Link]
1 import [Link] as plt
2 %matplotlib inline
3 values1 = [5,8,9,4,1,6,7,2,3,8]
4 values2 = [8,3,2,7,6,1,4,9,8,5]
5 [Link](range(1,11),values1)
6 [Link](range(1,11),values2)
7 [Link]('Roll No')
8 [Link]('CPI')
9 [Link](xy=[5,1],s='Lowest CPI')
10 [Link](['CX','CY'],loc=4)
11 [Link]()

#3150713 (PDS)  Unit 04 – Data Visualization 12


Choosing the Right Graph
 The kind of graph we choose determines how people view the associated data, so choosing the
right graph from the outset is important.
 For example,
 if we want o show how various data elements contribute towards a whole, we should use pie chart.
 If we want to compare data elements, we should use bar chart.
 If we want to show distribution of elements, we should use histograms.
 If we want to depict groups in elements, we should use boxplots.
 If we want to find patterns in data, we should use scatterplots.
 If we want to display trends over time, we should use line chart.
 If we want to display geographical data, we should use basemap.
 If we want to display network, we should use networkx.
 All the above graphs are there in our syllabus and we are going to cover all the graphs in this
Unit.
 We are also going to cover some other types of libraries which is not in the syllabus like
seaborn, plotly, cufflinks and choropleth maps etc..
#3150713 (PDS)  Unit 04 – Data Visualization 13
Pie Chart
 Pie chart focus on showing parts of a whole, the entire pie would be 100 percentage, the
question is how much of that percentage each value occupies.
[Link]
1 import [Link] as plt
2 %matplotlib notebook
3 values = [305,201,805,35,436]
4 l =
['Food','Travel','Accomodation','Misc','Shoping']
5 c = ['b','g','r','c','m']
6 e = [0,0.2,0,0,0]
7 [Link](values,colors=c,labels=l,explode=e)
8 [Link]()

#3150713 (PDS)  Unit 04 – Data Visualization 14


Pie Chart (Cont.)
 There are lots of other options available with the pie chart, we are going to cover two important
parameters in this slide.
[Link]
1 import [Link] as plt
2 %matplotlib notebook
3 values = [305,201,805,35,436]
4 l =
['Food','Travel','Accomodation','Misc','Shoping']
5 c = ['b','g','r','c','m']
6 [Link](values,colors=c,labels=l,shadow=True,
7 autopct='%1.1f%%')
8 [Link]()

#3150713 (PDS)  Unit 04 – Data Visualization 15


Bar charts
 Bar charts make comparing values easy, wide bars an d segregated measurements emphasize
the difference between values, rather that the flow of one value to another as a line graph.
[Link]
1 import [Link] as plt
2 %matplotlib notebook
3 x = [1,2,3,4,5]
4 y = [5.9,6.2,3.2,8.9,9.7]
5 l = ['1st','2nd','3rd','4th','5th']
6 c = ['b','g','r','c','m']
7 w = [0.5,0.6,0.3,0.8,0.9]
8 [Link]('Sem wise spi')
9 [Link](x,y,color=c,label=l,width=w)
10 [Link]()

#3150713 (PDS)  Unit 04 – Data Visualization 16


Histograms
 Histograms categorize data by breaking it into bins, where each bin contains a subset of the
data range.
 A Histogram then displays the number of items in each bin so that you can see the distribution
of data and the progression of data from bin to bin.
[Link]
1 import [Link] as plt
2 import numpy as np
3 %matplotlib notebook
4 cpis = [Link](0,10,100)
5 [Link](cpis,bins=10,
histtype='stepfilled',align='mid',label
='CPI Hist')
6 [Link]()
7 [Link]()

#3150713 (PDS)  Unit 04 – Data Visualization 17


Boxplots
 Boxplots provide a means of depicting groups of numbers through their quartiles.
 Quartiles means three points dividing a group into four equal parts.
 In boxplot, data will be divided in 4 part using the 3 points (25th percentile, median, 75th
percentile)
Interquartile Range
(IQR)

Outliers Whiskers Whiskers Outliers

Minimum Maximum
(Q1 – 1.5 * IQR) Median (Q3 + 1.5 * IQR)

Q1 Q2 Q3
(25th Percentile) (50th Percentile) (75th Percentile)

-5 -4 -3 -2 -1 0 1 2 3 4 5
#3150713 (PDS)  Unit 04 –Data Visualization 18
Boxplot (Cont.)
 Boxplot basically used to detect outliers in the data, lets see an example where we need
boxplot.
 We have a dataset where we have time taken to check the paper, and we want to find the
faculty which either takes more time or very little time to check the paper.
[Link]
1 import pandas as pd
2 import [Link] as plt
3 %matplotlib inline
4 timetaken =
[Link]([50,45,52,63,70,21,56,68,54,5
7,35,62,65,92,32])
5 [Link](timetaken)

 We can specify other parameters like


 widths, which specify the width of the box
 notch, default is False
 vert, set to 0 if you want to have horizontal graph
#3150713 (PDS)  Unit 04 – Data Visualization 19
Scatter Plot
 A scatter plot is a type of plot that shows the data as a collection of points.
 The position of a point depends on its two-dimensional value, where each value is a position on
either the horizontal or vertical dimension.
 It is really useful to study the relationship/pattern between variables.
[Link]
1 import [Link] as plt
2 import pandas as pd
3 %matplotlib inline
4 df = pd.read_csv('[Link]')
5 [Link](df['bmi'], df['charges'])
6 [Link]()

#3150713 (PDS)  Unit 04 – Data Visualization 20


Scatter Plot (Cont.)
 To find specific pattern from the data, we can further divide the data and plot scatter plot.
 We can do this with the help of groupby method of DataFrame, and then using tuple unpacking
while looping the group.
[Link]
1 import [Link] as plt
2 import pandas as pd
3 %matplotlib inline
4 df = pd.read_csv('[Link]')
5 grouped = [Link](['smoker'])
6 for key, group in grouped:
7 [Link](group['bmi'],
group['charges'],
label='Smoke = '+key)
8 [Link]()
9 [Link]()
 Note : we can specify marker, color, and size of the marker with the help
of marker, color and s parameter respectively.
#3150713 (PDS)  Unit 04 – Data Visualization 21
Time Series
 Observations over time can be considered as a Time Series.
 Visualization plays an important role in time series analysis and forecasting.
 Time Series plots can provide valuable diagnostics to identify temporal structures like trends,
cycles, and seasonality.
 In order to create a Time Series we first need to get the date range, which can be created with
the help of datetime and pandas library.
[Link] OUTPUT
1 import pandas as pd DatetimeIndex(['2020-08-28', '2020-
08-29', '2020-08-30', '2020-08-31',
2 import datetime as dt
'2020-09-01', '2020-09-02', '2020-
3 start_date = [Link](2020,8,28) 09-03', '2020-09-04', '2020-09-
4 end_date = [Link](2020,9,05) 05'],
5 daterange = pd.date_range(start_date,end_date) dtype='datetime64[ns]', freq='D')
6 print(daterange)

#3150713 (PDS)  Unit 04 – Data Visualization 22


Time Series (Cont.)
 We can use some more parameters for date_range() function like
 freq, to specify the frequency at which we want the date range (default is ‘D’ for days)
 periods, number of periods to generate in between start/end or from start with freq.
 We can also create a date range with the help of startdate, periods and freq, for example
[Link] OUTPUT
1 import pandas as pd DatetimeIndex(['2020-08-25', '2020-
08-26', '2020-08-27', '2020-08-28',
2 import datetime as dt
'2020-08-29', '2020-08-30', '2020-
3 start_date = [Link](2020,8,28) 08-31', '2020-09-01', '2020-09-02',
4 daterange = '2020-09-03'],
5 pd.date_range(start_date,freq='D',periods=10) dtype='datetime64[ns]', freq='D')
6 print(daterange)
 Some of important possible values for the freq are
 D, for calendar day  H, for hour  B, for business day
 W, for week  T/min, for minute  SM, for semi month end
 M, for month  S, for seconds  Q, for quarter end
 Y, for year  L, for milliseconds  BQ, for business quarter end
#3150713 (PDS)  Unit 04 – Data Visualization 23
Basemap
 The matplotlib basemap toolkit is a library for plotting 2D data on maps in Python.

#3150713 (PDS)  Unit 04 – Data Visualization 24


NetworkX
 We can use networkx library in order to deal with any kind of networks, which includes social
network, railway network, road connectivity etc….
 Install
 pip install networkx
 conda install networkx
 Types of network graph
 Undirected
 Directed
 Weighted graph

#3150713 (PDS)  Unit 04 – Data Visualization 25


NetworkX (example)
[Link]
1 import networkx as nx
2 g = [Link]() # undirected graph
3 g.add_edge('rajkot','junagadh')
4 g.add_edge('junagadh','porbandar')
5 g.add_edge('rajkot','jamnagar')
6 g.add_edge('jamnagar','bhanvad')
7 g.add_edge('bhanvad','porbandar')
8 [Link](g,with_labels=True)

[Link]
1 import networkx as nx
2 gD = [Link]() # directed graph
3 gD.add_edge('Modi','Arjun')
4 gD.add_edge('Modi','GambhavaSir')
5 gD.add_edge('GambhavaSir','Modi')
6
7 [Link](gD, with_labels=True)
#3150713 (PDS)  Unit 04 –Data Visualization 26
NetworkX (cont.)
 We can use many analysis functions available in NetworkX library, some of functions are as
below
 nx.shortest_path(g,'rajkot','porbandar')
▪ Will return ['rajkot', 'junagadh', 'porbandar']
 [Link](g)
▪ Will return clustering value for each node
 nx.degree_centrality(g)
▪ Will return the degree of centrality for each node, we can find most
popular/influential node using this method.
 [Link](g)
▪ Will return the density of the graph.
▪ The density is 0 for a graph without edges and 1 for a complete graph.
 [Link](g)
▪ Return a summary of information for the graph G.
▪ The summary includes the number of nodes and edges, and their average degree.

#3150713 (PDS)  Unit 04 – Data Visualization 27

You might also like