0% found this document useful (0 votes)
6 views21 pages

Data Visualization Techniques in Python

The document discusses various types of data and visualization techniques using Python libraries such as Matplotlib and Seaborn. It covers plotting methods including line plots, scatter plots, bar charts, and histograms, along with examples of bivariate and univariate analysis. The document also includes code snippets for creating these visualizations with sample datasets.

Uploaded by

Pavan Kumar
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)
6 views21 pages

Data Visualization Techniques in Python

The document discusses various types of data and visualization techniques using Python libraries such as Matplotlib and Seaborn. It covers plotting methods including line plots, scatter plots, bar charts, and histograms, along with examples of bivariate and univariate analysis. The document also includes code snippets for creating these visualizations with sample datasets.

Uploaded by

Pavan Kumar
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

Types of Data

Numerical Data
Categorical Data

In [ ]:
# import the library
import numpy as np
import pandas as pd
import [Link] as plt
import seaborn as sns

[Link]('default')

2D Line plot

Bivariate Analysis
categorical -> numerical and numerical -> numerical
Use case - Time series data

In [ ]:
# plotting a simple function
price = [48000,54000,57000,49000,47000,45000]
year = [2015,2016,2017,2018,2019,2020]

[Link](year,price)
Out[ ]:

[<[Link].Line2D at 0x7fb5d79e54f0>]
In [ ]:
# from a pandas dataframe
batsman = pd.read_csv('/content/[Link]')
batsman

[Link](batsman['index'],batsman['V Kohli'])
Out[ ]:
[<[Link].Line2D at 0x7fb5d682a220>]

In [ ]:
# plotting multiple plots
[Link](batsman['index'],batsman['V Kohli'])
[Link](batsman['index'],batsman['RG Sharma'])
Out[ ]:
[<[Link].Line2D at 0x7fb5d66f6fa0>]
In [ ]:
# labels title
[Link](batsman['index'],batsman['V Kohli'])
[Link](batsman['index'],batsman['RG Sharma'])

[Link]('Rohit Sharma Vs Virat Kohli Career Comparison')


[Link]('Season')
[Link]('Runs Scored')
Out[ ]:
Text(0, 0.5, 'Runs Scored')

In [ ]:
# colors(hex) and line(width and style) and marker(size)
[Link](batsman['index'],batsman['V Kohli'],color='#D9F10F')
[Link](batsman['index'],batsman['RG Sharma'],color='#FC00D6')

[Link]('Rohit Sharma Vs Virat Kohli Career Comparison')


[Link]('Season')
[Link]('Runs Scored')
Out[ ]:
Text(0, 0.5, 'Runs Scored')
In [ ]:
[Link](batsman['index'],batsman['V Kohli'],color='#D9F10F',linestyle='solid',linewidth
=3)
[Link](batsman['index'],batsman['RG Sharma'],color='#FC00D6',linestyle='dashdot',linewi
dth=2)

[Link]('Rohit Sharma Vs Virat Kohli Career Comparison')


[Link]('Season')
[Link]('Runs Scored')
Out[ ]:
Text(0, 0.5, 'Runs Scored')

In [ ]:
[Link](batsman['index'],batsman['V Kohli'],color='#D9F10F',linestyle='solid',linewidth
=3,marker='D',markersize=10)
[Link](batsman['index'],batsman['RG Sharma'],color='#FC00D6',linestyle='dashdot',linewi
dth=2,marker='o')

[Link]('Rohit Sharma Vs Virat Kohli Career Comparison')


[Link]('Season')
[Link]('Runs Scored')
Out[ ]:
Text(0, 0.5, 'Runs Scored')

In [ ]:
# legend -> location
[Link](batsman['index'],batsman['V Kohli'],color='#D9F10F',linestyle='solid',linewidth
=3,marker='D',markersize=10,label='Virat')
[Link](batsman['index'],batsman['RG Sharma'],color='#FC00D6',linestyle='dashdot',linewi
dth=2,marker='o',label='Rohit')

[Link]('Rohit Sharma Vs Virat Kohli Career Comparison')


[Link]('Season')
[Link]('Runs Scored')

[Link](loc='upper right')
Out[ ]:
<[Link] at 0x7fb5d60124f0>
In [ ]:
# limiting axes
price = [48000,54000,57000,49000,47000,45000,4500000]
year = [2015,2016,2017,2018,2019,2020,2021]

[Link](year,price)
[Link](0,75000)
[Link](2017,2019)
Out[ ]:
(2017.0, 2019.0)

In [ ]:
# grid
[Link](batsman['index'],batsman['V Kohli'],color='#D9F10F',linestyle='solid',linewidth
=3,marker='D',markersize=10)
[Link](batsman['index'],batsman['RG Sharma'],color='#FC00D6',linestyle='dashdot',linewi
dth=2,marker='o')

[Link]('Rohit Sharma Vs Virat Kohli Career Comparison')


[Link]('Season')
[Link]('Runs Scored')

[Link]()
In [ ]:
# show
[Link](batsman['index'],batsman['V Kohli'],color='#D9F10F',linestyle='solid',linewidth
=3,marker='D',markersize=10)
[Link](batsman['index'],batsman['RG Sharma'],color='#FC00D6',linestyle='dashdot',linewi
dth=2,marker='o')

[Link]('Rohit Sharma Vs Virat Kohli Career Comparison')


[Link]('Season')
[Link]('Runs Scored')

[Link]()

[Link]()

Scatter Plots
Bivariate Analysis
numerical vs numerical
Use case - Finding correlation

In [ ]:
# [Link] simple function
x = [Link](-10,10,50)

y = 10*x + 3 + [Link](0,300,50)
y
Out[ ]:
array([199. , 70.08163265, 13.16326531, 25.24489796,
198.32653061, 40.40816327, -64.51020408, 206.57142857,
-60.34693878, -28.26530612, 23.81632653, 29.89795918,
6.97959184, 166.06122449, 136.14285714, 156.2244898 ,
-8.69387755, 204.3877551 , 66.46938776, 85.55102041,
203.63265306, 182.71428571, 139.79591837, 164.87755102,
67.95918367, 57.04081633, 190.12244898, 51.20408163,
101.28571429, 84.36734694, 31.44897959, 47.53061224,
223.6122449 , 145.69387755, 278.7755102 , 122.85714286,
258.93877551, 174.02040816, 315.10204082, 338.18367347,
363.26530612, 242.34693878, 342.42857143, 376.51020408,
98.59183673, 376.67346939, 95.75510204, 268.83673469,
309.91836735, 324. ])

In [ ]:
[Link](x,y)
Out[ ]:
<[Link] at 0x7fb5d5da8850>
In [ ]:
# [Link] on pandas data
df = pd.read_csv('/content/[Link]')
df = [Link](50)
df
Out[ ]:

batter runs avg strike_rate

0 V Kohli 6634 36.251366 125.977972

1 S Dhawan 6244 34.882682 122.840842

2 DA Warner 5883 41.429577 136.401577

3 RG Sharma 5881 30.314433 126.964594

4 SK Raina 5536 32.374269 132.535312

5 AB de Villiers 5181 39.853846 148.580442

6 CH Gayle 4997 39.658730 142.121729

7 MS Dhoni 4978 39.196850 130.931089

8 RV Uthappa 4954 27.522222 126.152279

9 KD Karthik 4377 26.852761 129.267572

10 G Gambhir 4217 31.007353 119.665153

11 AT Rayudu 4190 28.896552 124.148148

12 AM Rahane 4074 30.863636 117.575758

13 KL Rahul 3895 46.927711 132.799182

14 SR Watson 3880 30.793651 134.163209

15 MK Pandey 3657 29.731707 117.739858

16 SV Samson 3526 29.140496 132.407060

17 KA Pollard 3437 28.404959 140.457703

18 F du Plessis 3403 34.373737 127.167414

19 YK Pathan 3222 29.290909 138.046272

20 BB McCullum 2882 27.711538 126.848592

21 RR Pant 2851 34.768293 142.550000

22 PA Patel 2848 22.603175 116.625717

23 JC Buttler 2832 39.333333 144.859335

24 SS Iyer 2780 31.235955 121.132898

25 Q de Kock 2767 31.804598 130.951254

26 Yuvraj Singh 2754 24.810811 124.784776

27 V Sehwag 2728 27.555556 148.827059

28 SA Yadav 2644 29.707865 134.009123

29 M Vijay 2619 25.930693 118.614130

30 RA Jadeja 2502 26.617021 122.108346

31 SPD Smith 2495 34.652778 124.812406

32 SE Marsh 2489 39.507937 130.109775

33 DA Miller 2455 36.102941 133.569097


34 JH Kallis 2427 28.552941 105.936272
batter runs avg strike_rate
35 WP Saha 2427 25.281250 124.397745

36 DR Smith 2385 28.392857 132.279534

37 MA Agarwal 2335 22.669903 129.506378

38 SR Tendulkar 2334 33.826087 114.187867

39 GJ Maxwell 2320 25.494505 147.676639

40 N Rana 2181 27.961538 130.053667

41 R Dravid 2174 28.233766 113.347237

42 KS Williamson 2105 36.293103 123.315759

43 AJ Finch 2092 24.904762 123.349057

44 AC Gilchrist 2069 27.223684 133.054662

45 AD Russell 2039 29.985294 168.234323

46 JP Duminy 2029 39.784314 120.773810

47 MEK Hussey 1977 38.764706 119.963592

48 HH Pandya 1972 29.878788 140.256046

49 Shubman Gill 1900 32.203390 122.186495

In [ ]:
[Link](df['avg'],df['strike_rate'],color='red',marker='+')
[Link]('Avg and SR analysis of Top 50 Batsman')
[Link]('Average')
[Link]('SR')
Out[ ]:
Text(0, 0.5, 'SR')

In [ ]:
# marker

In [ ]:
# size
# size
tips = sns.load_dataset('tips')

# slower
[Link](tips['total_bill'],tips['tip'],s=tips['size']*20)

Out[ ]:
<[Link] at 0x7fb5d597f550>

In [ ]:

# scatterplot using [Link]


# faster
[Link](tips['total_bill'],tips['tip'],'o')

Out[ ]:
[<[Link].Line2D at 0x7fb5d591ac10>]

In [ ]:
# [Link] vs [Link]

Bar chart

Bivariate Analysis
Numerical vs Categorical
Use case - Aggregate analysis of groups

In [ ]:
# simple bar chart
children = [10,20,40,10,30]
colors = ['red','blue','green','yellow','pink']

[Link](colors,children,color='black')

Out[ ]:
<BarContainer object of 5 artists>
In [ ]:

# bar chart using data

In [ ]:
# horizontal bar chart
[Link](colors,children,color='black')

Out[ ]:
<BarContainer object of 5 artists>

In [ ]:

# color and label


df = pd.read_csv('/content/batsman_season_record.csv')
df

Out[ ]:

batsman 2015 2016 2017

0 AB de Villiers 513 687 216

1 DA Warner 562 848 641

2 MS Dhoni 372 284 290

3 RG Sharma 482 489 333

4 V Kohli 505 973 308

In [ ]:

[Link]([Link]([Link][0]) - 0.2,df['2015'],width=0.2,color='yellow')
[Link]([Link]([Link][0]),df['2016'],width=0.2,color='red')
[Link]([Link]([Link][0]) + 0.2,df['2017'],width=0.2,color='blue')
[Link]([Link]([Link][0]), df['batsman'])

[Link]()

In [ ]:
[Link]([Link][0])

Out[ ]:
array([0, 1, 2, 3, 4])

In [ ]:

# Multiple Bar charts

In [ ]:

# xticks

In [ ]:
# a problem
children = [10,20,40,10,30]
colors = ['red red red red red red','blue blue blue blue','green green green green green'
,'yellow yellow yellow yellow ','pink pinkpinkpink']

[Link](colors,children,color='black')
[Link](rotation='vertical')

Out[ ]:
([0, 1, 2, 3, 4], <a list of 5 Text major ticklabel objects>)
Histogram

Univariate Analysis
Numerical col
Use case - Frequency Count

In [ ]:

# simple data

data = [32,45,56,10,15,27,61]

[Link](data,bins=[10,25,40,55,70])

Out[ ]:
(array([2., 2., 1., 2.]),
array([10, 25, 40, 55, 70]),
<a list of 4 Patch objects>)
In [ ]:

# on some data
df = pd.read_csv('/content/[Link]')
df
Out[ ]:

match_id batsman_runs

0 12 62

1 17 28

2 20 64

3 27 0

4 30 10

... ... ...

136 624 75

137 626 113

138 632 54

139 633 0

140 636 54

141 rows × 2 columns

In [ ]:

[Link](df['batsman_runs'],bins=[0,10,20,30,40,50,60,70,80,90,100,110,120])
[Link]()
In [ ]:

# handling bins

In [ ]:
# logarithmic scale
arr = [Link]('/content/[Link]')
[Link](arr,bins=[10,20,30,40,50,60,70],log=True)
[Link]()

Pie Chart
Univariate/Bivariate Analysis
Categorical vs numerical
Use case - To find contibution on a standard scale

In [ ]:
# simple data
data = [23,45,100,20,49]
subjects = ['eng','science','maths','sst','hindi']
[Link](data,labels=subjects)

[Link]()

In [ ]:
# dataset
df = pd.read_csv('/content/[Link]')
df
Out[ ]:

batsman batsman_runs

0 AB de Villiers 31

1 CH Gayle 175

2 R Rampaul 0

3 SS Tiwary 2

4 TM Dilshan 33

5 V Kohli 11

In [ ]:
[Link](df['batsman_runs'],labels=df['batsman'],autopct='%0.1f%% ')
[Link]()
In [ ]:

# percentage and colors


[Link](df['batsman_runs'],labels=df['batsman'],autopct='%0.1f%% ',colors=['blue','green'
,'yellow','pink','cyan','brown'])
[Link]()

In [ ]:
# explode shadow
[Link](df['batsman_runs'],labels=df['batsman'],autopct='%0.1f%% ',explode=[0.3,0,0,0,0,0
.1],shadow=True)
[Link]()
Changing styles

In [ ]:
[Link]

Out[ ]:
['Solarize_Light2',
'_classic_test_patch',
'bmh',
'classic',
'dark_background',
'fast',
'fivethirtyeight',
'ggplot',
'grayscale',
'seaborn',
'seaborn-bright',
'seaborn-colorblind',
'seaborn-dark',
'seaborn-dark-palette',
'seaborn-darkgrid',
'seaborn-deep',
'seaborn-muted',
'seaborn-notebook',
'seaborn-paper',
'seaborn-pastel',
'seaborn-poster',
'seaborn-talk',
'seaborn-ticks',
'seaborn-white',
'seaborn-whitegrid',
'tableau-colorblind10']

In [ ]:

[Link]('dark_background')

In [ ]:

arr = [Link]('/content/[Link]')
[Link](arr,bins=[10,20,30,40,50,60,70],log=True)
[Link]()
Save figure

In [ ]:

arr = [Link]('/content/[Link]')
[Link](arr,bins=[10,20,30,40,50,60,70],log=True)

[Link]('[Link]')

You might also like