-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetVisualizations.py
218 lines (191 loc) · 7.84 KB
/
getVisualizations.py
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
import pandas as pd
import plotly.graph_objects as go
import chart_studio
import chart_studio.plotly as ch_py
import chart_studio.tools as tls
input_file = './data/scored_df.csv'
## NOTE: anyone using this will need to make their own text file:
# 'chart_studio_info.txt' saved in the highest directory
# username goes on line 1, API key goes on line 2
user_info_file = 'chart_studio_info.txt'
scored_df = pd.read_csv(input_file, encoding='utf-8')
## the following is a neat workaround for wrapping text in the hovertemplate!
scored_df.text = scored_df.text.str.wrap(30)
scored_df.text = scored_df.text.apply(lambda x: x.replace('\n', '<br>'))
def upload_to_chart_studio(fig, filename):
# push to chart_studio account
# regenerate api key if necessary
# private information stored locally:
f = open(user_info_file, 'r')
userinfo = f.read().split()
f.close()
username, api_key = userinfo
chart_studio.tools.set_credentials_file(username=username, api_key=api_key)
ch_py.plot(fig, filename = filename, auto_open = True)
def create_scatter(score_type, figname, x_title, title, color = 'blue'):
fig = go.Figure(data = go.Scatter(
x = scored_df[score_type],
y = scored_df.shares,
text = scored_df.text,
hovertemplate = 'Facebook Post:<br>%{text}<br>' + 'Shares: %{y:.0f}<br>' + 'Score: %{x:.1f}' + '<extra></extra>',
hoverinfo = 'text',
mode = 'markers',
marker = dict(color = color)
))
fig.update_layout(
title = title,
title_x = 0.5,
xaxis_title = x_title,
yaxis_title = "Number of Shares",
autosize = True
)
fig.show()
# upload_to_chart_studio(fig, figname)
def create_multi_scatter(figname, title):
fig = go.Figure()
fig.add_trace(go.Scatter(
x = scored_df.score,
y = scored_df.shares,
name = 'Sentiment Score',
text = scored_df.text,
hovertemplate = 'Facebook Post:<br>%{text}<br>' + 'Shares: %{y:.0f}<br>' + 'Sentiment Score: %{x:.1f}' + '<extra></extra>',
hoverinfo = 'text',
mode = 'markers',
opacity = 0.2,
marker = dict(color = 'blue', size = 8)
)
)
fig.add_trace(go.Scatter(
x = scored_df.overall_score,
y = scored_df.shares,
name = 'Overall Score',
text = scored_df.text,
hovertemplate = 'Facebook Post:<br>%{text}<br>' + 'Shares: %{y:.0f}<br>' + 'Overall Score: %{x:.1f}' + '<extra></extra>',
hoverinfo = 'text',
mode = 'markers',
opacity = 0.5,
marker = dict(color = 'green', size = 6)
)
)
fig.update_layout(
title = title,
title_x = 0.5,
xaxis_title = 'Score',
yaxis_title = "Number of Shares",
autosize = True
)
# fig.show()
upload_to_chart_studio(fig, figname)
# Box plot of shares separated by positive, neutral, negative score
def create_boxplot(score_type, lower_score, upper_score, title, figname):
layout = go.Layout(yaxis=dict(range=[0, 6000]))
fig = go.Figure(layout = layout)
fig.add_trace(go.Box(y = scored_df.shares[scored_df[score_type] < lower_score],
name = 'Scores < ' + str(lower_score) + '<br>n = ' + str(scored_df.shares[scored_df[score_type] < lower_score].count())))
fig.add_trace(go.Box(y = scored_df.shares[scored_df[score_type].between(lower_score, upper_score)],
name = str(lower_score) + ' ≤ Scores ≤ ' + str(upper_score) + '<br>n = '
+ str(scored_df.shares[scored_df[score_type].between(lower_score, upper_score)].count())))
fig.add_trace(go.Box(y = scored_df.shares[scored_df[score_type] > upper_score],
name = 'Scores > ' + str(upper_score) + '<br>n = ' + str(scored_df.shares[scored_df[score_type] > upper_score].count())))
fig.update_layout(
title = title,
title_x = 0.5,
yaxis_title = 'Number of Shares',
showlegend = False
)
fig.show()
# upload_to_chart_studio(fig, figname)
def create_multi_boxplot(lower_score, upper_score, title, figname):
# concatenate sentiment score and overall score box plots
fig = go.Figure()
fig.add_trace(go.Box(
y = scored_df.shares[scored_df.score < lower_score].tolist()
+ scored_df.shares[scored_df.overall_score < lower_score].tolist(),
x = ['Sentiment Score'] * len(scored_df.shares[scored_df.score < lower_score])
+ ['Overall Score'] * len(scored_df.shares[scored_df.overall_score < lower_score]),
name = 'Score < ' + str(lower_score),
marker = dict(color = 'blue')
)
)
fig.add_trace(go.Box(
y = scored_df.shares[scored_df.score.between(lower_score, upper_score)].tolist()
+ scored_df.shares[scored_df.overall_score.between(lower_score, upper_score)].tolist(),
x = ['Sentiment Score'] * len(scored_df.shares[scored_df.score.between(lower_score, upper_score)])
+ ['Overall Score'] * len(scored_df.shares[scored_df.overall_score.between(lower_score, upper_score)]),
name = str(lower_score) + ' ≤ Score ≤ ' + str(upper_score),
marker = dict(color = 'green')
)
)
fig.add_trace(go.Box(
y = scored_df.shares[scored_df.score > upper_score].tolist()
+ scored_df.shares[scored_df.overall_score > upper_score].tolist(),
x = ['Sentiment Score'] * len(scored_df.shares[scored_df.score > upper_score])
+ ['Overall Score'] * len(scored_df.shares[scored_df.overall_score > upper_score]),
name = 'Score > ' + str(upper_score),
marker = dict(color = 'purple')
)
)
fig.update_layout(
title = title,
title_x = 0.5,
yaxis=dict(range=[0, 6000]),
yaxis_title = 'Number of Shares',
showlegend = True,
boxmode = 'group'
)
fig.show()
# upload_to_chart_studio(fig, figname)
def create_violinplot(score_type, lower_score, upper_score, title, figname):
layout = go.Layout(yaxis=dict(range=[0, 6000]))
fig = go.Figure(layout = layout)
fig.add_trace(go.Violin(y = scored_df.shares[scored_df[score_type] < lower_score],
name = 'Scores < ' + str(lower_score) + '<br>n = ' + str(scored_df.shares[scored_df[score_type] < lower_score].count())))
fig.add_trace(go.Violin(y = scored_df.shares[scored_df[score_type].between(lower_score, upper_score)],
name = str(lower_score) + ' ≤ Scores ≤ ' + str(upper_score) + '<br>n = '
+ str(scored_df.shares[scored_df[score_type].between(lower_score, upper_score)].count())))
fig.add_trace(go.Violin(y = scored_df.shares[scored_df[score_type] > upper_score],
name = 'Scores >' + str(upper_score) + '<br>n = ' + str(scored_df.shares[scored_df[score_type] > upper_score].count())))
fig.update_layout(
title = title,
title_x = 0.5,
yaxis_title = 'Number of Shares',
showlegend = True
)
fig.show()
# upload_to_chart_studio(fig, figname)
# other visualizations to consider:
# number of words in each post
# scored_df.text.apply(lambda x: len(x.split()))
# create_scatter(score_type = 'score',
# figname = 'shares_vs_sentiment',
# x_title = 'Sentiment Score',
# title = "Number of Shares vs. Sentiment Score for National Geographic Facebook Posts"
#)
# create_scatter(score_type = 'overall_score',
# figname = 'shares_vs_overall_score',
# x_title = 'Overall Score',
# title = "Number of Shares vs. Overall Score for National Geographic Facebook Posts",
# color = 'green'
#)
# create_boxplot(score_type = 'score', lower_score = -1, upper_score = 1,
# title = 'Number of Shares vs. Sentiment Score for National Geographic Facebook Posts',
# figname = 'sentiment_score_boxplot'
#)
# create_boxplot(score_type = 'overall_score', lower_score = -1, upper_score = 1,
# title = 'Number of Shares vs. Overall Score for National Geographic Facebook Posts',
# figname = 'overall_score_boxplot'
#)
# create_violinplot(score_type = 'overall_score', lower_score = -1, upper_score = 2,
# title = 'Number of Shares vs. Overall Score for National Geographic Facebook Posts',
# figname = 'overall_score_violinplot')
print("Creating multi-scatterplot of shares vs. sentiment score / overall score...")
print("Creating multi-boxplot of shares vs. sentiment score / overall score...")
create_multi_scatter(
title = 'Number of Shares vs. Scores for National Geographic Facebook Posts',
figname = 'shares_vs_overall_score'
)
create_multi_boxplot(
lower_score = -1, upper_score = 1,
title = 'Number of Shares vs. Scores for National Geographic Facebook Posts',
figname = 'all_scores_boxplot'
)