如何自定义 matplotlib-venn 图的边框和背景颜色?

How do I customise the border and background color of my matplotlib-venn plot?

我正在尝试使用 plt.figure 方法自定义 venn plot 的图形区域,但无法获得预期的结果。

我试图在图上创建黑色边框和白色背景,但图像仍然透明,没有边框。

我怀疑我的代码遗漏了一些基本的东西,但如果有任何指点,我们将不胜感激。这是我的代码。

from matplotlib import pyplot as plt
from matplotlib_venn import venn2, venn2_circles 

# Call 2 group Venn diagram
v = venn2(subsets = (10, 0, 5), set_labels = ('Euler', 'Venn'))
c = venn2_circles(subsets=(10,0,5), linestyle='dashed')

# Format
c[0].set_lw(2.0)
c[0].set_ls('dotted')
c[0].set_alpha(1)
c[0].set_color('#a6cee3')
c[1].set_lw(2.0)
c[1].set_ls('dotted')
c[1].set_alpha(1)
c[1].set_color('#b2df8a')

# Labels
plt.title("Diagrams")
for idx, subset in enumerate(v.subset_labels):
    v.subset_labels[idx].set_visible(False)

# Figure
plt.figure(linewidth=10, edgecolor="black", facecolor="white")
plt.show()

您需要在调用任何绘图函数之前调用 plt.figure()。所以,在调用 v = venn2(....

之前

plt.figure() 创建一个新的区域来绘制或绘制一些东西,并且可以处理很多选项。如果您不调用 plt.figure() 或某个等效函数,matplotlib 会创建一个默认值 figure。当您稍后调用 plt.figure() 时,matplotlib 会启动一个新的空 figure。通常,matplotlib 会显示两个 windows:第一个带有默认 figure 设置,第二个没有绘图。

完整的示例,稍微重写以使用循环,如下所示:

from matplotlib import pyplot as plt
from matplotlib_venn import venn2, venn2_circles


plt.figure(linewidth=10, edgecolor="black", facecolor="white")

# Call 2 group Venn diagram
v = venn2(subsets=(10, 0, 5), set_labels=('Euler', 'Venn'))
circles = venn2_circles(subsets=(10, 0, 5), linestyle='dashed')

# circle format
for circle, color in zip(circles, ['#a6cee3', '#b2df8a']):
    circle.set_lw(2.0)
    circle.set_ls('dotted')
    circle.set_alpha(1)
    circle.set_color(color)

# hide unwanted labels
for label in v.subset_labels:
    label.set_visible(False)

plt.title("Diagrams")
plt.show()