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