本文整理汇总了Python中wordcloud.WordCloud.layout_方法的典型用法代码示例。如果您正苦于以下问题:Python WordCloud.layout_方法的具体用法?Python WordCloud.layout_怎么用?Python WordCloud.layout_使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wordcloud.WordCloud
的用法示例。
在下文中一共展示了WordCloud.layout_方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: voxel
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import layout_ [as 别名]
def voxel(v,name):
voxel_idx = int(v)
# Prepare variables
regparams = app.df.loc[voxel_idx]
# Generate a lookup by concept name
lookup = get_lookup()
# We are only interested in nonzero concepts
regparams = pandas.DataFrame(regparams[regparams!=0])
concepts = regparams.index.tolist()
colors = random_colors(concepts)
regparams["key"] = [lookup[x] for x in regparams.index]
regparams["color"] = [colors[x] for x in regparams.index]
regparams.columns = ['value', 'key', 'color']
# Generate a word cloud image, take regression params into account
scaled = (regparams['value'].abs()*1000).copy()
text = []
for k,v in scaled.iteritems():
multiply_by = int(v)
string = [regparams.loc[k]['key'].replace(" ","_")] * multiply_by
text = text + string
text = " ".join(text)
regparams = regparams.to_json(orient="records")
# Min and max values for the color scale
min_voxel = app.X.loc[:,voxel_idx].min()
max_voxel = app.X.loc[:,voxel_idx].max()
# We will let the user select a voxel location based on region
regions = app.regions.to_dict(orient="records")
wordcloud = WordCloud(max_font_size=100, width=app.width, height=app.height,
relative_scaling=1.0, background_color="white").generate(text)
# Remove "_" in words
words = []
for tup in wordcloud.words_:
words.append((tup[0].replace("_"," "),tup[1]))
wordcloud.words_ = words
layout = []
for tup in wordcloud.layout_:
newtup = ((tup[0][0].replace("_"," "),tup[0][1]),
tup[1],
tup[2],
tup[3],
tup[4])
layout.append(newtup)
wordcloud.layout_ = layout
plt.imshow(wordcloud)
plt.axis("off")
sio = cStringIO.StringIO()
plt.savefig(sio, format="png")
png_data = sio.getvalue().encode("base64").strip()
return render_template("cloud.html",regparams=regparams,
min=app.df.loc[voxel_idx].min(),
max=app.df.loc[voxel_idx].max(),
width=app.width,
min_voxel=min_voxel,
max_voxel=max_voxel,
height=app.height,
padding=app.padding,
radius=app.radius,
maxRadius=app.maxRadius,
lookup=lookup,
colors=colors,
png_data=png_data,
voxel=voxel_idx,
regions=regions,
region_name=name)