【发布时间】:2017-08-19 23:38:57
【问题描述】:
我正在尝试从 csv 文件创建 wordcloud。以csv文件为例,其结构如下:
a,1
b,2
c,4
j,20
它有更多的行,或多或少 1800。第一列有字符串值(名称),第二列有它们各自的频率(int)。然后,读取文件并将键值行存储在字典 (d) 中,因为稍后我们将使用它来绘制 wordcloud:
reader = csv.reader(open('namesDFtoCSV', 'r',newline='\n'))
d = {}
for k,v in reader:
d[k] = v
一旦我们有了充满值的字典,我就会尝试绘制 wordcloud:
#Generating wordcloud. Relative scaling value is to adjust the importance of a frequency word.
#See documentation: https://github.com/amueller/word_cloud/blob/master/wordcloud/wordcloud.py
wordcloud = WordCloud(width=900,height=500, max_words=1628,relative_scaling=1,normalize_plurals=False).generate_from_frequencies(d)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
But an error is thrown:
Traceback (most recent call last):
File ".........../script.py", line 19, in <module>
wordcloud = WordCloud(width=900,height=500, max_words=1628,relative_scaling=1,normalize_plurals=False).generate_from_frequencies(d)
File "/usr/local/lib/python3.5/dist-packages/wordcloud/wordcloud.py", line 360, in generate_from_frequencies
for word, freq in frequencies]
File "/usr/local/lib/python3.5/dist-packages/wordcloud/wordcloud.py", line 360, in <listcomp>
for word, freq in frequencies]
TypeError: unsupported operand type(s) for /: 'str' and 'float
最后,文档说:
def generate_from_frequencies(self, frequencies, max_font_size=None):
"""Create a word_cloud from words and frequencies.
Parameters
----------
frequencies : dict from string to float
A contains words and associated frequency.
max_font_size : int
Use this font-size instead of self.max_font_size
Returns
-------
self
```python
So, I don't understand why is trowing me this error if I met the requirements of the function. I hope someone can help me, thanks.
**Note**
I work with worldcloud 1.3.1
【问题讨论】:
标签: python python-3.x csv word-cloud