1
我想生成一个由一些硬编码字符串组成的word_cloud的svg(截至目前,以后这些字符串将被动态生成)。 下面是Python代码生成word_cloud:如何将python wordcloud元素传递给svgwrite方法以生成wordcloud的svg?
from os import path
from wordcloud import WordCloud
d = path.dirname(__file__)
# Read the whole text.
#text = open(path.join(d, 'test.txt')).read()
mytext = ['hello, hi, ibm, pune, hola']
# Generate a word cloud image
wordcloud = WordCloud().generate(text)
import svgwrite
# Display the generated image:
# the matplotlib way:
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
现在代替使用plt.show(),我想通过wordcloud变量svgwrite如下方法:
svg_document = svgwrite.Drawing(filename = "test-svgwrite.svg",profile = 'full')
svg_document.add(svg_document.text(wordcloud,
insert = (210, 110)))
svg_document.tostring()
svg_document.save()
然而,这创建SVG不包含任何wordcloud,只有文本(如下图所示): check the screenshot here