从 matplotlib-venn 导出 SVG

Export SVG out of matplotlib-venn

我已经开始使用 matplotlib-venn 来绘制维恩图了。这是一个非常有用的工具,但我想知道生成的图形是否可以保存为 SVG(甚至 pdf)格式。我想保留图形向量,而不是像在 png 中那样对其进行栅格化。

我认为有办法,所以如果你能指出我的方法,那将非常有帮助。

看来您需要配置 SVG 'backend':

The matplotlib frontend or matplotlib API is the set of classes that do the heavy lifting, creating and managing figures, text, lines, plots and so on (Artist tutorial). This is an abstract interface that knows nothing about output. The backends are device-dependent drawing devices, aka renderers, that transform the frontend representation to hardcopy or a display device (What is a backend?). Example backends: PS creates PostScript® hardcopy, SVG creates Scalable Vector Graphics hardcopy,...

> # The default backend; one of GTK GTKAgg GTKCairo GTK3Agg GTK3Cairo
> # CocoaAgg MacOSX Qt4Agg Qt5Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG
> # Template.
> # You can also deploy your own backend outside of matplotlib by
> # referring to the module name (which must be in the PYTHONPATH) as
> # 'module://my_backend'. backend : qt4agg

来源:http://matplotlib.org/Matplotlib.pdf

您可以使用标准 savefig 方法。只需给您的输出路径一个“.svg”扩展名:

from matplotlib_venn import venn2
import matplotlib.pyplot as plt

venn2(subsets = (3, 2, 1))
plt.savefig('venn2.svg')

您可以使用 .png 扩展名保存为 PNG,并且...您可能会看到其他格式的情况。