- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下代码应该在 matplotlib 中绘制给定文本的词云并将其转换为 plotly:
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.tools as tls
# Thanks : https://www.kaggle.com/aashita/word-clouds-of-various-shapes ##
def plot_wordcloud(text, mask=None, max_words=200, max_font_size=100, figure_size=(24.0,16.0),
title = None, title_size=40, image_color=False):
stopwords = set(STOPWORDS)
wordcloud = WordCloud(background_color='black',
stopwords = stopwords,
max_words = max_words,
max_font_size = max_font_size,
random_state = 42,
width=800,
height=400,
mask = mask)
wordcloud.generate(str(text))
fig = plt.figure()
plt.imshow(wordcloud)
return tls.mpl_to_plotly(fig)
word_list = "Wikipedia was launched on January 15, 2001, by Jimmy Wales and Larry Sanger.[10] Sanger coined its name,[11][12] as a portmanteau of wiki[notes 3] and 'encyclopedia'. Initially an English-language encyclopedia, versions in other languages were quickly developed. With 5,748,461 articles,[notes 4] the English Wikipedia is the largest of the more than 290 Wikipedia encyclopedias. Overall, Wikipedia comprises more than 40 million articles in 301 different languages[14] and by February 2014 it had reached 18 billion page views and nearly 500 million unique visitors per month.[15] In 2005, Nature published a peer review comparing 42 science articles from Encyclopædia Britannica and Wikipedia and found that Wikipedia's level of accuracy approached that of Britannica.[16] Time magazine stated that the open-door policy of allowing anyone to edit had made Wikipedia the biggest and possibly the best encyclopedia in the world and it was testament to the vision of Jimmy Wales.[17] Wikipedia has been criticized for exhibiting systemic bias, for presenting a mixture of 'truths, half truths, and some falsehoods',[18] and for being subject to manipulation and spin in controversial topics.[19] In 2017, Facebook announced that it would help readers detect fake news by suitable links to Wikipedia articles. YouTube announced a similar plan in 2018."
plot_wordcloud(word_list, title="Word Cloud")
这只会返回一个空白图形,data
中没有任何内容。部分:
Figure({
'data': [],
'layout': {'autosize': False,
'height': 288,
'hovermode': 'closest',
'margin': {'b': 61, 'l': 54, 'pad': 0, 'r': 43, 't': 59},
'showlegend': False,
'width': 432,
'xaxis': {'anchor': 'y',
'domain': [0.0, 1.0],
'mirror': 'ticks',
'nticks': 10,
'range': [-0.5, 799.5],
'showgrid': False,
'showline': True,
'side': 'bottom',
'tickfont': {'size': 10.0},
'ticks': 'inside',
'type': 'linear',
'zeroline': False},
'yaxis': {'anchor': 'x',
'domain': [0.0, 1.0],
'mirror': 'ticks',
'nticks': 10,
'range': [399.5, -0.5],
'showgrid': False,
'showline': True,
'side': 'left',
'tickfont': {'size': 10.0},
'ticks': 'inside',
'type': 'linear',
'zeroline': False}}
})
这是为什么?我该如何解决?
如果我想绘制 matplotlib 图,它工作正常 - return fig
返回词云的静态图形。
我试图直接在 plotly 中绘制 wordcloud,但是使用 go.Scatter
您需要明确提供 x 和 y 值 - 它不能从 wordcloud
中获取它们隐含地喜欢 plt.imshow
能。所以,我得到一个“对象不可迭代”的错误:
def plot_wordcloud(text, mask=None, max_words=200, max_font_size=100, figure_size=(24.0,16.0),
title = None, title_size=40, image_color=False):
stopwords = set(STOPWORDS)
wordcloud = WordCloud(background_color='black',
stopwords = stopwords,
max_words = max_words,
max_font_size = max_font_size,
random_state = 42,
width=800,
height=400,
mask = mask)
wordcloud.generate(str(text))
data = go.Scatter(dict(wordcloud.generate(str(text))),
mode='text',
text=words,
marker={'opacity': 0.3},
textfont={'size': weights,
'color': colors})
layout = go.Layout({'xaxis': {'showgrid': False, 'showticklabels': False, 'zeroline': False},
'yaxis': {'showgrid': False, 'showticklabels': False, 'zeroline': False}})
fig = go.Figure(data=[data], layout=layout)
return fig
word_list = "Wikipedia was launched on January 15, 2001, by Jimmy Wales and Larry Sanger.[10] Sanger coined its name,[11][12] as a portmanteau of wiki[notes 3] and 'encyclopedia'. Initially an English-language encyclopedia, versions in other languages were quickly developed. With 5,748,461 articles,[notes 4] the English Wikipedia is the largest of the more than 290 Wikipedia encyclopedias. Overall, Wikipedia comprises more than 40 million articles in 301 different languages[14] and by February 2014 it had reached 18 billion page views and nearly 500 million unique visitors per month.[15] In 2005, Nature published a peer review comparing 42 science articles from Encyclopædia Britannica and Wikipedia and found that Wikipedia's level of accuracy approached that of Britannica.[16] Time magazine stated that the open-door policy of allowing anyone to edit had made Wikipedia the biggest and possibly the best encyclopedia in the world and it was testament to the vision of Jimmy Wales.[17] Wikipedia has been criticized for exhibiting systemic bias, for presenting a mixture of 'truths, half truths, and some falsehoods',[18] and for being subject to manipulation and spin in controversial topics.[19] In 2017, Facebook announced that it would help readers detect fake news by suitable links to Wikipedia articles. YouTube announced a similar plan in 2018."
plot_wordcloud(word_list, title="Word Cloud")
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-50-0567281b72b3> in <module>()
---> 48 plot_wordcloud(word_list, title="Word Cloud")
<ipython-input-50-0567281b72b3> in plot_wordcloud(text, mask, max_words, max_font_size, figure_size, title, title_size, image_color)
18
19
---> 20 data = go.Scatter(dict(wordcloud.generate(str(text))),
21 mode='text',
22 text=words,
TypeError: 'WordCloud' object is not iterable
如果我return wordcloud
,它显示:<wordcloud.wordcloud.WordCloud at 0x1c8faeda748>
.如果有人知道如何解压 wordcloud
对象,以便我可以将其中的 x 和 y 参数输入到 go.Figure
中,那也很棒(事实上更好)。
只是为了表明解压 wordcloud
object 会工作,我可以通过在 go.Scatter
中为 x 和 y 值放置随机数来使用 plotly 本地绘制 wordcloud像这样:
import random
import plotly.graph_objs as go
def plot_wordcloud(text, mask=None, max_words=200, max_font_size=100, figure_size=(24.0,16.0),
title = None, title_size=40, image_color=False):
stopwords = set(STOPWORDS)
wordcloud = WordCloud(background_color='black',
stopwords = stopwords,
max_words = max_words,
max_font_size = max_font_size,
random_state = 42,
width=800,
height=400,
mask = mask)
wordcloud.generate(str(text))
data = go.Scatter(x=[random.random() for i in range(3000)],
y=[random.random() for i in range(3000)],
mode='text',
text=str(word_list).split(),
marker={'opacity': 0.3},
textfont={'size': weights,
'color': colors})
layout = go.Layout({'xaxis': {'showgrid': False, 'showticklabels': False, 'zeroline': False},
'yaxis': {'showgrid': False, 'showticklabels': False, 'zeroline': False}})
fig = go.Figure(data=[data], layout=layout)
return fig
它只是不正确的词云(显然,正确定义了单词的位置和大小),它应该看起来像这样(用 matplotlib.pyplot
绘制的静态词云):
最佳答案
由于 wordcloud
生成图像,而 plotly 的转换函数当前无法处理图像,因此您需要以某种方式从 wordcloud.wordcloud.WordCloud 的位置、大小和方向重新生成 wordcloud
对象。
这些信息存储在 .layout_
属性中
wc = Wordcloud(...)
wc.generate(text)
print(wc.layout_)
打印形式为
的元组列表[(word, freq), fontsize, position, orientation, color]
例如在这种情况下
[(('Wikipedia', 1.0), 100, (8, 7), None, 'rgb(56, 89, 140)'),
(('articles', 0.4444444444444444), 72, (269, 310), None, 'rgb(58, 186, 118)'), ...]
所以原则上这允许将词云重新生成为文本。但是必须注意小细节。 IE。字体和字体大小需要相同。
这是一个纯 matplotlib 示例,它使用 matplotlib.text.Text
对象再现了词云。
import numpy as np
from wordcloud import WordCloud, STOPWORDS
from wordcloud.wordcloud import FONT_PATH
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
word_list = "Wikipedia was launched on January 15, 2001, by Jimmy Wales and Larry Sanger.[10] Sanger coined its name,[11][12] as a portmanteau of wiki[notes 3] and 'encyclopedia'. Initially an English-language encyclopedia, versions in other languages were quickly developed. With 5,748,461 articles,[notes 4] the English Wikipedia is the largest of the more than 290 Wikipedia encyclopedias. Overall, Wikipedia comprises more than 40 million articles in 301 different languages[14] and by February 2014 it had reached 18 billion page views and nearly 500 million unique visitors per month.[15] In 2005, Nature published a peer review comparing 42 science articles from Encyclopædia Britannica and Wikipedia and found that Wikipedia's level of accuracy approached that of Britannica.[16] Time magazine stated that the open-door policy of allowing anyone to edit had made Wikipedia the biggest and possibly the best encyclopedia in the world and it was testament to the vision of Jimmy Wales.[17] Wikipedia has been criticized for exhibiting systemic bias, for presenting a mixture of 'truths, half truths, and some falsehoods',[18] and for being subject to manipulation and spin in controversial topics.[19] In 2017, Facebook announced that it would help readers detect fake news by suitable links to Wikipedia articles. YouTube announced a similar plan in 2018."
def get_wordcloud(width, height):
wc = WordCloud(background_color='black',
stopwords = set(STOPWORDS),
max_words = 200,
max_font_size = 100,
random_state = 42,
width=int(width),
height=int(height),
mask = None)
wc.generate(word_list)
return wc
fig, (ax, ax2) = plt.subplots(nrows=2, sharex=True, sharey=True)
fp=FontProperties(fname=FONT_PATH)
bbox = ax.get_position().transformed(fig.transFigure)
wc = get_wordcloud(bbox.width, bbox.height)
ax.imshow(wc)
ax2.set_facecolor("black")
for (word, freq), fontsize, position, orientation, color in wc.layout_:
color = np.array(color[4:-1].split(", ")).astype(float)/255.
x,y = position
rot = {None : 0, 2: 90}[orientation]
fp.set_size(fontsize*72./fig.dpi)
ax2.text(y,x, word, va="top", ha="left", color=color, rotation=rot,
fontproperties=fp)
print(wc.layout_)
plt.show()
上图是通过imshow
显示的词云图,下图是重新生成的词云图。
现在你可能想在 plotly 而不是 matplotlib 中做同样的事情,但我对 plotly 不够熟练,无法在这里直接给出解决方案。
关于python - 密谋返回空白图形对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53221651/
我正在寻找 css 属性以隐藏带或不带 css 类的段落,如果它包含空格 ( ) 或空白,但我想至少保留一个带或不带的段落,如果有更多的话。 隐藏段落,如果它是空白的或包含 white-space(
在 ruby 中对空白有不同的敏感度/设置吗? 我有一个 RoR 项目,其中一个事件记录调用有很多组件: max_stuff = FooSummary.select("max(stuff)
如何在脚注中的数字后留空? 一般来说,对于所有脚注! 例子: 好 : 1 Hello World 坏:1Hello World 最佳答案 正确答案是不要重新定义\thefootnote ,因为这会在脚
我有这段代码,每次第一个 for 循环再次开始时,我希望它将数组重置为空白,因为它正在使用新用户,但我得到的输出包含一个数组中的所有值。 var items = []; for (var i
我试图在CakePHP中生成一个动态xml文档,以输出到浏览器。 这是我的 Controller 代码: Configure::write ('debug', 0); $this->layout =
当我尝试在 nxos 设备上运行某些命令时,输出末尾有一个空格。我必须将输出与现有变量列表进行比较。末尾的空格导致比较错误。如何在字符串列表中使用 .strip() 函数? - name: Curre
我对 Elasticsearch 相当陌生,我一直在尝试对我的数据进行搜索,并且总是让点击部分为空。即使在数据上传和索引之后也会发生这种情况。我的映射如下: { "mappings":{
我想将about:blank页面更改为firefox插件首页页面的url。 如何更改默认的新标签页网址或可以为新标签页提供默认网址? 我正在使用Firefox附加SDK。 最佳答案 您可以结合使用Ta
我正在使用 R 并具有以下数据框示例,其中所有变量都是因子: first second third social birth control high
如何清空显示对话框的页面。下面是我的代码HTML: .ui-dialog, .ui-dialog-content { border:1px solid #cde68c; border-botto
更新“他的问题是要求我只运行一次 str ,他们已经告诉我该函数只需要一个参数)” 我试图返回第一个不重复的字符,例如:“blazqnqbla”->第一个不重复的字符是“z”,因此函数需要返回z。现在
我的登录验证有问题。问题是当我尝试使用管理员登录时,页面停止在 checklogin.php 上并且不会告诉它是否成功。这是我的代码。 索引.html Aplik
我的查询是这样的 SELECT Distinct tm.teamid,tm.Team_Name,CONCAT_WS(' ',tu.FirstName+' '+tu.LastName) as Leade
我正在创建指向页面的超链接 url 由用户输入决定,因此由查询字符串决定 ; 问题是变量状态由两个或多个单词组成。因此,当我尝试单击证明表单中输入的超链接时,仅获取状态变量的第一个单词。浏览器将另一个
该问题在每个浏览器中的表现都不同,例如在 Firefox 中大约一个空格如果您再次滚动到顶部,则会出现具有相同高度的滚动框。在 chrome 中,滚动时框会变得狭窄等等...... 使用的调用是:
我对菜单栏文字之间的 CSS 空白有疑问。我尝试了很多方法,但仍然无法解决。有人可以帮我吗? 菜单问题图片如下: http://imageshack.us/photo/my-images/201/44
我对 有疑问.其中的插入符根据是否为空具有不同的垂直位置: 我的代码: textarea { padding: 0 5px; border: none; outline: n
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Ignore whitespace in HTML 我想在网页上将图片并排放置。这是我的 HTML:
每当我尝试检查元素时,什么都没有出现。我在使用 Chrome。我明白了 Elements | Network | Sources | Timeline | Profiles | Resources |
我在使用 Chrome、Firefox 和 IE 时遇到了一个奇怪的问题。我正在为我的投资组合网站/博客构建一个 WordPress 主题,一切都很好,直到今天,当我在 chrome 中查看该网站时,
我是一名优秀的程序员,十分优秀!