57

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?

tdube
  • 2,453
  • 2
  • 16
  • 25
jonas
  • 13,559
  • 22
  • 57
  • 75
  • 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? – Hooked Nov 07 '13 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 – jonas Nov 07 '13 at 16:41

3 Answers3

74

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

Hooked
  • 84,485
  • 43
  • 192
  • 261
  • I would like to separate the bubbles though, that is no overlap – jonas Nov 07 '13 at 16:47
  • 6
    @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. – Hooked Nov 07 '13 at 16:49
  • 3
    @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. – Hooked Nov 07 '13 at 17:04
  • 4
    @jonas Is your question "how do I draw a circle and put text in it?". See http://stackoverflow.com/questions/9215658/plot-a-circle-with-pyplot http://stackoverflow.com/questions/3439639/matplotlib-add-circle-to-plot http://stackoverflow.com/questions/17252790/matplotlib-adding-text-with-more-than-one-line-adding-text-that-can-follow-the to get you started ... – Hooked Nov 07 '13 at 19:56
  • 1
    The matplotlib-venn package is great but it currently has an issue that cause some sets to have a [wrong count](https://github.com/konstantint/matplotlib-venn/issues/23). – Franck Dernoncourt Oct 13 '15 at 16:39
  • if you're just looking for some quick and dirty 3set or 2set venn diagram pictures, here's a collection of all 2set and 3set venn diagrams: https://github.com/Ace-Cassidy/Venn-Diagram-Pictures – Ace.C Jun 08 '20 at 17:13
47

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

bitbang
  • 1,804
  • 14
  • 18
4

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

Soerendip
  • 7,684
  • 15
  • 61
  • 128