jonas
jonas

Reputation: 13969

Python Matplotlib Venn diagram

I want to plot variables that belongs to certain groups.

Say that I have 6 variables that I want to sort into these 3 groups and plot like a venn diagram. I would like to annotate the variable names into the three bubbles.
In this simple example we could say that 1 variable is in group 1, 3 variables in group 2 and 2 variables in group 3.

Could anyone help me with a simple example of how to do it in matplotlib?

Upvotes: 64

Views: 110834

Answers (4)

Liliet Lamar
Liliet Lamar

Reputation: 11

import matplotlib.pyplot as plt
from matplotlib_venn import venn2

# Data
female_students_grade_b = 8
male_students_grade_b = 14

# Calculate overlaps
only_female_grade_b = female_students_grade_b - (22 - male_students_grade_b)
only_male_grade_b = male_students_grade_b - (22 - female_students_grade_b)
both_female_and_grade_b = female_students_grade_b - only_female_grade_b

# Create Venn diagram
venn_labels = ('Female', 'Grade B')
venn_values = (only_female_grade_b, only_male_grade_b, 
both_female_and_grade_b)

fig, ax = plt.subplots()

venn = venn2(subsets=venn_values, set_labels=venn_labels, ax=ax)

ax.set_title('Venn Diagram: Female and Grade B')

plt.show()

Upvotes: 1

Soerendip
Soerendip

Reputation: 9185

Here you can just pass the arrays and the overlaps are calculated.

import numpy as np
from matplotlib_venn import venn3

def venn_diagram(a, b, c, labels=['A', 'B', 'C']):

    a = set(a)
    b = set(b)
    c = set(c)

    only_a = len(a - b - c)
    only_b = len(b - a - c)
    only_c = len(c - a - b)

    only_a_b = len(a & b - c)
    only_a_c = len(a & c - b)
    only_b_c = len(b & c - a)

    a_b_c = len(a & b & c)

    venn3(subsets=(only_a, only_b, only_a_b, only_c, only_a_c, only_b_c, a_b_c), set_labels=labels)

a, b, c = np.round(np.random.rand(3, 50000), 5)
venn_diagram(a, b, c)

link to image

Upvotes: 5

bitbang
bitbang

Reputation: 2192

simplest way to draw venn diagrams

import matplotlib.pyplot as plt
from matplotlib_venn import venn3

set1 = set(['A', 'B', 'C'])
set2 = set(['A', 'B', 'D'])
set3 = set(['A', 'E', 'F'])

venn3([set1, set2, set3], ('Group1', 'Group2', 'Group3'))

plt.show()

enter image description here

Upvotes: 63

Hooked
Hooked

Reputation: 88218

There is a beautiful Venn diagram add-on for matplotlib called matplotlib-venn. It looks like it can be completely customized to do what you are looking for, from the size of the circles (proportional to the set size), to inner and outer labels.

Using the example code on the website gives a plot like:

enter image description here

Edit: Per the comments below the following code gives non-overlapping circles with text using the same library:

import pylab as plt
from matplotlib_venn import venn3, venn3_circles

v = venn3(subsets=(1,1,0,1,0,0,0))
v.get_label_by_id('100').set_text('First')
v.get_label_by_id('010').set_text('Second')
v.get_label_by_id('001').set_text('Third')
plt.title("Not a Venn diagram")
plt.show()

Gives the diagram:

enter image description here

Upvotes: 84

Related Questions