-
Notifications
You must be signed in to change notification settings - Fork 9
/
plotly_wordcloud.py
53 lines (44 loc) · 1.65 KB
/
plotly_wordcloud.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
from wordcloud import WordCloud, STOPWORDS
import plotly.graph_objs as go
def plotly_wordcloud(text):
wc = WordCloud(stopwords = set(STOPWORDS),
max_words = 200,
max_font_size = 100)
wc.generate(text)
word_list=[]
freq_list=[]
fontsize_list=[]
position_list=[]
orientation_list=[]
color_list=[]
for (word, freq), fontsize, position, orientation, color in wc.layout_:
word_list.append(word)
freq_list.append(freq)
fontsize_list.append(fontsize)
position_list.append(position)
orientation_list.append(orientation)
color_list.append(color)
# get the positions
x=[]
y=[]
for i in position_list:
x.append(i[0])
y.append(i[1])
# get the relative occurence frequencies
new_freq_list = []
for i in freq_list:
new_freq_list.append(i*100)
new_freq_list
trace = go.Scatter(x=x,
y=y,
textfont = dict(size=new_freq_list,
color=color_list),
hoverinfo='text',
hovertext=['{0}{1}'.format(w, f) for w, f in zip(word_list, freq_list)],
mode='text',
text=word_list
)
layout = go.Layout({'xaxis': {'showgrid': False, 'showticklabels': False, 'zeroline': False},
'yaxis': {'showgrid': False, 'showticklabels': False, 'zeroline': False}})
fig = go.Figure(data=[trace], layout=layout)
return fig