Home:ALL Converter>How to pass a python wordcloud element to svgwrite method to generate a svg of the wordcloud?

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

Ask Time:2017-06-23T14:59:48         Author:Shashank Salaj

Json Formatter

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

Author:Shashank Salaj,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/44715044/how-to-pass-a-python-wordcloud-element-to-svgwrite-method-to-generate-a-svg-of-t
Roey :

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\nwordcloud = WordCloud()\nwordcloud.generate_from_frequencies(frequencies=features)\nwordcloud_svg = wordcloud.to_svg(embed_font=True)\nf = open("filename.svg","w+")\nf.write(wordcloud_svg )\nf.close()\n\nThe embed_font boolean prevented the words from overlapping.\nAlso you have a lot of freedom modifying wordcloud_svg to change colors, fonts , etc. It has an xml-like structure (print it :) )",
2020-10-01T14:10:22
delleae :

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.\n\nIn 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\n\nIn the imports section:\n\nimport matplotlib\nmatplotlib.use('SVG') #set the backend to SVG\nimport matplotlib.pyplot as plt\n\n\nAfter generating the WordCloud\n\nfname = \"cloud_test\"\nplt.imshow(wordcloud, interpolation=\"bilinear\") \nplt.axis(\"off\")\nfig = plt.gcf() #get current figure\nfig.set_size_inches(10,10) \nplt.savefig(fname, dpi=700)\n\n\nsavefig(filename) automatically saves it in SVG format, since that is what the backend is set to.",
2017-08-06T20:21:38
yy