Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 663
jonas Avatar asked Nov 07 '13 16:11

jonas


People also ask

How do you plot a Venn diagram in Python?

MatPlotLib with Python To plot a Venn diagram, first install Venn diagram using command "pip install matplotlib-venn". Using venn3, plot a 3-set area-weighted Venn diagram.

What is MatPlotLib Venn?

A Venn Diagram (also called primary diagram , set diagram or logic diagram ) is a diagram that shows all possible logical relationships between a finite collection of different sets. Fortunately, the matplotlib_venn library allows to build one easily with Python.

How do you create a Venn diagram in Excel?

On the Insert tab, in the Illustrations group, click SmartArt. In the Choose a SmartArt Graphic gallery, click Relationship, click a Venn diagram layout (such as Basic Venn), and then click OK.


1 Answers

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

like image 99
Hooked Avatar answered Sep 23 '22 14:09

Hooked