Lecture 34 & 35 -Data Visualizationand itd.pdf

ShahidManzoor70 33 views 37 slides Aug 06, 2024
Slide 1
Slide 1 of 37
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37

About This Presentation

ojojio


Slide Content

Data Visualization
Lecture 34 & 35
1
Computer Programming- CEN1008

RECAP Lecture 33
qData Visualization

Types of Data Visualization
•Line chart
•Bar chart
•Pie chart
•Histogram

Example-1
•Let’s say you want to compare sales of jackets to sales of socks over
the course of the previous year. There’s more than one way to present the data, and tables are one of the most common. Here’s what this
would look like:
Source:
https://www.microstrategy.com/us/resources/introductory-guides/data-visualization-
what-it-is-and-why-we-use-it

0
0.5
1
1.5
2
2.5
3
3.5
024681012
Current (mA)
Voltage (V)
V-I relationship
Example-2 (V-I relationship)

Example-3 (Diode Forward Biased Curve)
Sr.N
o
Diode
Voltage
Diode
Current
10.00.0
20.290.0
30.540.44
40.570.85
50.601.50
60.611.87
70.622.76
80.643.57
90.654.48
100.665.69

Matplotlib
•Matplotlib is a plotting library for the Python programming language.
•Matplotlib is one of the most popular Python packages used for data visualization.
•Types of Plots
•Line graph
•Bar graph
•Scatter plot
•Pie chart
•Histogram
•Area plot

Simple line graph
import matplotlib.pyplot as plt
X = [1,2,3]
Y = [2,3,1]
plt.plot(X,Y)
Output
plt.show()

Graph Template
Source:
https://www.simplilearn.com/data-visualization-in-python-using-matplotlib-tutorial

Simple line graph (Adding labels)
import matplotlib.pyplot as plt
X = [1,2,3]
Y = [2,3,1]
plt.plot(X,Y)
Output
plt.show()
plt.xlabel(‘X-axis’)
plt.ylabel(‘Y-axis’)
plt.title(‘Graph’)

Simple line graph (Linewidth)
plt.plot(X,Y,linewidth=5)

Simple line graph (Plotting two or more lines)
import matplotlib.pyplot as plt
X1 = [1,2,3]
Y1 = [2,3,1]
plt.plot(X1,Y1,label=‘Line-1’,linewidth=4)
plt.show()
plt.xlabel(‘X-axis’)
plt.ylabel(‘Y-axis’)
plt.title(‘Two lines graph’)
X2 = [1,2,3]
Y2 = [3,2,3]
plt.plot(X2,Y2,label=‘Line-2’,linewidth=4)
plt.legend()

Simple line graph (Plotting two or more lines)

Simple line graph (Adding Grid)
plt.grid()

Simple line graph (Customized plot)
import matplotlib.pyplot as plt
X1 = [1,2,3,4]
Y1 = [2,1,3,1]
plt.plot(x, y, color='green', linestyle='dashed',
linewidth = 3,marker='o', markerfacecolor='blue',
markersize=12)
plt.show()
plt.xlabel(‘X-axis’)
plt.ylabel(‘Y-axis’)
plt.title(‘Graph’)

Simple line graph (Customized plot)

Simple line graph (Customized plot)
plt.ylim(1,4)
plt.xlim(1,4)

Scatter Plot (Example)
import matplotlib.pyplot as plt
x = [1,2,3,4,5,6]
y = [2,4,5,9,11,12]
plt.scatter(x, y, label= "stars", color= "green",
marker= "*", s=60)
plt.show()
plt.xlabel(‘X-axis’)
plt.ylabel(‘Y-axis’)
plt.title(‘Scatter plot’)
plt.legend()

Scatter Plot

Scatter Plot
import matplotlib.pyplot as plt
x1 = [1,1.5,2,2.5,3,3.5,3.6]
y1 = [6,7,8,8.2,8.5,9,9.5]
plt.scatter(x1,y1, label='High income',color='g')
marker= "*", s=60)
plt.show()
plt.xlabel(‘X-axis’)
plt.ylabel(‘Y-axis’)
plt.title(‘Scatter plot’)
plt.legend()
x2=[8,8.5,9,9.5,10,10.5,11]
y2=[2,2.5,2.7,3,3.5,4,5]
plt.scatter(x2,y2,label='Low income',color='r')

Scatter Plot (Example)

Bar Chart Example

Bar Chart
import matplotlib.pyplot as plt
X = [1, 2, 3, 4]
label = ['A', 'B', 'C', 'D']
plt.show()
plt.xlabel(‘X-axis’)
plt.ylabel(‘Y-axis’)
plt.title(‘Bar chart’)
plt.bar(X, Y, tick_label = label, width = 0.5)
Y = [10, 20, 25, 20]

Bar Chart
For horizontal graph use barh() function

Bar Chart
import matplotlib.pyplot as plt
X1 = [0.25,1.5,2.75,4.0,5.25]
Y1 = [50,40,70,80,20]
plt.bar(X1,Y1,label="Honda",width=.5)
plt.bar(X2,Y2,label="Toyota",
color='green',width=.5)
plt.show()
plt.xlabel(‘Days’)
plt.ylabel(‘Distance (Km)’)
plt.title(‘Comparison’)
Y2 = [80,20,20,50,60]
plt.legend()
X2 = [.75,2.0,3.25,4.5,5.75]

Bar Chart

Bar Chart
import matplotlib.pyplot as plt
Position = [1,2,3,4,5]
Y1 = [50,40,70,80,20]
plt.bar(Position,Y1,label="Honda",width=.5)
plt.bar(Position,Y2,label="Toyota",color='green',
width=.5,bottom = Y1)
plt.show()
plt.xlabel(‘X-axis’)
plt.ylabel(‘Y-axis’)
plt.title(‘Comparison’)
Y2 = [80,20,20,50,60]
plt.legend()

Bar Chart

Pie Chart
Source: https://www.originlab.com/doc/Origin-Help/2D-Pie-Chart

Pie Chart
import matplotlib.pyplot as plt
companies = ['A', 'B', 'C', 'D']
share = [30,20,25,25]
colors = ['r', 'y', 'g', 'b']
plt.show()
plt.pie(share, explode = explode, labels = companies,
colors=colors, startangle=90, radius = 1.2,
autopct = '%1.1f%%')
explode = [0.0,0.0,0.0,0.0]
plt.legend()

Pie Chart

Pie Chart
explode = [0.0,0.1,0.0,0.0]
explode = [0.0,0.1,0.1,0.0]

Histogram chart
Source: https://statistics.laerd.com/statistical-guides/understanding-histograms.php

Histogram
import matplotlib.pyplot as plt
Age = [2,5,7,12,15,25,31,35,43,40,44,56,66,76,85]
range = (0,100)
plt.hist(Age, Bins, range, color = 'purple',
histtype = 'bar', rwidth = 0.8)
plt.show()
Bins = 10
plt.ylabel('Number of People')
plt.xlabel('Age')
plt.title('Histogram')

Histogram

Bins = 4
Histogram
Tags