简体   繁体   English

如何将python wordcloud元素传递给svgwrite方法以生成wordcloud的svg?

[英]How to pass a python wordcloud element to svgwrite method to generate a svg of the wordcloud?

I am trying to generate a svg of the word_cloud being formed out of some hardcoded strings(as of now, later these strings will be dynamically generated).我正在尝试生成由一些硬编码字符串形成的 word_cloud svg(截至目前,稍后将动态生成这些字符串)。 Below is the Python code to generate word_cloud:下面是生成 word_cloud 的 Python 代码:

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")

Now instead of using plt.show(), I want to pass the wordcloud variable to svgwrite method as below:现在,我想将 wordcloud 变量传递给 svgwrite 方法,而不是使用 plt.show(),如下所示:

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()

However, this created SVG doesn't contain any wordcloud, only the text (shown in below screenshot): check the screenshot here但是,这个创建的 SVG 不包含任何 wordcloud,只有文本(如下图所示):在此处查看屏幕截图

Facing some issues using matplotlib (which will use raster graphics in combination with wordcloud although it will be saved as ".svg"), i have figured out another way面对使用 matplotlib 的一些问题(它将使用光栅图形与 wordcloud 结合使用,尽管它会被保存为“.svg”),我想出了另一种方法

wordcloud = WordCloud()
wordcloud.generate_from_frequencies(frequencies=features)
wordcloud_svg = wordcloud.to_svg(embed_font=True)
f = open("filename.svg","w+")
f.write(wordcloud_svg )
f.close()

The embed_font boolean prevented the words from overlapping. embed_font 布尔值防止单词重叠。 Also you have a lot of freedom modifying wordcloud_svg to change colors, fonts , etc. It has an xml-like structure (print it :) )您也可以自由修改 wordcloud_svg 以更改颜色、字体等。它具有类似 xml 的结构(打印 :))

I found this while looking to do the same exact thing.我在做同样的事情时发现了这一点。 I got the same results from svgwrite and wound up using matplotlib's features instead.我从 svgwrite 得到了相同的结果,并最终使用了 matplotlib 的特性。

In matplotlib's documentation there is discussion of changing the format the backend uses.在 matplotlib 的文档中,讨论了更改后端使用的格式。 When the backend is using SVG format, the plot can be saved as a .svg当后端使用 SVG 格式时,绘图可以保存为 .svg

In the imports section:在进口部分:

import matplotlib
matplotlib.use('SVG') #set the backend to SVG
import matplotlib.pyplot as plt

After generating the WordCloud生成 WordCloud 后

fname = "cloud_test"
plt.imshow(wordcloud, interpolation="bilinear") 
plt.axis("off")
fig = plt.gcf() #get current figure
fig.set_size_inches(10,10)  
plt.savefig(fname, dpi=700)

savefig(filename) automatically saves it in SVG format, since that is what the backend is set to. savefig(filename) 自动将其保存为 SVG 格式,因为这是后端设置的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM