Matplotlib

Matplotlib Venn Plotting with Examples : An easy explanation

Matplotlib Venn plotting visualizes the number sets. Here, We will see how can we plot a Venn diagram in python using matplotlib-Venn in steps.

Matplotlib Venn plotting Steps :

We will simply draw the matplotlib Venn chart in steps. In order to demonstrate this, We will take two sets.

unique elements of A=15,

unique elements of B=10,

The common element of A AND B =5

Now we will see how to plot the Venn diagram in python.

Step 1: Installing the library

In order to draw the Venn diagram in python. You need to install the matplotlib-Venn Library/python package. We can install it via the below command.

pip install matplotlib-venn

You may use a different package manager for installing this library.

Step 2: Importing packages

We will import these below packages.

from matplotlib_venn import venn2
from matplotlib import pyplot as plt
%matplotlib inline

We need matplotlib for basic plotting. Also venn2 for specifically Venn diagrams.

 

Step 3: Plotting diagram

In this step, We will plot the final diagram. Here we need to call the venn2() with arguments. Let’s see them.

venn2(subsets = (15, 10, 5), set_labels = ('A', 'B'))

 

In the above example, We have seen that the venn() function requires few parameters. Like

1. Subset : ( A, B, A AND B) It is reordering as per our example. Unique elements of A is 15 and B is 10, and common are 5 in numbers.

 

2. set_labels: Strings for nominations. Here have used A and B in the above example. But in real scenarios, it will be a meaningful name.

Complete Code –

In this section, We will merge the complete code.

from matplotlib_venn import venn2
from matplotlib import pyplot as plt
%matplotlib inline
venn2(subsets = (15, 10, 5), set_labels = ('A', 'B'))

Here is the output for that.

Matplotlib venn

 

Conclusion –

Venn diagram is the best way to visualize the sets. In this article, We have seen the implementation of venn diagram in python with a minimal example. We have also described each step and most are self-explanatory to draw a Venn diagram. But if you feel anything doubtful in the complete article, Please let us know.

Thanks

Data Science Learner Team