Я пытался построить облако анимированных слов, используя плотно. Когда выводим сюжет (html), я вижу анимацию всего облака слов (один за другим, используя игровую кнопку). Проблема с ползунком, который вообще не двигается.
Входные данные - это 2 списка из 4 элементов: первый список состоит из 4 различных текстов википедии, хранящихся в my_test = [text1, text2, text3, text4]
, а второй список - year = ["1952", "1962", "1967", "1972"]
.
Код:
from wordcloud import WordCloud, STOPWORDS
import plotly.graph_objs as go
from plotly.offline import plot
import plotly.offline as py
py.init_notebook_mode()
def plotly_wordcloud(text):
wc = WordCloud(width = 500,
height = 300,
margin=0,
stopwords = set(STOPWORDS),
max_words = 2000,
max_font_size = 100,
min_font_size = 10)
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
return x,y,word_list,freq_list,fontsize_list,color_list,new_freq_list
my_text = [text1,text2,text3,text4]
years = ["1952", "1962", "1967", "1972"]
fig_dict = {
"data": [],
"layout": {},
"layout.update": {},
"frames": []
}
sliders_dict = {
"active": 0,
"yanchor": "top",
"xanchor": "left",
"currentvalue": {
"font": {"size": 20},
"prefix": "Year:",
"visible": True,
"xanchor": "right"
},
"transition": {"duration": 300, "easing": "cubic-in-out"},
"pad": {"b": 10, "t": 50},
"len": 0.9,
"x": 0.1,
"y": 0,
"steps": []
}
fig_dict["layout"] = {"width":1000, "height":600}
fig_dict["layout"]["xaxis"] = {"showgrid":False, "showticklabels":False, "zeroline":False}
fig_dict["layout"]["yaxis"] = {"showgrid":False, "showticklabels":False, "zeroline":False}
fig_dict["layout"]["hovermode"] = "closest"
fig_dict["layout"]["title"] = "Issues by Month"
fig_dict["layout"]["sliders"] = {
"args": ["transition", {"duration": 300,
"easing": "cubic-in-out"}],
"initialValue": "1952",
"plotlycommand": "animate",
"values": years,
"visible": True
}
fig_dict["layout"]["updatemenus"] = [
{"buttons": [
{"args": [None, {"frame": {"duration": 500, "redraw": False},
"fromcurrent": True, "transition": {"duration": 300,
"easing": "quadratic-in-out"}}],
"label": "Play",
"method": "animate"
},
{
"args": [[None], {"frame": {"duration": 0, "redraw": False},
"mode": "immediate",
"transition": {"duration": 0}}],
"label": "Pause",
"method": "animate"
}
],
"direction": "left",
"pad": {"r": 10, "t": 87},
"showactive": False,
"type": "buttons",
"x": 0.1,
"xanchor": "right",
"y": 0,
"yanchor": "top"
}]
first_time = True
for texto,year in zip(my_text,years):
x,y,word_list,freq_list,fontsize_list,color_list,new_freq_list = plotly_wordcloud(str(texto))
if first_time == True:
first_time = False
data = go.Scatter(x=x,
y=x,
textfont = dict(size=new_freq_list,
color=color_list),
name=year,
mode='text',
text=word_list
)
fig_dict["data"].append(data)
fig_dict["frames"].append(go.Frame(data=[go.Scatter(
x=x,
y=y,
textfont = dict(size=new_freq_list,
color=color_list),
name=year,
mode='text',
text=word_list
)]))
slider_step = {"args": [[year],
{"frame": {"duration": 300, "redraw": False},
"mode": "immediate",
"transition": {"duration": 300}}
],
"label": year,
"method": "animate"}
sliders_dict["steps"].append(slider_step)
fig_dict["layout"]["sliders"] = [sliders_dict]
fig = go.Figure(fig_dict)
plot(fig)