python - 如何将SVG字符串保存到python中的文件
问题描述
顾名思义,我正在使用这个 wordcloud 生成器,并希望将内容保存为.svg
. 该库有一个to_svg()
函数,它返回一个字符串。它还有一个to_file()
不以这种格式保存的。
有什么方法可以使用pyplot
's savefig 函数将字符串输出保存to_svg()
到文件中?
解决方案
它不使用pyplot.savefig
,但除了wordcloud
您链接的库和 Python 本身之外,它不需要任何东西:
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)
输出是您所追求的词 cloud .svg。
推荐阅读
- sql - 显示没有值的单位
- c# - 通过使较大的数字以最佳方式分布来对数据进行排序
- linux - 用于添加和删除用户的 bash 脚本
- javascript - 为什么我的函数只在循环的第一个元素中起作用
- java - 我在 Spring 变量定义中做错了什么?
- javascript - Javascript string.length 不等于 Python len()
- c# - C# 如何通过名称设置 ValueTuple 字段值
- javascript - 鼠标单击图像后,Slick Slider 加速
- google-apps-script - 在同一个 gdoc 中自动复制和粘贴
- c# - 将参数从视图传递到控制器的 ASP NET Core (MVC) 问题