How to Create a Beautify Venn Diagrams in Python | by Di(Candice) Han | Towards Data Science

Venn diagram is the most common diagram in scientific research articles and can be used to represent the relationship between multiple data sets. From Venn diagram, you can easily detect the commonalities and differences among those datasets. This tutorial will show you three different ways to create Venn diagrams in Python and how to beautify these diagrams.
Part 1: How to Create Venn Diagram
Step1: you need to install the library named matplotlib-venn.
pip install matplotlib-venn
Step2: import libraries
#Import librariesfrom matplotlib_venn import venn2, venn2_circles, venn2_unweightedfrom matplotlib_venn import venn3, venn3_circlesfrom matplotlib import pyplot as plt%matplotlib inline
Step3: create datasets for data visualization
The letters in each dataset represent the student names and we would like to visualize how many students are enrolled in each course, how many of them are enrolled in two and three courses.
Course1=[‘A’,’B’,’C’,’E’,’F’,’G’,’I’,’P’,’Q’]Course2=[‘B’,’E’,’F’,’H’,’K’,’Q’,’R’,’S’,’T’,’U’,’V’,’Z’]Course3=[‘C’,’E’,’G’,’H’,’J’,’K’,’O’,’Q’,’Z’]
Step4: visualize Venn diagram
Let us start to draw a Venn Diagram with 2 groups.
There are 3 main methods to make a Venn diagram with the matplotlib library, leading to the same result.
Method 1 is the most straightforward one. Put two datasets directly.
#Method1: put two datasets directlyvenn2([set(Course1), set(Course2)])plt.show()
You will get a Venn diagram like this:
For method 2, you need to know below numbers first.
Ab = Contained in the left group (indicated as A here), but not the right group (indicated as B here)
aB = Contained in right group B, but not the left group A
AB = A⋂B; contained in both groups.
The subsets parameter is a 3 element list where the numbers 5, 8, 4 correspond to Ab, aB, AB.
#Method 2: venn2(subsets = (5, 8, 4))plt.show()
You will get the same diagram as Venn Diagram 1. Matplotlib automatically assigned A and B to the Venn diagram.
For method 3, you need to pass a dictionary to the parameter subset.
#Method 3: venn2(subsets = {‘10’: 5, ‘01’: 8, ‘11’: 4})plt.show()
The key is the binary encoding method. So the three keys must be “10”, “01” and “11”, and each value after the key represents the size of the corresponding area.
Part 2: beautify the Venn diagram
change labels
change labels
venn2([set(Course1), set(Course2)],set_labels=(‘Course1’, ‘Course2’))plt.show()
2. set colors and opacity
venn2([set(Course1), set(Course2)],set_labels=(‘Course1’, ‘Course2’),set_colors=(‘orange’, ‘darkgrey’), alpha = 0.8)plt.show()
3. chang the circles
venn2([set(Course1), set(Course2)],set_labels=(‘Course1’, ‘Course2’),set_colors=(‘orange’, ‘darkgrey’), alpha = 0.8)venn2_circles([set(dataset1), set(dataset2)], linestyle=’-.’, linewidth=2, color=’black’)plt.show()
Matplotlib provides a variety of different linestyles. More information about linestyle can be found on Matplotlib website.
4. change the size of label and numbers
vd2=venn2([set(Course1), set(Course2)],set_labels=(‘Course1’, ‘Course2’),set_colors=(‘orange’, ‘darkgrey’), alpha = 0.8)venn2_circles([set(Course1), set(Course2)], linestyle=’-.’, linewidth=2, color=’black’)for text in vd2.set_labels:  #change label size text.set_fontsize(16);for text in vd2.subset_labels:  #change number size text.set_fontsize(16)plt.show()
5. add title to the chart
To complete the Venn diagram, title has to be added.
vd2=venn2([set(Course1), set(Course2)],set_labels=(‘Course1’, ‘Course2’),set_colors=(‘orange’, ‘darkgrey’), alpha = 0.8)venn2_circles([set(Course1), set(Course2)], linestyle=’-.’, linewidth=2, color=’black’)for text in vd2.set_labels: text.set_fontsize(16);for text in vd2.subset_labels: text.set_fontsize(16)plt.title(‘Venn Diagram for Course1 and Course2’,fontname=’Times New Roman’,fontweight=’bold’,fontsize=20,pad=30,backgroundcolor=’#cbe7e3',color=’black’,style=’italic’);plt.show()
Drawing a Venn diagram with 3 Groups has no significant difference from generating a Venn diagram with 2 Groups.
vd3=venn3([set(Course1),set(Course2),set(Course3)], set_labels=(‘Course1’, ‘Course2’,’Course3'), set_colors=(‘#c4e6ff’, ‘#F4ACB7’,’#9D8189'),  alpha = 0.8)venn3_circles([set(Course1), set(Course2),set(Course3)], linestyle=’-.’, linewidth=2, color=’grey’)for text in vd3.set_labels: text.set_fontsize(16);for text in vd3.subset_labels: text.set_fontsize(16)plt.title(‘Venn Diagram for 3 courses’,fontname=’Times New Roman’,fontweight=’bold’,fontsize=20, pad=30,backgroundcolor=’#cbe7e3',color=’black’,style=’italic’);plt.show()
Matplotlib allows us to customize each circle separately. Let us customize the upper left circle.
vd3=venn3([set(Course1),set(Course2),set(Course3)], set_labels=(‘Course1’, ‘Course2’,’Course3'), set_colors=(‘#c4e6ff’, ‘#F4ACB7’,’#9D8189'),  alpha = 0.8)c=venn3_circles([set(Course1), set(Course2),set(Course3)], linestyle=’-.’, linewidth=2, color=’grey’)for text in vd3.set_labels: text.set_fontsize(16);for text in vd3.subset_labels: text.set_fontsize(16)plt.title(‘Venn Diagram for 3 courses’,fontname=’Times New Roman’,fontweight=’bold’,fontsize=20, pad=30,backgroundcolor=’#cbe7e3',color=’black’,style=’italic’);c[0].set_lw(3.0) #customize upper left circle c[0].set_ls(‘:’)plt.show()
The color of the circle can be customized as well. However, if we assign a new color to the circle, it will overwrite the colors we set by parameter set_colors in venn3 function.
vd3=venn3([set(Course1),set(Course2),set(Course3)], set_labels=(‘Course1’, ‘Course2’,’Course3'), set_colors=(‘#c4e6ff’, ‘#F4ACB7’,’#9D8189'),  alpha = 0.8)c=venn3_circles([set(Course1), set(Course2),set(Course3)], linestyle=’-.’, linewidth=2, color=’grey’)for text in vd3.set_labels: text.set_fontsize(16);for text in vd3.subset_labels: text.set_fontsize(16)plt.title(‘Venn Diagram for 3 courses’,fontname=’Times New Roman’,fontweight=’bold’,fontsize=20, pad=30,backgroundcolor=’#cbe7e3',color=’black’,style=’italic’);c[0].set_lw(7.0)c[0].set_ls(‘:’)c[0].set_color(‘#c4e6ff’)plt.show()
Enjoy Venn Diagramming!