How to Create and Customize Venn Diagrams in Python | by Julia Kho | Towards Data Science

Venn diagrams are great for illustrating the relationship between two or three groups; you can easily see the commonalities and differences. This article will show you how to create Venn diagrams in Python and how to customize the diagrams to your liking.
First of all, you will have to install the library to your computer. Use the following command in your terminal.
pip install matplotlib-venn
Once you have it installed, we can start coding. We are going to import the following libraries.
#Import librariesfrom matplotlib_venn import venn2, venn2_circles, venn2_unweightedfrom matplotlib_venn import venn3, venn3_circlesfrom matplotlib import pyplot as plt%matplotlib inline
To create a Venn Diagram for 2 groups, use venn2.
venn2(subsets = (30, 10, 5), set_labels = ('Group A', 'Group B'))
The subsets parameter is a 3 element list where the numbers 30, 10, 5 correspond to Ab, aB, AB.
Ab = Contained in group A, but not B
aB = Contained in group B, but not A
AB = Contained in both group A and B
The set_labels parameter allows you to label your two groups in the Venn diagram.
The default colors of the circles are red and green, but the good news is that you can customize the colors! Add the set_colors parameter to set the colors of your groups. In addition, you can use the alpha parameter to control the transparency.
venn2(subsets = (30, 10, 5), set_labels = ('Group A', 'Group B'), set_colors=('purple', 'skyblue'), alpha = 0.7);
The cool thing about the Venn diagram is that is automatically sizes the circle by the magnitude of the number of items within the circle. However, if you do not wish for the circles to be weighted, you can use the unweighted Venn diagram instead.
The two groups are now the same size even though group A is larger than group B.
venn2_unweighted(subsets = (30, 10, 5), set_labels = ('Group A', 'Group B'), set_colors=('r', 'g'), alpha = 0.5);
You can further customize your Venn diagram by overlaying an outline of the circle over the original. Note that this works just with the weighted Venn diagrams.
venn2(subsets = (30, 10, 5), set_labels = ('Group A', 'Group B'), set_colors=('r', 'g'), alpha = 0.5);venn2_circles(subsets = (30, 10, 5));
If you do not prefer the solid line, you can change to different line style with the linestyle parameter. You can set the line width as well.
venn2(subsets = (30, 10, 5), set_labels = ('Group A', 'Group B'), set_colors=('r', 'g'), alpha = 0.5);venn2_circles(subsets = (30, 10, 5), linestyle='dashed', linewidth=2, color='k');
And to finish it off, let’s give the diagram a title!
plt.title(“I made a Venn Diagram!”);
To create Venn diagrams with 3 groups instead of 2, we switch from venn2 to venn3.
venn3(subsets = (20, 10, 12, 10, 9, 4, 3), set_labels = ('Group A', 'Group B', 'Group C'), alpha = 0.5);
You can customize the colors of each area of the diagram with the get_patch_by_id method. Notice that the middle area where all three circles overlap is now orange.
v.get_patch_by_id('111').set_color('orange')
See the diagram below for the id to call for each area.
Last thing I want to show you is how to change the line widths for each individual circle. If I set the Venn diagram circle to the variable c, I can call each individual circle c[0], c[1], or c[2] and set the line width.
c = venn3_circles(subsets = (20, 10, 12, 10, 9, 4, 3), linestyle='dashed', linewidth=1, color=”grey”)c[0].set_lw(5.0)c[1].set_lw(8.0)c[2].set_lw(2.0)
If you would like to follow along with my Jupyter Notebook, you can find it here.
Lastly, if you’re working with more than 2 or 3 sets, check out the upsetplot library (https://pypi.org/project/UpSetPlot/). Thank you to Renato Bellotti for bringing that to my attention.
Happy Venn Diagramming! Cheers.