当前位置: 首页>>代码示例>>Python>>正文


Python WordCloud.words_方法代码示例

本文整理汇总了Python中wordcloud.WordCloud.words_方法的典型用法代码示例。如果您正苦于以下问题:Python WordCloud.words_方法的具体用法?Python WordCloud.words_怎么用?Python WordCloud.words_使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在wordcloud.WordCloud的用法示例。


在下文中一共展示了WordCloud.words_方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: voxel

# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import words_ [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)
开发者ID:vsoch,项目名称:cogatcloud,代码行数:80,代码来源:index.py


注:本文中的wordcloud.WordCloud.words_方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。