Skip to content

[Python] Use matplotlib-venn Package To Plot Venn Diagram

Python is a programming language that is very suitable for data analysis. Its clean and concise syntax and a variety of rich packages allow us to use python to accomplish almost any function we want, and we only need to fund the correct package!

Today, what I want to record is how to draw the Venn Diagram by calling python’s matplotlib-venn package. The following is a step-by-step introduction


Venn Diagram

Venn diagram is a kind of figure representing a SET of two or more group, you can clearly see the relationship between different collections.

In below I quote directly from wikipedia’s explanation:

—— From Wiki

Venn diagram is a widely-used diagram style that shows the logical relation between sets, popularized by John Venn in the 1880s. The diagrams are used to teach elementary set theory, and to illustrate simple set relationships in probabilitylogicstatisticslinguistics and computer science. A Venn diagram uses simple closed curves drawn on a plane to represent sets. Very often, these curves are circles or ellipses.

Similar ideas had been proposed before Venn. Christian Weise in 1712 (Nucleus Logicoe Wiesianoe) and Leonhard Euler (Letters to a German Princess) in 1768, for instance, came up with similar ideas. The idea was popularised by Venn in Symbolic Logic, Chapter V “Diagrammatic Representation”, 1881.

Intuitively speaking, suppose we have two sets: Set A and Set B. Then we can draw the two sets into the following diagram show:

From Wikipedia

This is a classic Venn Diagram.


Use Python To Plot Venn Diagram

If you want to draw a venn diagram in python, you can use the matplotlib-venn package.

matplotlib-venn is mainly used to draw a venn diagram between two sets and three sets.

First, we need to install it.

pip3 install matplotlib-venn

After the installation is over, let’s start to draw the simplest two sets of venn diagram.

# coding: utf-8
from matplotlib_venn import venn2
from matplotlib import pyplot as plt


# Venn2
subset = (2, 2, 1)
venn2(subsets=subset)
plt.show()



Output:

As you can see, this Venn diagram is drawn according to the values we set. The corresponding relationship between the set subset and the picture is as follows:

The first two values represent the two sets A and B minus the part of the intersection and the last value represents the value of the intersection.

In addition to this method, we can also directly set the content of each intersection and let the program automatically generate the Venn diagram.

I think this method is better and more flexible.

# coding: utf-8
from matplotlib_venn import venn2
from matplotlib import pyplot as plt


# Venn2
set_a = set(['A', 'B', 'C'])
set_b = set(['B', 'C', 'D'])

venn2(subsets=[set_a, set_b])
plt.show()



Output:

The advantage of this is that we don’t need to actively set the value of each set and the value of the intersection, just set the two sets directly, and the rest of the program will automatically calculate it for us.

In addition, we can also set the color and name of the graph:

# coding: utf-8
from matplotlib_venn import venn2
from matplotlib import pyplot as plt


# Venn2
set_a = set(['A', 'B', 'C'])
set_b = set(['B', 'C', 'D'])

venn2(subsets=[set_a, set_b],
      set_labels=['Set_A', 'Set_B'],
      set_colors=['red', 'blue'])

plt.show()



Output:

The above is the general usage of venn2. Next, by the way, record the usage of venn3. Of course, it is roughly the same as venn2.

# coding: utf-8
from matplotlib_venn import venn3
from matplotlib import pyplot as plt


# Venn3
venn3(subsets=(1, 2, 3, 4, 5, 6, 7))
plt.show()



Output:

A total of 7 values are entered, and the entry sequence is shown in the Venn diagram above.

There is not much difference in the method of directly setting the values of each set and letting the program draw automatically:

# coding: utf-8
from matplotlib_venn import venn3
from matplotlib import pyplot as plt


# Venn3
set_a = set(['A', 'B', 'C'])
set_b = set(['B', 'C', 'D'])
set_c = set(['C', 'D', 'E'])

venn3(subsets=[set_a, set_b, set_c],
      set_labels=['Set_A', 'Set_B', 'Set_C'],
      set_colors=['red', 'blue', 'green'])

plt.show()



Output:


References


Read More

Leave a Reply