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). Below is the Python code to generate word_cloud:
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:
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
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.
In matplotlib's documentation there is discussion of changing the format the backend uses. When the backend is using SVG format, the plot can be saved as a .svg
In the imports section:
After generating the WordCloud
savefig(filename) automatically saves it in SVG format, since that is what the backend is set to.