Skip to content

Commit

Permalink
Extracted layout logic into separate classes, added typing annotations.
Browse files Browse the repository at this point in the history
  • Loading branch information
konstantint committed Jan 29, 2024
1 parent 6f85565 commit 3788e87
Show file tree
Hide file tree
Showing 30 changed files with 4,591 additions and 3,402 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ build/
dist/
*.pyc
Temp.ipynb
Untitled.ipynb
.ipynb_checkpoints
*.komodoproject
venv/
.cache/
.mypy_cache/
.vscode/
10 changes: 10 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
Version 1.0.0-alpha
--------------

- Dropped support for Python versions below 3.5 (by excluding those versions from CI builds)
- Added typing annotations. Some arguments are now a bit more strictly typed.
E.g. what previously would accept a List now requires (at least as far as type annotations are concerned) a tuple.
- Refactored the code by abstracting away the layout algorithms to allow plug-in customization (Issue #35).
This deprecated the `normalize_to` input argument to the venn2 and venn3 functions, and made
`venn2_unweighted` and `venn3_unweighted` obsolete. These will be removed in some future version.

Version 0.11.10
--------------

Expand Down
46 changes: 26 additions & 20 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,41 @@ The package provides four main functions: ``venn2``,
``venn2_circles``, ``venn3`` and ``venn3_circles``.

The functions ``venn2`` and ``venn2_circles`` accept as their only
required argument a 3-element list ``(Ab, aB, AB)`` of subset sizes,
e.g.::
required argument a 3-element tuple ``(Ab, aB, AB)`` of subset sizes,
and draw a two-circle venn diagram with respective region areas, e.g.::

venn2(subsets = (3, 2, 1))

and draw a two-circle venn diagram with respective region areas. In
the particular example, the region, corresponding to subset ``A and
not B`` will be three times larger in area than the region,
corresponding to subset ``A and B``. Alternatively, you can simply
provide a list of two ``set`` or ``Counter`` (i.e. multi-set) objects instead (new in version 0.7),
e.g.::
In this example, the region, corresponding to subset ``A and not B`` will
be three times larger in area than the region, corresponding to subset ``A and B``.

venn2([set(['A', 'B', 'C', 'D']), set(['D', 'E', 'F'])])
You can also provide a tuple of two ``set`` or ``Counter`` (i.e. multi-set)
objects instead (new in version 0.7), e.g.::

venn2((set(['A', 'B', 'C', 'D']), set(['D', 'E', 'F'])))

Similarly, the functions ``venn3`` and ``venn3_circles`` take a
7-element list of subset sizes ``(Abc, aBc, ABc, abC, AbC, aBC,
7-element tuple of subset sizes ``(Abc, aBc, ABc, abC, AbC, aBC,
ABC)``, and draw a three-circle area-weighted venn
diagram. Alternatively, you can provide a list of three ``set`` or ``Counter`` objects
(rather than counting sizes for all 7 subsets).
diagram. Alternatively, a tuple of three ``set`` or ``Counter`` objects may be provided.

The functions ``venn2`` and ``venn3`` draw the diagrams as a collection of colored
patches, annotated with text labels. The functions ``venn2_circles`` and
``venn3_circles`` draw just the circles.

Sometimes the area weighing needs to be disabled or manually tuned to achieve a visually
better result. This can be achieved as follows::

from matplotlib_venn.layout.venn2 import DefaultLayoutAlgorithm
venn2((1,2,3), layout_algorithm=DefaultLayoutAlgorithm(fixed_subset_sizes=(1,1,1)))

The functions ``venn2_circles`` and ``venn3_circles`` draw just the
circles, whereas the functions ``venn2`` and ``venn3`` draw the
diagrams as a collection of colored patches, annotated with text
labels. In addition (version 0.7+), functions ``venn2_unweighted`` and
``venn3_unweighted`` draw the Venn diagrams without area-weighting.
from matplotlib_venn.layout.venn3 import DefaultLayoutAlgorithm
venn3((7,6,5,4,3,2,1), layout_algorithm=DefaultLayoutAlgorithm(fixed_subset_sizes=(1,1,1,1,1,1,1)))

Note that for a three-circle venn diagram it is not in general
possible to achieve exact correspondence between the required set
sizes and region areas, however in most cases the picture will still
provide a decent indication.
provide a useful representation.

The functions ``venn2_circles`` and ``venn3_circles`` return the list of ``matplotlib.patch.Circle`` objects that may be tuned further
to your liking. The functions ``venn2`` and ``venn3`` return an object of class ``VennDiagram``,
Expand Down Expand Up @@ -94,7 +99,7 @@ A more elaborate example::
arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0.5',color='gray'))
plt.show()

An example with multiple subplots (new in version 0.6)::
An example with multiple subplots::

from matplotlib_venn import venn2, venn2_circles
figure, axes = plt.subplots(2, 2)
Expand All @@ -117,7 +122,8 @@ three sets of objects::

Questions
---------
* If you ask your questions at `StackOverflow <http://stackoverflow.com/>`_ and tag them `matplotlib-venn <http://stackoverflow.com/questions/tagged/matplotlib-venn>`_, chances are high you'll get an answer from the maintainer of this package.
* If you ask your questions at `StackOverflow <http://stackoverflow.com/>`_ and tag them `matplotlib-venn <http://stackoverflow.com/questions/tagged/matplotlib-venn>`_,
chances are high you could get an answer from the maintainer of this package.


See also
Expand Down
31 changes: 20 additions & 11 deletions matplotlib_venn/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'''
"""
Venn diagram plotting routines.
Copyright 2012, Konstantin Tretyakov.
Copyright 2012-2024, Konstantin Tretyakov.
http://kt.era.ee/
Licensed under MIT license.
This package contains routines for plotting area-weighted two- and three-circle venn diagrams.
There are four main functions here: :code:`venn2`, :code:`venn2_circles`, :code:`venn3`, :code:`venn3_circles`.
The :code:`venn2` and :code:`venn2_circles` accept as their only required argument a 3-element list of subset sizes:
The :code:`venn2` and :code:`venn2_circles` accept as their only required argument a 3-element list of subset sizes:
subsets = (Ab, aB, AB)
Expand All @@ -22,16 +22,16 @@
The functions :code:`venn2_circles` and :code:`venn3_circles` simply draw two or three circles respectively,
such that their intersection areas correspond to the desired set intersection sizes.
Note that for a three-circle venn diagram it is not possible to achieve exact correspondence, although in
most cases the picture will still provide a decent indication.
Note that for a three-circle Venn diagram it is not possible to achieve exact correspondence, although in
most cases the picture will still provide a decent representation.
The functions :code:`venn2` and :code:`venn3` draw diagram as a collection of separate colored patches with text labels.
The functions :code:`venn2_circles` and :code:`venn3_circles` return the list of Circle patches that may be tuned further
to your liking.
The functions :code:`venn2` and :code:`venn3` return an object of class :code:`Venn2` or :code:`Venn3` respectively,
which give access to constituent patches and text elements.
The functions :code:`venn2` and :code:`venn3` return an object of class :code:`VennDiagram` which provides access to
constituent patches and text elements.
Example::
Expand All @@ -48,12 +48,21 @@
c[0].set_lw(1.0)
c[0].set_ls('dotted')
plt.title("Sample Venn diagram")
plt.annotate('Unknown set', xy=v.get_text_by_id('100').get_position() - np.array([0, 0.05]), xytext=(-70,-70),
plt.annotate('Unknown set', xy=v.get_label_by_id('100').get_position() - np.array([0, 0.05]), xytext=(-70,-70),
ha='center', textcoords='offset points', bbox=dict(boxstyle='round,pad=0.5', fc='gray', alpha=0.1),
arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0.5',color='gray'))
'''
"""

from matplotlib_venn._venn2 import venn2, venn2_circles
from matplotlib_venn._venn3 import venn3, venn3_circles
from matplotlib_venn._util import venn2_unweighted, venn3_unweighted
___all___ = ['venn2', 'venn2_circles', 'venn3', 'venn3_circles', 'venn2_unweighted', 'venn3_unweighted']
__version__ = '0.11.10'

___all___ = [
"venn2",
"venn2_circles",
"venn3",
"venn3_circles",
"venn2_unweighted",
"venn3_unweighted",
]
__version__ = "1.0.0-alpha"
Loading

0 comments on commit 3788e87

Please sign in to comment.