Venn Diagram in Python Programming

Hey there fellow Python coder! In this tutorial, we will be learning what a Venn Diagram is and how one can implement the same in various ways using Python Programming Language.

Let’s get right on it!

Introduction to Venn Diagrams

Venn diagrams are a way to show the relationship between different sets of things. They help organize information and see where different categories overlap or have things in common.

Let’s understand better through an example: You can make circles to represent different groups, such as “Fruits” and “Vegetables” Where these circles overlap, you can show eatables that are both fruits and vegetables, like “Tomato”. Have a look at the examples mentioned below to understand the concept through examples.

Venn Diagram in Python

Understanding Basic Terminologies

Before moving on to the implementation of Venn Diagrams in Python programming we need to understand some basic terminologies that are related to Venn Diagrams.

  1. Set: A set is a collection of objects or elements. For example, you can have a set of fruits, animals, planets, and colors. ( As mentioned in the image above.)
  2. Intersection: The intersection of two sets is the portion where they overlap. In the first Venn Diagram above, Tomato is the Intersection of two sets.
  3. Union: The union of two sets is the combination of both sets. In a Venn diagram, it’s the total area covered by both circles.
  4. Complement: The complement of a set consists of all elements that are not in that set.
  5. Universal Set: The universal set is the set that contains all the elements in all the circles/sets.
  6. Disjoint Sets: Sets are disjoint if they have no elements in common.

To understand all the terminologies much better have a look at the image below.

Basic Terminologies - Venn Diagram

Types of Venn Diagrams

I know you are waiting to get right on implementing Venn Diagrams but Hang tight we just need to understand the types of Venn Diagrams available. There are two types of Venn Diagram: Two-set Venn Diagrams and Three-set Venn Diagrams.

Venn Diagram Types

Two-set Venn Diagrams: A two-set Venn diagram is a visual representation that uses two circles to show the relationships between two sets of objects or elements.

Three-set Venn Diagrams: A three-set Venn diagram is a visual representation that uses three overlapping circles to illustrate the relationships between three sets of objects or elements.

Code Implementation of Venn Diagram in Python

In Python, various libraries make it easy for both beginners and advanced coders to create simple and interactive Venn diagrams. In this tutorial, we will explore three implementations of the Venn Diagram using the Matplotlib library. Matplotlib can be used to implement the simplest Venn diagrams but might require some manual work to draw them.

Matplotlib comes up with a sub-library called venn which provides the functionality to display Venn Diagrams in your system. First of all, make sure that matplotlib library is installed in your system. If not then install the same using pip install matplotlib.

We will start with implementing a basic Venn Diagram using Matplotlib.

import matplotlib.pyplot as plt
from matplotlib_venn import venn2

venn2(subsets=(100, 150, 50), set_labels=('Fruits', 'Vegetables'))
plt.show()

venn2(subsets = ? , set_labels = ? ) : The function takes two arguments called “subsets” and “set_labels”.

The subsets specify the sizes of the three regions in the Venn diagram. The first value represents the size of the first circle, the second value represents the size of the second circle, and the third value represents the size of the intersection between the two circles. The set_labels specifies the names of the two circular categories.

The output that is displayed on the screen after executing the code is as follows:

Matplotlib - Venn Diagram Output

User-Defined Dataset Venn Diagram

What if we don’t wish to specify the intersection size ourselves but want the code to automatically identify the intersection of two circles based on the dataset?

Let’s do that by first creating a dataset of two sets for fruits and vegetables individually.

setA = {'Apple', 'Banana', 'Orange', 'Tomatoes', 'Grapes', 'Strawberry', 'Pumpkins', 'Watermelon', 'Pineapple', 'Cucumbers', 'Mango', 'Kiwi', 'Peach'}
setB = {'Pumpkins', 'Carrot', 'Cucumbers', 'Broccoli', 'Spinach', 'Tomato', 'Cucumber', 'Tomatoes', 'Bell Pepper'}

Then as we did before let’s create a simple Venn Diagram for the two sets using the code below:

from matplotlib_venn import venn2
from matplotlib_venn import venn2_circles

venn2([setA, setB], set_labels=('Fruits', 'Vegetables'), set_colors = ('skyblue', 'lavender'))
venn2_circles([setA, setB])
plt.show()

You might have noticed some new things in this code. Yes, first of all, we have used venn2_circles functionality. Let’s understand what it does. venn2_circles is used to draw the circular boundary around the created Venn Diagram in the plot.

Secondly, we have used set_colors which sets the colors for the two circles that will be present in the Venn Diagrams.

Here have a look at the output of the code.

Matplotlib - Venn Diagram Output

You see how the Venn Diagram automatically counted the intersection elements from the two sets.

Customizing Venn Diagram

But you know there is still an issue here. The counts don’t show any information about the original dataset as there are no words at all. Let’s change that!

Let’s start with the original code that we just learned a moment ago. The only difference in the code is that we will store the Venn diagram in a variable named venn_diagram.

from matplotlib_venn import venn2
from matplotlib_venn import venn2_circles
import matplotlib.pyplot as plt

setA = {'Apple', 'Banana', 'Orange', 'Tomatoes', 'Grapes', 'Strawberry', 'Pumpkins', 'Watermelon', 'Pineapple', 'Cucumbers', 'Mango', 'Kiwi', 'Peach'}
setB = {'Pumpkins', 'Carrot', 'Cucumbers', 'Broccoli', 'Spinach', 'Tomato', 'Cucumber', 'Tomatoes', 'Bell Pepper'}

venn_diagram = venn2([setA, setB], set_labels=('Fruits', 'Vegetables'), set_colors = ('skyblue', 'lavender'))
venn2_circles([setA, setB])

Now let’s learn how to add labels to the respective circular sets in the Venn Diagram. To achieve that we will make use of a combination of two functions called get_label_by_id and set_text.

get_label_by_id() is used to get the text label associated with a specific region with the help of a combination of binary codes as the function’s parameter. You might be wondering what binary code is. The binary code is as follows:

  • 10  – Only in SET A
  • 01  – Only in SET B
  • 11  – Intersection of SET A and SET B

set_text() allows you to customize the label text for a specific region in the Venn diagram.

Let’s understand it much better through code implementation.

venn_diagram.get_label_by_id('10').set_text('\n'.join(setA - setB))
venn_diagram.get_label_by_id('01').set_text('\n'.join(setB - setA))
venn_diagram.get_label_by_id('11').set_text('\n'.join(setA & setB))

So let’s understand what exactly is happening in the code above. In every line, we are first getting the value of the label using the unique binary code and then setting the text to the respective values needed.

One new thing you might have found is the values inside the join function. Let me explain what exactly the values mean:

  • setA – setB: This implies those elements which are present in setA but not in setB. So exclusively setA.
  • setB – setA: This is opposite to setA – setB. Hence exclusively setB.
  • setA & setB: The elements that are the intersection of setA and setB.

Here have a look at what the final output looks like after adding the values of the elements instead of numbers.

Matplotlib - Venn Diagram - Output 3

Conclusion

So that’s all for now. Go ahead, and have some fun experimenting with different types of Venn Diagrams. Why not give a three-set Venn diagram a shot by yourself? All you need to do is pass a few extra values/ data to the same function.

Keep reading to learn some more interesting concepts! You can also have a look at the following content:

  1. Waffle Chart using pyWaffle in Python
  2. Plot Polar Chart in Python using matplotlib
  3. Create a pie chart using Matplotlib in Python

Leave a Reply

Your email address will not be published. Required fields are marked *