-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram_09_Plotting.py
More file actions
255 lines (195 loc) · 8.4 KB
/
Program_09_Plotting.py
File metadata and controls
255 lines (195 loc) · 8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 3 00:45:34 2022
@author: shanu
"""
#Histogram chart
#Bar chart
#density plot
#scatter plot
#box plot
#Line Plot
# Import seaborn
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
# Apply the default theme
sns.set_theme()
'''
Statistical analyses require knowledge about the distribution of variables in your dataset.
The seaborn function displot() supports several approaches to visualizing distributions.
These include classic techniques like histograms and
computationally-intensive approaches like kernel density estimation
Distplot stands for distribution plot, it takes as input an array and
plots a curve corresponding to the distribution of points in the array.
Only for numeric data
'''
emp_data= pd.read_csv("C:\\Users\\shanu\\OneDrive\\Desktop\\Data_Science\\Python\\Week3\\employee_data.csv")
sns.distplot(emp_data['CTC'])
sns.displot(data=emp_data, x="CTC")
sns.displot(data=emp_data, x="CTC", kde=True)
#how is distribution for male and female employees
sns.displot(data=emp_data, x="CTC",col='GENDER', kde=True)
#on top of eachother
sns.displot(data=emp_data, x="CTC",hue= 'GENDER', kde=True)
#Is the above analysis true for all locations
sns.displot(data=emp_data, x="CTC",col='GENDER',hue= 'LOCATION', kde=True)
#cumulative distribution
sns.displot(data=emp_data, kind="ecdf", x="CTC", hue="GENDER", rug=True)
sns.kdeplot(data=emp_data,x="CTC",shade=True, color='b')
sns.kdeplot(data=emp_data,x="CTC",hue='GENDER',shade=True, color='b')
sns.factorplot(data=emp_data, x='ANNUAL PERFORMANCE RATING',kind="count", color='steelblue')
sns.displot(data=emp_data, x="ANNUAL PERFORMANCE RATING", kde=False)
#how is joining of employees in years
emp_data['DATE OF JOINING']=emp_data['DATE OF JOINING'].apply(lambda x: pd.to_datetime(x,format='%A, %d %B %Y'))
emp_data['year']= emp_data['DATE OF JOINING'].dt.year
g = sns.factorplot(data=emp_data,x="year", kind="count", color='steelblue')
g.set_xticklabels(step=2)
#Factor plots can be useful for this kind of visualization as well.
# This allows you to view the distribution of a parameter within bins
#defined by any other parameter
sns.factorplot(data=emp_data,x="year", kind="count", hue='GENDER')
sns.factorplot(data=emp_data,x="LOCATION", y='CTC',kind="box", hue='GENDER')
#--------------------------------------------------------------------------------
'''
Several specialized plot types in seaborn are oriented towards visualizing categorical data.
They can be accessed through catplot(). These plots offer different levels of granularity.
'''
#bar plot using catplot()
sns.catplot(data=emp_data, kind="count", x="LOCATION")
sns.catplot(data=emp_data, kind="count", x="LOCATION", col= 'GENDER')
sns.catplot(data=emp_data, kind="count", x="LOCATION", col= 'DEPT')
sns.catplot(data=emp_data, kind="count", x="DEPT")
ax = sns.catplot(data=emp_data, kind="count", x="DEPT")
locs, labels = plt.xticks()
plt.setp(labels, rotation=90)
#with barplot
employee_count= emp_data.groupby(['DEPT'], as_index=True).agg(total_employee=('EMP ID', 'count')).reset_index()
sns.barplot(data=employee_count, x="DEPT", y= 'total_employee')
ax = sns.barplot(data=employee_count, x="DEPT", y= 'total_employee')
ax.set_xticklabels(ax.get_xticklabels(),rotation = 90)
sns.factorplot(data=emp_data, kind="count", x="LOCATION")
#plots With cat and numeric
#you could show only the mean value and its confidence interval within each nested category:
sns.catplot(data=emp_data, kind="bar", x="LOCATION", y="CTC")
sns.catplot(data=emp_data, kind="bar", x="LOCATION", y="CTC", hue='GENDER')
sns.boxplot(data=emp_data,x="LOCATION", y="CTC", palette='rainbow')
sns.boxplot(data=emp_data,x="LOCATION", y="CTC", hue='GENDER')
#with overall distribution
#At the finest level, you may wish to see every observation by drawing
#a “swarm” plot: a scatter plot that adjusts the positions of the points
# along the categorical axis so that they don’t overlap
sns.catplot(data=emp_data, kind="swarm", x="LOCATION", y="CTC")
sns.catplot(data=emp_data, kind="swarm", x="LOCATION", y="CTC", hue='GENDER')
#Alternately, you could use kernel density estimation to represent the
#underlying distribution that the points are sampled from:
sns.catplot(data=emp_data, kind="violin", x="LOCATION", y="CTC")
sns.catplot(data=emp_data, kind="violin", x="LOCATION", y="CTC", hue='GENDER')
sns.catplot(data=emp_data, kind="violin", x="LOCATION", y="CTC", hue='GENDER',split=True)
#--------------------------------------------------------------------------------
'''
relational plots This plot shows the relationship between variables
using seaborn function relplot().
'''
loan_data = pd.read_csv("C:\\Users\\shanu\\OneDrive\\Desktop\\Data_Science\\Python\\Week3\\Loan_Prediction.csv")
# Create a visualization
sns.relplot(data=loan_data,x='ApplicantIncome', y='LoanAmount')
sns.relplot(
data=loan_data,
x='ApplicantIncome', y='LoanAmount', col="Married"
)
sns.relplot(
data=loan_data,
x='ApplicantIncome', y='LoanAmount', hue="Married"
)
sns.relplot(
data=loan_data,
x='ApplicantIncome', y='LoanAmount', hue="Loan_Status"
)
sns.relplot(
data=loan_data,
x='ApplicantIncome', y='LoanAmount', col="Married",
hue="Gender",
)
sns.relplot(
data=loan_data,
x='ApplicantIncome', y='LoanAmount', hue="Married",
style="Gender",
)
sns.relplot(
data=loan_data,
x='ApplicantIncome', y='LoanAmount', col="Married",
hue="Gender", style="Gender"
)
sns.relplot(
data=loan_data,
x='ApplicantIncome', y='LoanAmount', col="Married",
hue="Gender", style="Gender", size= 'Loan_Amount_Term'
)
#Statistical estimation in seaborn goes beyond descriptive statistics.
# For example, it is possible to enhance a scatterplot by including a linear regression
# model (and its uncertainty) using lmplot()
sns.lmplot(data=loan_data, x='ApplicantIncome', y='LoanAmount')
sns.lmplot(data=loan_data, x='ApplicantIncome', y='LoanAmount', hue='Gender'
)
#-------------------------------------------
#line charts with time data
sns.relplot(data=emp_data, kind="line", x="DATE OF JOINING", y= 'CTC' )
sns.relplot(data=emp_data, kind="line", x="DATE OF JOINING", y= 'CTC', hue='GENDER' )
g.set_xticklabels(step=2)
dots = sns.load_dataset("dots")
sns.relplot(
data=dots, kind="line",
x="time", y="firing_rate",facet_kws=dict(sharex=False),)
sns.relplot(
data=dots, kind="line",
x="time", y="firing_rate", col="align"
)
sns.relplot(
data=dots, kind="line",
x="time", y="firing_rate", col="align",
hue="choice", size="coherence", style="choice",
facet_kws=dict(sharex=False),
)
#Statistical estimation
'''
Often, we are interested in the average value of one variable as a
#function of other variables. Many seaborn functions will automatically
#perform the statistical estimation that is necessary to answer these questions:
When statistical values are estimated, seaborn will use bootstrapping
to compute confidence intervals and draw error bars representing
the uncertainty of the estimate.
'''
fmri = sns.load_dataset("fmri")
sns.relplot(
data=fmri, kind="line",
x="timepoint", y="signal", col="region",
hue="event", style="event",
)
#----------------------------------------------------------------------------
#Multivariate views on complex datasets
'''
Some seaborn functions combine multiple kinds of plots to quickly
give informative summaries of a dataset. One, jointplot(),
focuses on a single relationship. It plots the joint distribution
between two variables along with each variable’s marginal distribution:
'''
sns.jointplot(data=loan_data, x='ApplicantIncome', y='LoanAmount')
sns.jointplot(data=loan_data, x='ApplicantIncome', y='LoanAmount', hue= 'Loan_Status')
'''
The other, pairplot(), takes a broader view:
it shows joint and marginal distributions for all pairwise relationships
and for each variable, respectively
'''
sns.pairplot(data=loan_data)
sns.pairplot(data=loan_data, hue= 'Loan_Status')
# plotting correlation heatmap
sns.heatmap(loan_data.corr(), cmap="YlGnBu", annot=True)
## Draw the heatmap with the mask and correct aspect ratio
#sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3, center=0,
# square=True, linewidths=.5, cbar_kws={"shrink": .5})
#reference links
#https://seaborn.pydata.org/tutorial/introduction
#https://jakevdp.github.io/PythonDataScienceHandbook/04.14-visualization-with-seaborn.html
#https://vitalflux.com/correlation-heatmap-with-seaborn-pandas/
#Dump from class