I have recently discovered that simple Venn diagrams are surprisingly popular in bioinformatics. So popular they are, in fact, that there are several bioinformatics research papers devoted solely to their use. And those are highly accessed papers, let me add! Yet, despite this wild popularity, tools that let you render a decent Venn diagram programmatically seem to be rather scarce.
If you google a bit, you will find a bunch of on-line tools of varying degrees of quality and ability (1, 2, 3, 4, 5, 6, 7, 8, 9,...), a Java-based tool, a Perl library, a couple of Python scripts (1, 2), some R libraries (1, 2, 3, 4, 5), and lots of forum discussions. Seems to be plenty, doesn't it? Well, it turns out that if you want your diagram to be area-weighted (i.e. the regions of the diagram should be roughly proportional to the corresponding set sizes), 4 of those 18 links won't do. If you want to generate and configure the diagram conveniently from a script, drop another 9. Then, if you want the diagram to look nice, drop 4 more, and all you are left with is the Vennerable R package. Unfortunately, Vennerable plots are still a pain to configure — even adding a plot title seems to be impossible, not speaking of highlighting and annotating a region on the diagram.
Having been totally disappointed in the state of the art of contemporary Venn-diagramming tools, I made a small Python package for drawing Venn diagrams that has the necessary flexibility. At least it lets me put plot titles and annotate diagram regions as I fancy.
Package installation goes by the standard method: easy_install matplotlib-venn
For basic usage examples, consult the PyPI page.








First, thanks for writing this code. It is exactly what I need.
Unfortunately, I cannot run it. It appears to interfere with matplotlib.
Installing matplotlib-venn via pip I have this problem:
Ipython (without pylab)
In [1]: from matplotlib.venn import venn2
…
—> 13 from matplotlib.patches import Circle, PathPatch
14 from matplotlib.path import Path
15 from matplotlib.colors import ColorConverter
ImportError: No module named patches
When I import matplotlib and check the content I get this:
In [2]: import matplotlib
In [4]: dir(matplotlib)
Out[4]: ['__doc__', '__name__', '__path__']
However, uninstalling matplotlib-venn returns things to normal:
In [1]: import matplotlib
In [2]: dir(matplotlib)
Out[2]:
['RcParams',
'Verbose',
'__builtins__',
'__doc__',
'__file__',
'__name__',
'__package__',
'__path__',
'__version__',
'__version__numpy__',
'_basedir',
'_deprecated_ignore_map',
'_deprecated_map',
'_fname',
...
When I install with easy_install I don't get the interference with matplotlib but venn2 is missing:
In [4]: import matplotlib.venn as venn
In [5]: dir(venn)
Out[5]:
['___all___',
'__builtins__',
'__doc__',
'__file__',
'__name__',
'__package__',
'__path__',
'_math',
'_venn3',
'venn3',
'venn3_circles']
Do you have advice?
best,
Hjalmar
Eh, I was suspecting that this whole “namespace package” business won’t work as expected for at least someone (i.e. distributing a package matplotlib.venn from a separate egg).
That means I need to rename the package to something non-conflicting (e.g. matplotlibvenn instead of matplotlib.venn or the like). I’ll fix it today/tomorrow. If you want to try things before, just download the source, rename the matplotlib directory there to something different, edit namespace_packages option in setup.py, and either do python setup.py install or just run python from that directory and do “import whateveryournewname.venn”, etc.
I did a quick fix.
Placed _venn2.py, _venn3.py and _math.py in my working dir and imported venn2 from _venn2.
It worked fine.
Thanks again for writing the code, it was very useful.
I fixed the problem now by renaming the package from matplotlib.venn to matplotlib_venn in the new version.
Thanks for your feedback!
It works fine using pip install.
Thanks
Hi Konstantin,
I really like your venn module – it’s exactly what I need and very easy to use. Thanks!
There’s a bug in the colour-calculation code:
venn2((3,2,1),set_colors=(‘blue’,'lightblue’))
ValueError: to_rgba: Invalid rgba arg “[ 0.47490196 0.59294118 1.33137255]” number in rbg sequence outside 0-1 range
Basically, it doesn’t handle colours that are too bright.
Wow, that was an unexpectedly stupid bug from my side.
Fixed. Do
“
pip install -U --no-deps matplotlib_venn”(or “
easy_install --upgrade matplotlib_venn“)and try again.
Thanks!
Many thanks for this very helpful library!