代码之家  ›  专栏  ›  技术社区  ›  Jondiedoop

在bokeh中显示wordlcoud的图像

  •  0
  • Jondiedoop  · 技术社区  · 8 年前

    假设我有一个字符串,我想把它变成一个单词cloud,比如:

    from wordcloud import WordCloud
    import matplotlib.pyplot as plt
    
    teststring = 'hi this is a test test test test test string for a word cloud'
    wordcloud = WordCloud(width=1000, height=1000, margin=0, background_color='white', 
                          collocations=False).generate(teststring)
    plt.imshow(wordcloud)
    

    我还做了一个有几个标签的bokeh标签对象

    from bokeh.plotting import figure, output_file, show
    from bokeh.models.widgets import Panel, Tabs
    
    output_file('stackoverflow.html')
    p1 = figure()
    p1.line(x=range(5), y=range(5))
    tab1 = Panel(child=p1, title="Line plot")
    tabs = Tabs(tabs=[tab1])
    show(tabs)
    

    如何将wordcloud的图形添加到tab2中以显示在面板中?

    我看了很多链接(比如),但都没用。我发现的关闭是在这里: How do I work with images in Bokeh (Python) ,但要让它起作用,我似乎必须中间保存wordcloud(实际上我有大约25个)。这真的是必要的吗,或者我可以立即在bokeh中显示wordcloud吗?

    提前谢谢!

    0 回复  |  直到 8 年前
        1
  •  0
  •   Joran Beasley    7 年前

    无耻的插头…我刚刚为bokeh写了一个wordcloud插件

    https://github.com/joranbeasley/bokeh_wordcloud2

    下面是一个将它与一大块文本一起使用的示例

    from bokeh_wordcloud2 import WordCloud2
    
    source = ColumnDataSource(data={'x':[my_text]})
    wordcloud = WordCloud2(source=source,wordCol='x',colors='blue')
    curdoc().add_root(wordcloud)