1

I'm using this wordcloud generator to do as the name suggests, and would like to save the contents as .svg. The library has a to_svg() function, which returns a string. It also has a to_file() which doesn't save in this format.

Is there any way to use pyplot's savefig function to save the string output from to_svg() to a file?

1
  • Why do you want to use pyplot.savefig for this? You could just with open('my.svg', 'w') as svg: svg.write(the_string)? Commented Nov 23, 2020 at 21:33

1 Answer 1

4

It doesn't use pyplot.savefig, but it needs nothing beyond the wordcloud library you linked and Python itself:

from wordcloud import WordCloud

wc = WordCloud()
wc.generate_from_text('This is a word cloud example which has a few words, showing them word for word in a cloud.')

svg_text = wc.to_svg()
with open('my.svg', 'w') as f:
    f.write(svg_text)

The output is the word cloud .svg you're after.

this is a screengrab from the .svg in InkScape

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry about the mouse cursor in frame :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.