This module provides a function for creating Matplotlib figures with ellipse-based Venn diagrams for up to five classes. Its primary aim is a simple interface and visually appealing results.
Features:
- Custom color mixing callbacks.
- Area-proportional option for
$n=2$ .
pip install yamvp
import numpy as np
import matplotlib.pyplot as plt
from yamvp import venn
# Fill a 4D 2x2x2x2 array with random values
rand4 = np.random.randint(0, 1000, size=(2, 2, 2, 2))
# Set the value at A∩B to 42
rand4[1,1,0,0] = 42
# Create the Venn-diagram
fig = venn(rand4, ["A", "B", "C", "D"])
# Save the figure
fig.savefig("rand4_demo.png", dpi=100, bbox_inches="tight")
plt.close(fig)venn([[None, "B"], ["A", "AB"]], ["Alpha", "Beta"], outfile = "venn2_demo.png")vals3 = [
[[None, "C"], ["B", "BC"]],
[["A", "AC"], ["AB", "ABC"]],
]
venn(vals3, ["Alpha", "Beta", "Gamma"], outfile = "venn3_demo.png")vals4 = np.empty((2, 2, 2, 2), dtype=object)
for i, yA in enumerate(("", "A")):
for j, yB in enumerate(("", "B")):
for k, yC in enumerate(("", "C")):
for l, yD in enumerate(("", "D")):
vals4[i, j, k, l] = yA + yB + yC + yD
vals4[0,0,0,0] = None
venn(vals4, ["Alpha", "Beta", "Gamma", "Delta"], outfile = "venn4_demo.png")vals5 = np.empty((2, 2, 2, 2, 2), dtype=object)
for i, yA in enumerate(("", "A")):
for j, yB in enumerate(("", "B")):
for k, yC in enumerate(("", "C")):
for l, yD in enumerate(("", "D")):
for m, yE in enumerate(("", "E")):
vals5[i, j, k, l, m] = yA + yB + yC + yD + yE
vals5[0,0,0,0,0] = None
venn(vals5, ["Alpha", "Beta", "Gamma", "Delta", "Epsilon"], outfile = "venn5_demo.png")Each intersection’s color is the mean of the corresponding class colors.
venn(vals4, ["Alpha", "Beta", "Gamma", "Delta"], color_mixing = "average", outfile="venn4_demo_colors_average_mixing.png")This is what would happen if we simply stacked the ellipses with opacity = 0.5. The result depends on the order in which the ellipses are drawn.
venn(vals4, ["Alpha", "Beta", "Gamma", "Delta"], color_mixing = "alpha", outfile = "venn4_demo_colors_alpha_mixing.png") def color_mix_multiply(colors):
arr = np.stack([np.array(c, float) for c in colors], axis=0)
return np.prod(arr, axis=0)
venn(vals4, ["Alpha", "Beta", "Gamma", "Delta"], color_mixing=color_mix_multiply, outfile="venn4_demo_colors_multiply_mixing.png", text_color="white") venn(vals3, ["Alpha", "Beta", "Gamma"], colors=["red", "green", "blue"], outfile = "venn3_demo_colors.png")The area_proportional flag is ignored if
For
rand2 = np.random.randint(0, 1000, size=(2, 2))
venn(rand2, ["Alpha", "Beta"], area_proportional=True, outfile = "rand2_demo.png")For
rand3 = np.array(np.random.randint(0, 1000, size=(2, 2, 2)), dtype=object)
rand3[0,0,0] = None
venn(rand3, ["Alpha", "Beta", "Gamma"], area_proportional=True, outfile = "rand3_demo.png")The optimizer is based on a monte-carlo greedy hill climing algorithm, it tries to find a solution with sufficient loss, and selects the best result on using a quality score.
This project is licensed under the MIT License (c) 2025 Bálint Csanády, aielte-research. See the LICENSE file for details.











