How to Create and Customize Venn Diagrams in Python?
Venn Diagrams are useful for illustrating relations between two or more groups. We can easily see commonalities and differences between different groups. In this article, we are going to discuss how to create and customize Venn diagrams in Python:
Simple Venn Diagram:
Install matplotlib-venn Library in your computer (Here we used the tool Pycharm) go to the terminal and use the following command.
pip install matplotlib-venn
After installing the library create a new python file and import the libraries as explained in the below program:
The statement venn2(subsets = (30, 10, 5), set_labels = (‘Group A’, ‘Group B’)) refers to the subset’s parameter is a 3 element list where the numbers 50, 10, 7 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 show() function in pyplot module of matplotlib library is used to display all figures.
Below are various examples that depict how to create and customize Venn diagrams:
Venn Diagrams automatically size the circle depending upon the magnitude of items allotted. However, we can disable this by using an unweighted Venn Diagram, so the circles appear in the same size irrespective of the items allotted.
The default colors of Venn Diagrams are red and green now we will customize the colors orange and blue using set_colors parameter. The alpha parameter is used to control the transparency.
We can customize the outline of the circle note it works on weighted Venn Diagrams which are shown in the below program.
We can also customize the outline of the circle with dashed line style and line width:
A title can be assigned to Venn diagrams using the title() method.
Let us draw three Venn Diagrams use venn3, venn3_circles modules.
Let us customize the colors of each area of the diagram with the get_patch_by_id() method.