How to Create and Customize Venn Diagrams in Python?

Venn diagrams are visual representations used to show relationships between different sets of data. Python's matplotlib-venn library provides powerful tools to create and customize these diagrams with ease.

The matplotlib-venn library extends matplotlib's functionality specifically for creating Venn diagrams. It supports both 2-set and 3-set diagrams with extensive customization options including colors, labels, and styling.

Installation

First, install the required library ?

pip install matplotlib-venn

Basic Two-Set Venn Diagram

Here's how to create a simple intersection of two sets ?

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

# Create two sets
set1 = set(['A', 'B', 'C', 'D'])
set2 = set(['B', 'C', 'D', 'E', 'F'])

# Create Venn diagram
venn2([set1, set2], ('Set 1', 'Set 2'))
venn2_circles([set1, set2], linewidth=1)

plt.title("Basic Two-Set Venn Diagram")
plt.show()

Three Disjoint Sets

This example shows three sets with no overlapping elements ?

import matplotlib.pyplot as plt
from matplotlib_venn import venn3

# Create three disjoint sets
fruits_red = set(['apple', 'cherry', 'strawberry'])
fruits_yellow = set(['banana', 'lemon', 'pineapple'])
fruits_blue = set(['blueberry', 'grape', 'plum'])

# Create three-set Venn diagram
venn3([fruits_red, fruits_yellow, fruits_blue], ('Red Fruits', 'Yellow Fruits', 'Blue Fruits'))

plt.title("Three Disjoint Sets")
plt.show()

Customized Three-Set Venn Diagram

Advanced customization with colors, transparency, and styling ?

import matplotlib.pyplot as plt
from matplotlib_venn import venn3, venn3_circles

# Create diagram with subset counts
venn3(subsets=(20, 10, 12, 10, 9, 4, 3), 
      set_labels=('Set A', 'Set B', 'Set C'), 
      set_colors=('orange', 'green', 'purple'), 
      alpha=0.7)

# Add circle outlines
venn3_circles(subsets=(20, 10, 12, 10, 9, 4, 3), linewidth=2)

plt.title("Customized Three-Set Venn Diagram")
plt.show()

Customization Options

Parameter Description Example Values
set_colors Colors for each circle ('red', 'blue', 'green')
alpha Transparency level 0.5 (50% transparent)
linewidth Circle border thickness 1, 2, 3
set_labels Labels for each set ('Group 1', 'Group 2')

Working with Subset Numbers

For three-set Venn diagrams, the subsets parameter takes 7 values representing different regions ?

import matplotlib.pyplot as plt
from matplotlib_venn import venn3

# The 7 numbers represent:
# (A only, B only, AB only, C only, AC only, BC only, ABC)
subset_sizes = (10, 8, 5, 7, 3, 4, 2)

venn3(subsets=subset_sizes, 
      set_labels=('Mathematics', 'Physics', 'Chemistry'))

plt.title("Student Subject Preferences")
plt.show()

Conclusion

The matplotlib-venn library makes creating Venn diagrams straightforward in Python. You can easily customize colors, labels, and styling to create professional-looking diagrams that effectively visualize set relationships and data intersections.

Updated on: 2026-03-27T00:59:34+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements