-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgraph.py
83 lines (68 loc) · 2.01 KB
/
graph.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
import plotly.plotly as pltly
from plotly.graph_objs import *
import math
import pandas as pd
import csv
data = pd.read_csv('loglikelihoods.csv')
data = data.drop(data.index[:1])
data = data.drop(data.index[-1:])
positivedata = data[:11]
negativedata = data[-11:]
positivedata.sort(columns=['loglikely'], ascending=False, inplace = True )
negativedata.sort(columns=['loglikely'], ascending=False, inplace = True )
positive_words = positivedata['token'].values.tolist()
negative_words = negativedata['token'].values.tolist()
positive_values = positivedata['loglikely'].values.tolist()
negative_values = negativedata['loglikely'].values.tolist()
positive_words = [w.replace('u0001f602', '😂') for w in positive_words]
num_words = int((len(positive_values)+len(negative_values))/2.0)
max_neg = float(min(negative_values))
max_pos = float(max(positive_values))
def pickColor(value, what='COLOR'):
alphaChannel = math.fabs(value / max_pos if value > 0 else value / max_neg)
if value > 0:
hashcolor = 'rgba(68, 122, 219,%f)'%alphaChannel
else:
hashcolor = 'rgba(219, 90, 68,%f)'%alphaChannel
return hashcolor
# print zip(positive_words, positive_values)
###### ------- PLOT 1 ------- ######
positive = Bar(
y = positive_words,
x = positive_values,
name='Pro Feminist',
marker = Marker(
color = [ pickColor(i) for i in positive_values ]
),
orientation = 'h',
yaxis='y'
)
negative = Bar(
y = negative_words,
x = negative_values,
name='Anti Feminist',
marker=Marker(
color = [ pickColor(i) for i in negative_values]
),
orientation = 'h',
xaxis = 'x',
yaxis = 'y1'
)
data = Data( [ negative, positive ] )
layout = Layout(
title='Most Characteristic Words in Pro and Anti Feminist Tweets',
showlegend=False,
xaxis = XAxis(title='Log-Likelihood'),
yaxis = YAxis(
autorange='reversed',
position=0.5,
anchor='x',
),
yaxis1 = YAxis(
autorange='reversed',
position=0.4,
anchor='x'
)
)
fig = Figure(data=data, layout=layout)
plot_url = pltly.plot(fig, filename='bdw-feminism')