Customized Venn diagram in python

Mastering & Customizing Venn Diagrams in Python

A Venn diagram shows all possible logical relationships between a finite collection of different sets.

Thankfully, creating Venn diagrams in Python is easy! The only library you will need is matplotlib_venn. This is easily installed via pip install in the command line.

import matplotlib.pyplot as plt
from matplotlib_venn import venn2, venn2_circles, venn2_unweighted
from matplotlib_venn import venn3, venn3_circles
venn2(subsets=(10, 10, 4), set_labels=('A', 'B'))
plt.show()

You can also create a Venn diagram with sets of unequal sizes:

venn2(subsets=(5, 8, 2), set_labels=('A', 'B'))
plt.show()

There is also an option to create the Venn diagrams as circles, without any labels or coloring:

from matplotlib_venn import venn2_circles

venn2_circles(subsets=(10, 10, 4))

plt.show()

venn2_circles(subsets=(5, 8, 2))

plt.show()
Equal sized sets
Unequal sized sets

The default colors are red and green, but you can use any colors from the matplotlib documentation.

venn2(subsets=(12, 9, 3), set_labels=('A', 'B'), set_colors=('mediumaquamarine', 'slategrey'), alpha=0.75)

If you have sets with unequal numbers but don’t want the different circle sizes, the unweighted library lets you do this:

venn2_unweighted(subsets=(20, 10, 5), set_labels=('A', 'B'))

You can also customize the circles with lines like in the uncolored circles:

# Venn diagram with solid black lines
venn2(subsets=(20, 10, 5), set_labels=('A', 'B'), set_colors=('mediumaquamarine', 'slategrey'), alpha=0.75)
venn2_circles(subsets=(20, 10, 5), linewidth=1)

# Venn diagram with black dashed lines
venn2(subsets=(20, 10, 5), set_labels=('A', 'B'), set_colors=('mediumaquamarine', 'slategrey'), alpha=0.75)
venn2_circles(subsets=(20, 10, 5), linestyle='dashed', linewidth=2, color='k')

# Venn diagram with dotted red lines
venn2(subsets=(20, 10, 5), set_labels=('A', 'B'), set_colors=('mediumaquamarine', 'slategrey'), alpha=0.75)
venn2_circles(subsets=(20, 10, 5), linestyle='dotted', linewidth=2, color='red')

plt.show()
solid black lines
black dashed lines
dotted light red lines

You can also make the lines different sizes between the different circles:

circle = venn2_circles(subsets=(20, 10, 5), linestyle='dashed', linewidth=2, color='dimgrey')
circle[0].set_lw(7.0)
circle[1].set_lw(2.0)

There are also implementations for three sets:

venn3(subsets=(20,10,5,9,4,2,3), set_labels=('A', 'B', 'C'), alpha=0.5)

And you can also customize these just like we show above with the two circles:

circles = venn3_circles(subsets=(15, 7, 1, 5, 1, 1, 1), linewidth=2, color='indigo')
circles[0].set_lw(7.0)
circles[1].set_lw(4.0)
circles[2].set_lw(2.0)

You can do pretty much everything with the three sets that you can with the two, so I will leave that to the reader if interested.

For more information, take a look at the documentation.

Or, follow along with my notebook on GitHub.

One response to “Mastering & Customizing Venn Diagrams in Python”