66

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?

2
  • How do you want to "plot" inside a Venn diagram? Do you simply want the 3 circle diagram with text labels for the different groups? Commented Nov 7, 2013 at 16:39
  • Yes thats right, the names are quite long so it would be nice to have some scaling effect for that on the bubble sizes Commented Nov 7, 2013 at 16:41

4 Answers 4

85

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

Sign up to request clarification or add additional context in comments.

6 Comments

I would like to separate the bubbles though, that is no overlap
@jonas Overlap is what makes a Venn diagram, well, a Venn diagram! It shows the inter-relationship between the categories. For your sake however, it looks like you can simply make a bunch of the singular Venn diagrams and put them together how you please.
@jonas I've added an example. To be fair, I had never used to library before I started this question, I simply read the documentation. Try reading through the link provided, it may help.
@jonas Is your question "how do I draw a circle and put text in it?". See stackoverflow.com/questions/9215658/plot-a-circle-with-pyplot stackoverflow.com/questions/3439639/… stackoverflow.com/questions/17252790/… to get you started ...
The matplotlib-venn package is great but it currently has an issue that cause some sets to have a wrong count.
|
64

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

2 Comments

This answer should be scored-up for being extremely simple and effective. Outstanding!
I agree, that while extremely simple and effective, this is the most straightforward answer.
5

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

2 Comments

This does the job, but seems to be rather slow on large lists(~50k each).
That is a good point. You could use vectorized functions instead.
1
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()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.