-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataVisualizationAndReport.py
More file actions
150 lines (101 loc) · 2.81 KB
/
DataVisualizationAndReport.py
File metadata and controls
150 lines (101 loc) · 2.81 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
# -*- coding: utf-8 -*-
"""DataVisualizationAndReport.ipynb adlı not defterinin kopyası
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1dInIYUbSimYfnotiIetLWBcMOMoMxAFL
"""
##Report Project
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
sns.set ( rc = {'figure.figsize': (5,5)})
df = pd.read_csv('world-happiness-report.csv')
df.head(14)
df.info()
df ['Country name'].value_counts()
df.describe().T
df.describe()
from os import mkdir
try:
mkdir('Plots')
except:
pass
mypath = 'Plots'
f, axes = plt.subplots(5,2 , figsize=(20,30))
f.tight_layout(pad=8)
f.suptitle('Histogram Plots')
cols = df.select_dtypes(exclude='object').columns
x_axes = 0
y_axes = 0
for col in cols :
sns.histplot(data=df, x=col, kde=True, ax=axes[x_axes, y_axes])
if y_axes == 1:
y_axes= 0
x_axes +=1
else:
y_axes +=1
plt.savefig('Plots/histogram_plots.png')
plt.show()
f, axes = plt.subplots(5,2 , figsize=(20,30))
f.tight_layout(pad=8)
f.suptitle('Histogram Plots')
cols = df.select_dtypes(exclude='object').columns
x_axes = 0
y_axes = 0
for col in cols :
sns.histplot(data=df, x=col, kde=True, ax=axes[x_axes, y_axes])
if y_axes == 1:
y_axes= 0
x_axes +=1
else:
y_axes +=1
plt.show()
f, axes = plt.subplots(5,2 , figsize=(20,30))
f.tight_layout(pad=8)
f.suptitle('Box Plots')
cols = df.select_dtypes(exclude='object').columns
x_axes = 0
y_axes = 0
for col in cols :
sns.boxplot(data=df, x=col, ax=axes[x_axes, y_axes])
if y_axes== 1:
y_axes= 0
x_axes+= 1
else:
y_axes+= 1
plt.savefig('Plots/box_plot.png')
plt.show()
year_group = df.groupby(by='year').sum()
year_group['Positive affect'].plot()
plt.savefig('Plots/Positive_affect_plot.png')
year_group['Negative affect'].plot()
plt.savefig('Plots/Negative_affect_plot.png')
ax1 = sns.barplot(x=year_group.index, y=year_group['Social support'].values)
ax1.tick_params(axis='x' , rotation=90)
plt.savefig('Plots/SocialSupport.png')
sns.set (rc={'figure.figsize': (15,10)})
sns.heatmap(df.corr(), annot= True)
plt.title('Correlation Matrix')
plt.savefig('Plots/Correlation_matrix.png')
sns.jointplot(data=df , x='year', y='Social support')
sns.jointplot(data=df , x='Life Ladder', y='Social support')
plt.savefig('Plots/jointplots.png')
import os
from os import listdir,mkdir
all_files = os.listdir('Plots')
reports = [f'Plots/{file}' for file in all_files]
!pip install FPDF
from fpdf import FPDF
WIDTH = 210
HEIGHT = 297
pdf = FPDF()
pdf.set_font('Arial', 'B', 56)
pdf.add_page()
pdf.cell (180,20, txt='REPORT' , align= 'C')
for report in reports:
pdf.add_page()
pdf.set_font('Arial', 'B', 24)
pdf.cell(190, 20, txt=report, align= 'C')
pdf.image(report, 5, 30, WIDTH-5)
pdf.output('Countries_report.pdf')