【发布时间】:2021-11-14 16:53:49
【问题描述】:
我正在使用 python wordcloud 库、Wikipedia 和 Matplotlib 生成一些 wordcloud。我的意图是以矢量格式将 wordcloud 保存在 SVG 文件中。目前该程序正在以 SVG 格式保存文件,但内容为光栅格式。我的部分代码如下:
# Import packages
import wikipedia
import re
from wordcloud import WordCloud, STOPWORDS
import matplotlib
matplotlib.use('SVG') #set the backend to SVG
import matplotlib.pyplot as plt
# Specify the title of the Wikipedia page
wiki = wikipedia.page('Puzzle')
# Extract the plain text content of the page
text = wiki.content
# Clean text
text = re.sub(r'==.*?==+', '', text)
text = text.replace('\n', '')
# Define a function to plot word cloud
def plot_cloud(wordcloud):
fname = "cloud_test"
plt.axis("off")
fig = plt.gcf() #get current figure
fig.set_size_inches(10,10)
plt.savefig(fname + ".svg", dpi=700, format="svg")
plt.imshow(wordcloud, interpolation="bilinear")
# Generate word cloud
wordcloud = WordCloud(width = 3000, height = 2000, random_state=1, background_color='black', colormap='Pastel1', collocations=False, stopwords = STOPWORDS).generate(text)
# Plot
plot_cloud(wordcloud)
结果如下: https://www.dropbox.com/s/a2ux72dtq62atkd/cloud_test.svg?dl=1
【问题讨论】:
-
内容为光栅格式您介意详细说明吗?
-
内容表示当我用浏览器打开 svg 文件并查看源代码时,我看到“data:image/png;base64”表示实际嵌入在 svg 文件中的 png 文件。
-
我添加了结果svg,如果你有时间请看一下
-
请不要将图片上传到Dropbox,而是直接在问题上发布图片
-
我试过了,但是图片是 svg 格式的。此处不支持上传 svg
标签: python matplotlib svg word-cloud