How to Design Professional Venn Diagrams in Python | by Chaitanya Baweja | Towards Data Science

Last week I was designing some reading material for my Python Class. The lecture topic was Python Sets.
For illustrating some functions, I wanted to plot Venn Diagrams. Yes, the overlapping circle charts that you often find in data presentations.
My search for open-source tools for designing professional looking Venn Diagrams lead me to tools like Canva, Meta-Chart, and Creately. While they had appealing designs, none of them had the required elegance and were seldom customizable.
Somewhere in the depth of google search, I encountered matplotlib-venn and fell in love with it.
The matplotlib-venn library allows you to completely customize your Venn diagrams, from the size of the circles to border type and line width. Here is a sample plot generated using this library.
After using this library for multiple projects, I decided to share what I have learned through this tutorial. I will explain how to use matplotlib-venn to create your own professional Venn diagrams.
All the examples in this tutorial were tested on a Jupyter notebook running Python 3.7. We will be using matplotlib-venn in this tutorial.
There is an accompanying Jupyter notebook for this tutorial here.
A Venn diagram is an illustration used to depict logical relationships between different groups or sets.
Each group is represented using a circle. The size of each circle corresponds to the size/importance of the group.
Overlaps between these circles represent the intersection between the two sets. These diagrams are thus, especially useful for identifying shared elements between each set.
Let us look at an example for better clarification. Suppose you have a group of students that take multiple subjects. Two of these subjects are English and French.
English = {'John', 'Amy', 'Howard', 'Lucy', 'Alice', 'George', 'Jacob', 'Rajesh', 'Remy', 'Tom'}French = {'Arthur', 'Leonard', 'Karan', 'Debby', 'Bernadette', 'Alice', 'Ron', 'Penny', 'Sheldon', 'John'}
Both subjects have ten students each. Alice and John take both English and French.
Now, we will visualize these sets using a Venn Diagram.
Both circles are of the same size. This is because both these sets have the same number of students.
The overlap between the two circles contains two objects: Alice and John.
Now, that we understand what Venn diagrams are, let us design some using Python.
The first step is to import the required libraries: matplotlib-venn and matplotlib. Then, we define the sets that we wish to plot:
Finally, we will plot our Venn Diagram using the venn2 function of matplotlib-venn:
venn2([English,French])
Output:
The function takes as argument a list of the two sets and plots the required Venn diagram.
We can also add labels to the plot and individual circles.
Output:
Now, let us consider two more subjects: Math and Science.
Math = {'Amy', 'Leo', 'Ash', 'Brandon', 'Sara', 'Alice', 'Ron', 'Annie', 'Jake', 'Zac'}Science = {'Dexter', 'Geeta', 'James', 'Charles', 'Sammy', 'Brandon', 'Aayush', 'Josephy', 'Casey', 'Will'}
We will now plot a Venn diagram with three sets: English, French, and Maths. We do this using the venn3 function:
Output:
As expected, there are overlaps among all three sets. All sets have Alice in common.
Now, let us consider using Science as the third set rather than Math.
Output:
We see that Science is a disjoint set, without any overlap with the other circles.
Suppose there is a mandatory subject called Fire Safety that must be taken by everyone. We can define it by taking a set union of all subjects.
Output:
Sometimes we do not have the set definitions available to us. We only know the size of these sets and the amount of overlap between them. In such situations, we use the subsets parameter:
subsets is a tuple containing three values:
Contained in set A, but not BContained in Set B, but not AIntersection of set A and B
Contained in set A, but not B
Contained in Set B, but not A
Intersection of set A and B
venn2(subsets = (50, 20, 10), set_labels = ('Set A', 'Set B'))
You can find more details and examples using subsets here.
While the default colors, red and green, are suitable in most situations, you can choose different colors as per your convenience.
The set_colors parameter allows us to set the color of a group.
The alpha parameter allows us to control transparency.
venn2([English, French], set_labels = ('English Class', 'French Class'), set_colors=('darkblue', 'yellow'), alpha = 0.8);
Output:
We can also add custom borders to our Venn Diagram. We achieve this by overlaying a customized outline over the original.
We plot this outline using venn2_circles and venn3_circles. Take a look:
We can also select the width and style of our border using linewidth and linestyle parameters respectively.
More examples can be found here.
We can also access each element of the Venn Diagram using their id.
The figure below denotes the IDs for each area of the 2-group and 3-group Venn diagrams.
You can customize the colors of each area of the diagram with the get_patch_by_id method. We can also customize the text for each region using the get_label_by_id method.
Take a look:
Notice that the intersection area is now purple-colored, and the left circle has ‘New Text’ written on it.
More details about customization are available here.
The matplotlib-venn library allows you to completely customize your Venn diagrams, from the size of the circles to border type and line width. It can be used to make both two and three group Venn diagrams.
It is advised not to design Venn diagrams with over three groups. This is still an open issue in the library.
You can find all the snippets in this accompanying Jupyter Notebook.
Hope you found this tutorial useful.
Chaitanya Baweja aspires to solve real world problems with engineering solutions. Follow his journey on Twitter and Linkedin.