├── .gitignore
├── .travis.yml
├── CHANGELOG.txt
├── DEVELOPER-README.rst
├── LICENSE
├── MANIFEST.in
├── README.rst
├── matplotlib_venn
├── __init__.py
├── _arc.py
├── _common.py
├── _math.py
├── _region.py
├── _util.py
├── _venn2.py
├── _venn3.py
└── layout
│ ├── __init__.py
│ ├── api.py
│ ├── venn2
│ ├── __init__.py
│ └── exact.py
│ └── venn3
│ ├── __init__.py
│ ├── cost_based.py
│ └── pairwise.py
├── pyproject.toml
├── setup.cfg
├── setup.py
└── tests
├── __init__.py
├── functional_test.py
├── issues_test.py
├── math_test.py
├── region_label_visual.ipynb
├── region_test.py
├── region_visual.ipynb
├── utils.py
├── venn2_functional.ipynb
├── venn3_functional.ipynb
├── venn3_pairwise_layout_test.py
└── venn_ax_kw_test.py
/.gitignore:
--------------------------------------------------------------------------------
1 | *.egg-info
2 | build/
3 | dist/
4 | *.pyc
5 | Temp.ipynb
6 | Untitled.ipynb
7 | .ipynb_checkpoints
8 | *.komodoproject
9 | venv*/
10 | .cache/
11 | .mypy_cache/
12 | .vscode/
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | dist: focal
2 | language: python
3 | python:
4 | - "3.5"
5 | - "3.6"
6 | - "3.7"
7 | - "3.8"
8 | - "3.9"
9 | - "3.10"
10 | - "3.11"
11 | - "3.12"
12 |
13 | install:
14 | - which python
15 | - python -m venv venv
16 | - source venv/bin/activate
17 | - pip install -e ".[shapely]"
18 | - pip install pytest
19 |
20 | script:
21 | - py.test
22 |
--------------------------------------------------------------------------------
/CHANGELOG.txt:
--------------------------------------------------------------------------------
1 | Version 1.1.2
2 | -------------
3 | - Fixed floating-point comparison in a doctest (Issue #85)
4 | - Fixed IPynb-based tests to comply with the new locals() semantics in Python 3.13 (Issue #86)
5 |
6 | Version 1.1.1
7 | --------------
8 |
9 | - Removed cost_based.LayoutAlgorithmOptions (options are passed directly to the LayoutAlgorithm constructor instead).
10 |
11 | Version 1.1.0
12 | --------------
13 |
14 | - Implemented a cost-based layout algorithm (Issue #35).
15 | The implementation depends on shapely, which is added as an optional
16 | dependency (package needs to be installed as matplotlib-venn[shapely] for
17 | it to be included).
18 | Thanks to https://github.com/paulbrodersen
19 | - Added a set diagram to the README (Issue #51).
20 | - Fixed doctests that started failing due to different numpy versions representing values differently.
21 |
22 | Version 1.0.0
23 | --------------
24 |
25 | - Fixed package installation issues (Issue #78).
26 |
27 | Version 1.0.0-alpha
28 | --------------
29 |
30 | - Dropped support for Python versions below 3.5 (by excluding those versions from CI builds).
31 | - Added typing annotations. Some arguments are now a bit more strictly typed.
32 | E.g. what previously would accept a List now requires (at least as far as type annotations are concerned) a tuple.
33 | - Refactored the code by abstracting away the layout algorithms to allow plug-in customization (Issue #35).
34 | This deprecated the `normalize_to` input argument to the venn2 and venn3 functions, and made
35 | `venn2_unweighted` and `venn3_unweighted` obsolete. These will be removed in some future version.
36 |
37 | Version 0.11.10
38 | --------------
39 |
40 | - Updated tests to work with Matplotlib 3.6+ (PR#70).
41 |
42 | Version 0.11.9
43 | --------------
44 |
45 | - Minor update to metadata (mark Development Status as Stable).
46 |
47 | Version 0.11.8
48 | --------------
49 |
50 | - Added pyproject.toml (Issue #71).
51 |
52 | Version 0.11.7
53 | --------------
54 |
55 | - Fixed Travis-CI-related error messages.
56 |
57 | Version 0.11.6
58 | --------------
59 |
60 | - Added matplotlib_venn.__version__ field (Issue #59).
61 |
62 | Version 0.11.5
63 | --------------
64 |
65 | - Added subset_label_formatter parameter (PR#28).
66 |
67 | Version 0.11.4
68 | --------------
69 |
70 | - Added support for Counter objects (PR#26).
71 |
72 | Version 0.11.3
73 | --------------
74 |
75 | - Tiny change in README required a version bump to upload it to PyPi.
76 |
77 | Version 0.11.2
78 | --------------
79 |
80 | - Fixes issue #24.
81 | - Addresses Debian bug #813782.
82 |
83 | Version 0.11
84 | ------------
85 |
86 | - Fixed issue #17. This would change the previous layout of circles in certain pathological cases.
87 |
88 | Version 0.10
89 | ------------
90 |
91 | - Completely rewritten the region generation logic, presumably fixing all of the problems behind issue #14
92 | (and hopefully not introducing too many new bugs). The new algorithm positions the labels in a different way,
93 | which may look slightly worse than the previous one in some rare cases.
94 | - New kind of IPython-based tests.
95 |
96 | Version 0.9
97 | -----------
98 |
99 | - Better support for weird special cases in Venn3 (i.e. one circle being completely inside another, issue #10).
100 |
101 | Version 0.8
102 | -----------
103 |
104 | - Added support for Python 3.
105 |
106 | Version 0.7
107 | -----------
108 |
109 | - Added the possibility to provide sets (rather than subset sizes) to venn2 and venn3.
110 | Thanks to https://github.com/aebrahim
111 | - Functions won't bail out on sets of size 0 now (the diagrams won't look pretty, though).
112 | Thanks to https://github.com/olgabot
113 | - Venn2/Venn3 objects now provide information about the coordinates and radii of the circles.
114 | - Utility functions added for drawing unweighed diagrams (venn2_unweighted, venn3_unweighted)
115 | - Labels for zero-size sets can be switched off using a method of VennDiagram.
116 | - Some general code refactoring.
117 |
118 | Version 0.6
119 | -----------
120 |
121 | - Added "ax" keyword to the plotting routines to specify the axes object on which the diagram will be created.
122 | Thanks goes to https://github.com/sinhrks
123 |
124 | Version 0.5
125 | -----------
126 |
127 | - Fixed a bug (issue 1, "unreferenced variable 's'" in venn2 and venn2_circles)
128 |
129 | Version 0.4
130 | -----------
131 |
132 | - Fixed a bug ("ValueError: to_rgba: Invalid rgba arg" when specifying lighter set colors)
133 |
134 | Version 0.3
135 | -----------
136 |
137 | - Changed package name from `matplotlib.venn` to `matplotlib_venn`.
138 | - Fixed up some places to comply with pep8 lint checks.
139 |
140 | Version 0.2
141 | -----------
142 |
143 | - Changed parameterization of venn3 and venn3_circles (now expects 7-element vectors as arguments rather than 8-element).
144 | - 2-set venn diagrams (functions venn2 and venn2_circles)
145 | - Added support for non-intersecting sets ("Euler diagrams")
146 | - Minor fixes here and there.
147 |
148 | Version 0.1
149 | -----------
150 |
151 | - Initial version, three-circle area-weighted venn diagrams.
152 |
--------------------------------------------------------------------------------
/DEVELOPER-README.rst:
--------------------------------------------------------------------------------
1 | ====================================================
2 | Developer notes for Python/Matplotlib
3 | ====================================================
4 |
5 | Starting development
6 | --------------------
7 |
8 | The package is formatted as a standard Python setuptools package, so
9 | you you can use::
10 |
11 | $ python setup.py develop
12 |
13 | to temporarily add it to your Python path. To remove it from the path use::
14 |
15 | $ python setup.py develop -u
16 |
17 |
18 | Running the tests
19 | -----------------
20 |
21 | The recommended way to run package test is via `py.test `_.
22 | If you have it installed, just typing::
23 |
24 | $ py.test
25 |
26 | from the current directory will suffice. Note that ``setup.cfg`` contains some configuration
27 | for ``py.test``. You may change the settings there while developing some feature to speed-up test runs.
28 | For example, adding the name of a particular module to the end of the ``addopts`` setting will
29 | limit test runs to that module only.
30 |
31 | If you do not have ``py.test`` installed, you may run the tests via::
32 |
33 | $ python setup.py test
34 |
35 | However, this will install the ``py.test`` egg locally in this directory and takes a bit more time to run.
36 |
37 | If you plan to contribute code, please, test that it works both for Python 2.x and Python 3.x.
38 |
39 |
40 | Functional tests
41 | -----------------
42 |
43 | The functional tests for the package are developed using the ``ipython`` notebook interface
44 | and stored in the ``tests/*.ipynb`` files. Those notebook files are executed automatically when ``py.test`` is run
45 | from the code in ``tests/functional_test.py``.
46 |
47 | To review and develop functional tests you therefore have to install ``ipython[notebook]``::
48 |
49 | $ pip install ipython[notebook]
50 |
51 | In order for the notebook code to execute correctly, the ``matplotlib_venn`` and ``tests`` packages must be in
52 | your Python's scope, which will happen automatically if you did ``python setup.py develop`` before.
53 |
54 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2012, Konstantin Tretyakov
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4 |
5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6 |
7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------------------------------------
/MANIFEST.in:
--------------------------------------------------------------------------------
1 | include README.rst CHANGELOG.txt
--------------------------------------------------------------------------------
/README.rst:
--------------------------------------------------------------------------------
1 | ====================================================
2 | Venn diagram plotting routines for Python/Matplotlib
3 | ====================================================
4 |
5 | .. image:: https://travis-ci.org/konstantint/matplotlib-venn.png?branch=master
6 | :target: https://travis-ci.org/konstantint/matplotlib-venn
7 |
8 | Routines for plotting area-weighted two- and three-circle venn diagrams.
9 |
10 | Installation
11 | ------------
12 |
13 | Install the package as usual via ``pip``::
14 |
15 | $ python -m pip install matplotlib-venn
16 |
17 | Since version 1.1.0 the package includes an extra "cost based" layout algorithm for `venn3` diagrams,
18 | that relies on the `shapely` package, which is not installed as a default dependency. If you need the
19 | new algorithm (or just have nothing against installing `shapely` along the way), instead do::
20 |
21 | $ python -m pip install "matplotlib-venn[shapely]"
22 |
23 | It is quite probable that `shapely` will become a required dependency eventually in one of the future versions.
24 |
25 | Dependencies
26 | ------------
27 |
28 | - ``numpy``,
29 | - ``scipy``,
30 | - ``matplotlib``,
31 | - ``shapely`` (optional).
32 |
33 | Usage
34 | -----
35 | The package provides four main functions: ``venn2``,
36 | ``venn2_circles``, ``venn3`` and ``venn3_circles``.
37 |
38 | The functions ``venn2`` and ``venn2_circles`` accept as their only
39 | required argument a 3-element tuple ``(Ab, aB, AB)`` of subset sizes,
40 | and draw a two-circle venn diagram with respective region areas, e.g.::
41 |
42 | venn2(subsets = (3, 2, 1))
43 |
44 | In this example, the region, corresponding to subset ``A and not B`` will
45 | be three times larger in area than the region, corresponding to subset ``A and B``.
46 |
47 | You can also provide a tuple of two ``set`` or ``Counter`` (i.e. multi-set)
48 | objects instead (new in version 0.7), e.g.::
49 |
50 | venn2((set(['A', 'B', 'C', 'D']), set(['D', 'E', 'F'])))
51 |
52 | Similarly, the functions ``venn3`` and ``venn3_circles`` take a
53 | 7-element tuple of subset sizes ``(Abc, aBc, ABc, abC, AbC, aBC,
54 | ABC)``, and draw a three-circle area-weighted Venn
55 | diagram:
56 |
57 | .. image:: https://user-images.githubusercontent.com/13646666/87874366-96924800-c9c9-11ea-8b06-ac1336506b59.png
58 |
59 | Alternatively, a tuple of three ``set`` or ``Counter`` objects may be provided.
60 |
61 | The functions ``venn2`` and ``venn3`` draw the diagrams as a collection of colored
62 | patches, annotated with text labels. The functions ``venn2_circles`` and
63 | ``venn3_circles`` draw just the circles.
64 |
65 | The functions ``venn2_circles`` and ``venn3_circles`` return the list of ``matplotlib.patch.Circle`` objects that may be tuned further
66 | to your liking. The functions ``venn2`` and ``venn3`` return an object of class ``VennDiagram``,
67 | which gives access to constituent patches, text elements, and (since
68 | version 0.7) the information about the centers and radii of the
69 | circles.
70 |
71 | Basic Example::
72 |
73 | from matplotlib_venn import venn2
74 | venn2(subsets = (3, 2, 1))
75 |
76 | For the three-circle case::
77 |
78 | from matplotlib_venn import venn3
79 | venn3(subsets = (1, 1, 1, 2, 1, 2, 2), set_labels = ('Set1', 'Set2', 'Set3'))
80 |
81 | A more elaborate example::
82 |
83 | from matplotlib import pyplot as plt
84 | import numpy as np
85 | from matplotlib_venn import venn3, venn3_circles
86 | plt.figure(figsize=(4,4))
87 | v = venn3(subsets=(1, 1, 1, 1, 1, 1, 1), set_labels = ('A', 'B', 'C'))
88 | v.get_patch_by_id('100').set_alpha(1.0)
89 | v.get_patch_by_id('100').set_color('white')
90 | v.get_label_by_id('100').set_text('Unknown')
91 | v.get_label_by_id('A').set_text('Set "A"')
92 | c = venn3_circles(subsets=(1, 1, 1, 1, 1, 1, 1), linestyle='dashed')
93 | c[0].set_lw(1.0)
94 | c[0].set_ls('dotted')
95 | plt.title("Sample Venn diagram")
96 | plt.annotate('Unknown set', xy=v.get_label_by_id('100').get_position() - np.array([0, 0.05]), xytext=(-70,-70),
97 | ha='center', textcoords='offset points', bbox=dict(boxstyle='round,pad=0.5', fc='gray', alpha=0.1),
98 | arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0.5',color='gray'))
99 | plt.show()
100 |
101 | An example with multiple subplots::
102 |
103 | from matplotlib_venn import venn2, venn2_circles
104 | figure, axes = plt.subplots(2, 2)
105 | venn2(subsets={'10': 1, '01': 1, '11': 1}, set_labels = ('A', 'B'), ax=axes[0][0])
106 | venn2_circles((1, 2, 3), ax=axes[0][1])
107 | venn3(subsets=(1, 1, 1, 1, 1, 1, 1), set_labels = ('A', 'B', 'C'), ax=axes[1][0])
108 | venn3_circles({'001': 10, '100': 20, '010': 21, '110': 13, '011': 14}, ax=axes[1][1])
109 | plt.show()
110 |
111 | Perhaps the most common use case is generating a Venn diagram given
112 | three sets of objects::
113 |
114 | set1 = set(['A', 'B', 'C', 'D'])
115 | set2 = set(['B', 'C', 'D', 'E'])
116 | set3 = set(['C', 'D',' E', 'F', 'G'])
117 |
118 | venn3([set1, set2, set3], ('Set1', 'Set2', 'Set3'))
119 | plt.show()
120 |
121 | Tuning the diagram layout
122 | -------------------------
123 |
124 | Note that for a three-circle venn diagram it is not in general
125 | possible to achieve exact correspondence between the required set
126 | sizes and region areas. The default layout algorithm aims to correctly represent:
127 |
128 | * Relative areas of the full individual sets (A, B, C).
129 | * Relative areas of pairwise intersections of sets (A&B, A&C, B&C, not to be confused with the regions
130 | A&B&~C, A&~B&C, ~A&B&C, on the diagram).
131 |
132 | Sometimes the result is unsatisfactory and either the area weighting or the layout logic needs
133 | to be tuned.
134 |
135 | The area weighing can be adjusted by providing a `fixed_subset_sizes` argument to the `DefaultLayoutAlgorithm`::
136 |
137 | from matplotlib_venn.layout.venn2 import DefaultLayoutAlgorithm
138 | venn2((1,2,3), layout_algorithm=DefaultLayoutAlgorithm(fixed_subset_sizes=(1,1,1)))
139 |
140 | from matplotlib_venn.layout.venn3 import DefaultLayoutAlgorithm
141 | venn3((7,6,5,4,3,2,1), layout_algorithm=DefaultLayoutAlgorithm(fixed_subset_sizes=(1,1,1,1,1,1,1)))
142 |
143 | In the above examples the diagram regions will be plotted as if `venn2((1,1,1))` and `venn3((1,1,1,1,1,1,1))` were
144 | invoked, yet the actual numbers will be `(1,2,3)` and `(7,6,5,4,3,2,1)` respectively.
145 |
146 | The diagram can be tuned further by switching the layout algorithm to a different implementation.
147 | At the moment the package offers an alternative layout algorithm for `venn3` diagrams that lays the circles out by
148 | optimizing a user-provided *cost function*. The following examples illustrate its usage::
149 |
150 | from matplotlib_venn.layout.venn3 import cost_based
151 | subset_sizes = (100,200,10000,10,20,3,1)
152 | venn3(subset_sizes, layout_algorithm=cost_based.LayoutAlgorithm())
153 |
154 | alg = cost_based.LayoutAlgorithm(cost_fn=cost_based.WeightedAggregateCost(transform_fn=lambda x: x))
155 | venn3(subset_sizes, layout_algorithm=alg)
156 |
157 | alg = cost_based.LayoutAlgorithm(cost_fn=cost_based.WeightedAggregateCost(weights=(0,0,0,1,1,1,1)))
158 | venn3(subset_sizes, layout_algorithm=alg)
159 |
160 | The default "pairwise" algorithm is, theoretically, a special case of the cost-based method with the respective cost function::
161 |
162 | alg = cost_based.LayoutAlgorithm(cost_fn=cost_based.pairwise_cost)
163 | venn3(subset_sizes, layout_algorithm=alg)
164 |
165 | (The latter plot will be close, but not perfectly equal to the outcome of `DefaultLayoutAlgorithm()`).
166 |
167 | Note that the import::
168 |
169 | from matplotlib_venn.layout.venn3 import cost_based
170 |
171 | will fail unless you have the optional `shapely` package installed (see "Installation" above).
172 |
173 |
174 | Questions
175 | ---------
176 |
177 | * If you ask your questions at `StackOverflow `_ and tag them
178 | `matplotlib-venn `_, chances are high you could get
179 | an answer from the maintainer of this package.
180 |
181 | See also
182 | --------
183 |
184 | * Report issues and submit fixes at Github:
185 | https://github.com/konstantint/matplotlib-venn
186 |
187 | Check out the ``DEVELOPER-README.rst`` for development-related notes.
188 | * Some alternative means of plotting a Venn diagram (as of
189 | October 2012) are reviewed in the blog post:
190 | http://fouryears.eu/2012/10/13/venn-diagrams-in-python/
191 | * The `matplotlib-subsets
192 | `_ package
193 | visualizes a hierarchy of sets as a tree of rectangles.
194 | * The `matplotlib_set_diagrams `_ package
195 | is a GPL-licensed alternative that offers a different layout algorithm, which supports more than
196 | three sets and provides a cool ability to incorporate wordclouds into your Venn (Euler) diagrams.
197 |
198 |
--------------------------------------------------------------------------------
/matplotlib_venn/__init__.py:
--------------------------------------------------------------------------------
1 | """
2 | Venn diagram plotting routines.
3 |
4 | Copyright 2012-2024, Konstantin Tretyakov.
5 | http://kt.era.ee/
6 |
7 | Licensed under MIT license.
8 |
9 | This package contains routines for plotting area-weighted two- and three-circle venn diagrams.
10 | There are four main functions here: :code:`venn2`, :code:`venn2_circles`, :code:`venn3`, :code:`venn3_circles`.
11 |
12 | The :code:`venn2` and :code:`venn2_circles` accept as their only required argument a 3-element list of subset sizes:
13 |
14 | subsets = (Ab, aB, AB)
15 |
16 | That is, for example, subsets[0] contains the size of the subset (A and not B), and
17 | subsets[2] contains the size of the set (A and B), etc.
18 |
19 | Similarly, the functions :code:`venn3` and :code:`venn3_circles` require a 7-element list:
20 |
21 | subsets = (Abc, aBc, ABc, abC, AbC, aBC, ABC)
22 |
23 | The functions :code:`venn2_circles` and :code:`venn3_circles` simply draw two or three circles respectively,
24 | such that their intersection areas correspond to the desired set intersection sizes.
25 | Note that for a three-circle Venn diagram it is not possible to achieve exact correspondence, although in
26 | most cases the picture will still provide a decent representation.
27 |
28 | The functions :code:`venn2` and :code:`venn3` draw diagram as a collection of separate colored patches with text labels.
29 |
30 | The functions :code:`venn2_circles` and :code:`venn3_circles` return the list of Circle patches that may be tuned further
31 | to your liking.
32 |
33 | The functions :code:`venn2` and :code:`venn3` return an object of class :code:`VennDiagram` which provides access to
34 | constituent patches and text elements.
35 |
36 | Example::
37 |
38 | from matplotlib import pyplot as plt
39 | import numpy as np
40 | from matplotlib_venn import venn3, venn3_circles
41 | plt.figure(figsize=(4,4))
42 | v = venn3(subsets=(1, 1, 1, 1, 1, 1, 1), set_labels = ('A', 'B', 'C'))
43 | v.get_patch_by_id('100').set_alpha(1.0)
44 | v.get_patch_by_id('100').set_color('white')
45 | v.get_label_by_id('100').set_text('Unknown')
46 | v.get_label_by_id('A').set_text('Set "A"')
47 | c = venn3_circles(subsets=(1, 1, 1, 1, 1, 1, 1), linestyle='dashed')
48 | c[0].set_lw(1.0)
49 | c[0].set_ls('dotted')
50 | plt.title("Sample Venn diagram")
51 | plt.annotate('Unknown set', xy=v.get_label_by_id('100').get_position() - np.array([0, 0.05]), xytext=(-70,-70),
52 | ha='center', textcoords='offset points', bbox=dict(boxstyle='round,pad=0.5', fc='gray', alpha=0.1),
53 | arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0.5',color='gray'))
54 | """
55 |
56 | from matplotlib_venn._venn2 import venn2, venn2_circles
57 | from matplotlib_venn._venn3 import venn3, venn3_circles
58 | from matplotlib_venn._util import venn2_unweighted, venn3_unweighted
59 |
60 | ___all___ = [
61 | "venn2",
62 | "venn2_circles",
63 | "venn3",
64 | "venn3_circles",
65 | "venn2_unweighted",
66 | "venn3_unweighted",
67 | ]
68 | __version__ = "1.1.2"
69 |
--------------------------------------------------------------------------------
/matplotlib_venn/_arc.py:
--------------------------------------------------------------------------------
1 | """
2 | Venn diagram plotting routines.
3 | General-purpose math routines for computing with circular arcs.
4 | Everything is encapsulated in the "Arc" class.
5 |
6 | Copyright 2014-2024, Konstantin Tretyakov.
7 | http://kt.era.ee/
8 |
9 | Licensed under MIT license.
10 | """
11 |
12 | from typing import Optional, Sequence
13 | import math
14 | import numpy as np
15 | from matplotlib_venn._math import (
16 | NUMERIC_TOLERANCE,
17 | Point2DInternal,
18 | circle_circle_intersection,
19 | vector_angle_in_degrees,
20 | )
21 |
22 |
23 | class Arc:
24 | """
25 | A representation of a directed circle arc.
26 | Essentially it is a namedtuple(center, radius, from_angle, to_angle, direction) with a bunch of helper methods
27 | for measuring arc lengths and intersections.
28 |
29 | The from_angle and to_angle of an arc must be represented in degrees.
30 | The direction is a boolean, with True corresponding to counterclockwise (positive) direction, and False - clockwise (negative).
31 | For convenience, the class defines a "sign" property, which is +1 if direction = True and -1 otherwise.
32 | """
33 |
34 | def __init__(
35 | self,
36 | center: Point2DInternal,
37 | radius: float,
38 | from_angle: float,
39 | to_angle: float,
40 | direction: bool,
41 | ):
42 | """Raises a ValueError if radius is negative.
43 |
44 | >>> a = Arc((0, 0), -1, 0, 0, True)
45 | Traceback (most recent call last):
46 | ...
47 | ValueError: Arc's radius may not be negative
48 | >>> a = Arc((0, 0), 0, 0, 0, True)
49 | >>> a = Arc((0, 0), 1, 0, 0, True)
50 | """
51 | self.center = np.asarray(center)
52 | self.radius = float(radius)
53 | if radius < 0.0:
54 | raise ValueError("Arc's radius may not be negative")
55 | self.from_angle = float(from_angle)
56 | self.to_angle = float(to_angle)
57 | self.direction = direction
58 | self.sign = 1 if direction else -1
59 |
60 | def length_degrees(self) -> float:
61 | """Computes the length of the arc in degrees.
62 | The length computation corresponds to what you would expect if you would draw the arc using matplotlib taking direction into account.
63 |
64 | >>> Arc((0,0), 1, 0, 0, True).length_degrees()
65 | 0.0
66 | >>> Arc((0,0), 2, 0, 0, False).length_degrees()
67 | 0.0
68 |
69 | >>> Arc((0,0), 3, 0, 1, True).length_degrees()
70 | 1.0
71 | >>> Arc((0,0), 4, 0, 1, False).length_degrees()
72 | 359.0
73 |
74 | >>> Arc((0,0), 5, 0, 360, True).length_degrees()
75 | 360.0
76 | >>> Arc((0,0), 6, 0, 360, False).length_degrees()
77 | 0.0
78 |
79 | >>> Arc((0,0), 7, 0, 361, True).length_degrees()
80 | 360.0
81 | >>> Arc((0,0), 8, 0, 361, False).length_degrees()
82 | 359.0
83 |
84 | >>> Arc((0,0), 9, 10, -10, True).length_degrees()
85 | 340.0
86 | >>> Arc((0,0), 10, 10, -10, False).length_degrees()
87 | 20.0
88 |
89 | >>> Arc((0,0), 1, 10, 5, True).length_degrees()
90 | 355.0
91 | >>> Arc((0,0), 1, -10, -5, False).length_degrees()
92 | 355.0
93 | >>> Arc((0,0), 1, 180, -180, True).length_degrees()
94 | 0.0
95 | >>> Arc((0,0), 1, 180, -180, False).length_degrees()
96 | 360.0
97 | >>> Arc((0,0), 1, -180, 180, True).length_degrees()
98 | 360.0
99 | >>> Arc((0,0), 1, -180, 180, False).length_degrees()
100 | 0.0
101 | >>> Arc((0,0), 1, 175, -175, True).length_degrees()
102 | 10.0
103 | >>> Arc((0,0), 1, 175, -175, False).length_degrees()
104 | 350.0
105 | """
106 | d_angle = self.sign * (self.to_angle - self.from_angle)
107 | if d_angle > 360:
108 | return 360.0
109 | elif d_angle < 0:
110 | return d_angle % 360.0
111 | else:
112 | return abs(
113 | d_angle
114 | ) # Yes, abs() is needed, otherwise we get the weird "-0.0" output in the doctests
115 |
116 | def length_radians(self) -> float:
117 | """Returns the length of the arc in radians.
118 |
119 | >>> Arc((0,0), 1, 0, 0, True).length_radians()
120 | 0.0
121 | >>> Arc((0,0), 2, 0, 360, True).length_radians()
122 | 6.283...
123 | >>> Arc((0,0), 6, -18, 18, True).length_radians()
124 | 0.6283...
125 | """
126 | return self.length_degrees() * np.pi / 180.0
127 |
128 | def length(self) -> float:
129 | """Returns the actual length of the arc.
130 |
131 | >>> Arc((0,0), 2, 0, 360, True).length()
132 | 12.566...
133 | >>> Arc((0,0), 2, 90, 360, False).length()
134 | 3.1415...
135 | >>> Arc((0,0), 0, 90, 360, True).length()
136 | 0.0
137 | """
138 | return self.radius * self.length_radians()
139 |
140 | def sector_area(self) -> float:
141 | """Returns the area of the corresponding arc sector.
142 |
143 | >>> Arc((0,0), 2, 0, 360, True).sector_area()
144 | 12.566...
145 | >>> Arc((0,0), 2, 0, 36, True).sector_area()
146 | 1.2566...
147 | >>> Arc((0,0), 2, 0, 36, False).sector_area()
148 | 11.3097...
149 | """
150 | return self.radius**2 / 2 * self.length_radians()
151 |
152 | def segment_area(self) -> float:
153 | """Returns the area of the corresponding arc segment.
154 |
155 | >>> Arc((0,0), 2, 0, 360, True).segment_area()
156 | 12.566...
157 | >>> Arc((0,0), 2, 0, 180, True).segment_area()
158 | 6.283...
159 | >>> Arc((0,0), 2, 0, 90, True).segment_area()
160 | 1.14159...
161 | >>> Arc((0,0), 2, 0, 90, False).segment_area()
162 | 11.42477796...
163 | >>> Arc((0,0), 2, 0, 0, False).segment_area()
164 | 0.0
165 | >>> Arc((0, 9), 1, 89.99, 90, False).segment_area()
166 | 3.1415...
167 | """
168 | theta = self.length_radians()
169 | return self.radius**2 / 2 * (theta - math.sin(theta))
170 |
171 | def angle_as_point(self, angle: float) -> np.ndarray:
172 | """
173 | Converts a given angle in degrees to the point coordinates on the arc's circle.
174 | Inverse of point_to_angle.
175 |
176 | >>> Arc((1, 1), 1, 0, 0, True).angle_as_point(0).tolist()
177 | [2.0, 1.0]
178 | >>> Arc((1, 1), 1, 0, 0, True).angle_as_point(90).tolist()
179 | [1.0, 2.0]
180 | >>> bool(np.all(np.isclose(Arc((1, 1), 1, 0, 0, True).angle_as_point(-270), [1.0, 2.0])))
181 | True
182 | """
183 | angle_rad = angle * np.pi / 180.0
184 | return self.center + self.radius * np.array(
185 | [math.cos(angle_rad), math.sin(angle_rad)]
186 | )
187 |
188 | def start_point(self) -> np.ndarray:
189 | """
190 | Returns a 2x1 numpy array with the coordinates of the arc's start point.
191 |
192 | >>> Arc((0, 0), 1, 0, 0, True).start_point().tolist()
193 | [1.0, 0.0]
194 | >>> Arc((0, 0), 1, 45, 0, True).start_point().tolist()
195 | [0.707..., 0.707...]
196 | """
197 | return self.angle_as_point(self.from_angle)
198 |
199 | def end_point(self) -> np.ndarray:
200 | """
201 | Returns a 2x1 numpy array with the coordinates of the arc's end point.
202 |
203 | >>> bool(np.all(Arc((0, 0), 1, 0, 90, True).end_point() - np.array([0, 1]) < NUMERIC_TOLERANCE))
204 | True
205 | """
206 | return self.angle_as_point(self.to_angle)
207 |
208 | def mid_point(self) -> np.ndarray:
209 | """
210 | Returns the midpoint of the arc as a 1x2 numpy array.
211 | """
212 | midpoint_angle = self.from_angle + self.sign * self.length_degrees() / 2
213 | return self.angle_as_point(midpoint_angle)
214 |
215 | def approximately_equal(self, arc: "Arc", tolerance=NUMERIC_TOLERANCE) -> bool:
216 | """
217 | Returns true if the parameters of this arc are within of the parameters of the other arc, and the direction is the same.
218 | Note that no angle simplification is performed (i.e. some arcs that might be equal in principle are not declared as such
219 | by this method)
220 |
221 | >>> tol = NUMERIC_TOLERANCE
222 | >>> Arc((0, 0), 10, 20, 30, True).approximately_equal(Arc((tol/2, tol/2), 10+tol/2, 20-tol/2, 30-tol/2, True))
223 | True
224 | >>> Arc((0, 0), 10, 20, 30, True).approximately_equal(Arc((0, 0), 10, 20, 30, False))
225 | False
226 | >>> Arc((0, 0), 10, 20, 30, True).approximately_equal(Arc((0, 0+tol), 10, 20, 30, True))
227 | False
228 | """
229 | return bool(
230 | self.direction == arc.direction
231 | and np.all(abs(self.center - arc.center) < tolerance)
232 | and abs(self.radius - arc.radius) < tolerance
233 | and abs(self.from_angle - arc.from_angle) < tolerance
234 | and abs(self.to_angle - arc.to_angle) < tolerance
235 | )
236 |
237 | def point_as_angle(self, pt: Point2DInternal) -> float:
238 | """
239 | Given a point located on the arc's circle, return the corresponding angle in degrees.
240 | No check is done that the point lies on the circle
241 | (this is essentially a convenience wrapper around _math.vector_angle_in_degrees)
242 |
243 | >>> a = Arc((0, 0), 1, 0, 0, True)
244 | >>> a.point_as_angle((1, 0))
245 | 0.0
246 | >>> a.point_as_angle((1, 1))
247 | 45.0
248 | >>> a.point_as_angle((0, 1))
249 | 90.0
250 | >>> a.point_as_angle((-1, 1))
251 | 135.0
252 | >>> a.point_as_angle((-1, 0))
253 | 180.0
254 | >>> a.point_as_angle((-1, -1))
255 | -135.0
256 | >>> a.point_as_angle((0, -1))
257 | -90.0
258 | >>> a.point_as_angle((1, -1))
259 | -45.0
260 | """
261 | return vector_angle_in_degrees(np.asarray(pt) - self.center)
262 |
263 | def contains_angle_degrees(self, angle: float) -> float:
264 | """
265 | Returns true, if a point with the corresponding angle (given in degrees) is within the arc.
266 | Does no tolerance checks (i.e. if the arc is of length 0, you must provide angle == from_angle == to_angle to get a positive answer here)
267 |
268 | >>> a = Arc((0, 0), 1, 0, 0, True)
269 | >>> assert a.contains_angle_degrees(0)
270 | >>> assert a.contains_angle_degrees(360)
271 | >>> assert not a.contains_angle_degrees(1)
272 |
273 | >>> a = Arc((0, 0), 1, 170, -170, True)
274 | >>> assert not a.contains_angle_degrees(165)
275 | >>> assert a.contains_angle_degrees(170)
276 | >>> assert a.contains_angle_degrees(175)
277 | >>> assert a.contains_angle_degrees(180)
278 | >>> assert a.contains_angle_degrees(185)
279 | >>> assert a.contains_angle_degrees(190)
280 | >>> assert not a.contains_angle_degrees(195)
281 |
282 | >>> assert not a.contains_angle_degrees(-195)
283 | >>> assert a.contains_angle_degrees(-190)
284 | >>> assert a.contains_angle_degrees(-185)
285 | >>> assert a.contains_angle_degrees(-180)
286 | >>> assert a.contains_angle_degrees(-175)
287 | >>> assert a.contains_angle_degrees(-170)
288 | >>> assert not a.contains_angle_degrees(-165)
289 | >>> assert a.contains_angle_degrees(-170 - 360)
290 | >>> assert a.contains_angle_degrees(-190 - 360)
291 | >>> assert a.contains_angle_degrees(170 + 360)
292 | >>> assert not a.contains_angle_degrees(0)
293 | >>> assert not a.contains_angle_degrees(100)
294 | >>> assert not a.contains_angle_degrees(-100)
295 | """
296 | _d = self.sign * (angle - self.from_angle) % 360.0
297 | return _d <= self.length_degrees()
298 |
299 | def intersect_circle(
300 | self, center: Point2DInternal, radius: float
301 | ) -> Sequence[np.ndarray]:
302 | """
303 | Given a circle, finds the intersection point(s) of the arc with the circle.
304 | Returns a list of 2x1 numpy arrays. The list has length 0, 1 or 2, depending on how many intesection points there are.
305 | If the circle touches the arc, it is reported as two intersection points (which are equal).
306 | Points are ordered along the arc.
307 | Intersection with the same circle as the arc's own (which means infinitely many points usually) is reported as no intersection at all.
308 |
309 | >>> a = Arc((0, 0), 1, -60, 60, True)
310 | >>> str(a.intersect_circle((1, 0), 1)).replace(' ', '')
311 | '[array([0.5...,-0.866...]),array([0.5...,0.866...])]'
312 | >>> a.intersect_circle((0.9, 0), 1)
313 | []
314 | >>> str(a.intersect_circle((1,-0.1), 1)).replace(' ', '')
315 | '[array([0.586...,0.810...])]'
316 | >>> str(a.intersect_circle((1, 0.1), 1)).replace(' ', '')
317 | '[array([0.586...,-0.810...])]'
318 | >>> a.intersect_circle((0, 0), 1) # Infinitely many intersection points
319 | []
320 | >>> str(a.intersect_circle((2, 0), 1)).replace(' ', '') # Touching point, hence repeated twice
321 | '[array([1.,0.]),array([1.,0.])]'
322 |
323 | >>> a = Arc((0, 0), 1, 60, -60, False) # Same arc, different direction
324 | >>> str(a.intersect_circle((1, 0), 1)).replace(' ', '')
325 | '[array([0.5...,0.866...]),array([0.5...,-0.866...])]'
326 |
327 | >>> a = Arc((0, 0), 1, 120, -120, True)
328 | >>> a.intersect_circle((-1, 0), 1)
329 | [array([-0.5..., 0.866...]), array([-0.5..., -0.866...])]
330 | >>> a.intersect_circle((-0.9, 0), 1)
331 | []
332 | >>> a.intersect_circle((-1,-0.1), 1)
333 | [array([-0.586..., 0.810...])]
334 | >>> a.intersect_circle((-1, 0.1), 1)
335 | [array([-0.586..., -0.810...])]
336 | >>> a.intersect_circle((-2, 0), 1)
337 | [array([-1., 0.]), array([-1., 0.])]
338 | >>> a = Arc((0, 0), 1, -120, 120, False)
339 | >>> a.intersect_circle((-1, 0), 1)
340 | [array([-0.5..., -0.866...]), array([-0.5..., 0.866...])]
341 | """
342 | intersections = circle_circle_intersection(
343 | self.center, self.radius, center, radius
344 | )
345 | if intersections is None:
346 | return []
347 |
348 | # Check whether the points lie on the arc and order them accordingly
349 | _len = self.length_degrees()
350 | isections = [
351 | [self.sign * (self.point_as_angle(pt) - self.from_angle) % 360.0, pt]
352 | for pt in intersections
353 | ]
354 |
355 | # Try to find as many candidate intersections as possible (i.e. +- tol within arc limits)
356 | # Unless arc's length is 360, interpret intersections just before the arc's starting point as belonging to the starting point.
357 | if _len < 360.0 - NUMERIC_TOLERANCE:
358 | for isec in isections:
359 | if isec[0] > 360.0 - NUMERIC_TOLERANCE:
360 | isec[0] = 0.0
361 |
362 | isections = [
363 | (a, pt[0], pt[1])
364 | for (a, pt) in isections
365 | if a < _len + NUMERIC_TOLERANCE or a > 360 - NUMERIC_TOLERANCE
366 | ]
367 | isections.sort()
368 | return [np.array([b, c]) for (a, b, c) in isections]
369 |
370 | def intersect_arc(self, arc: "Arc") -> Sequence[np.ndarray]:
371 | """
372 | Given an arc, finds the intersection point(s) of this arc with that.
373 | Returns a list of 2x1 numpy arrays. The list has length 0, 1 or 2, depending on how many intesection points there are.
374 | Points are ordered along the arc.
375 | Intersection with the arc along the same circle (which means infinitely many points usually) is reported as no intersection at all.
376 |
377 | >>> a = Arc((0, 0), 1, -90, 90, True)
378 | >>> str(a.intersect_arc(Arc((1, 0), 1, 90, 270, True))).replace(' ', '')
379 | '[array([0.5,-0.866...]),array([0.5,0.866...])]'
380 | >>> str(a.intersect_arc(Arc((1, 0), 1, 90, 180, True))).replace(' ', '')
381 | '[array([0.5,0.866...])]'
382 | >>> a.intersect_arc(Arc((1, 0), 1, 121, 239, True))
383 | []
384 | >>> tol = NUMERIC_TOLERANCE
385 | >>> str(a.intersect_arc(Arc((1, 0), 1, 120-tol, 240+tol, True))).replace(' ', '') # Without -tol and +tol the results differ on different architectures due to rounding (see Debian #813782).
386 | '[array([0.5,-0.866...]),array([0.5,0.866...])]'
387 | """
388 | intersections = self.intersect_circle(arc.center, arc.radius)
389 | isections = [
390 | pt
391 | for pt in intersections
392 | if arc.contains_angle_degrees(arc.point_as_angle(pt))
393 | ]
394 | return isections
395 |
396 | def subarc(
397 | self, from_angle: Optional[float] = None, to_angle: Optional[float] = None
398 | ) -> "Arc":
399 | """
400 | Creates a sub-arc from a given angle (or beginning of this arc) to a given angle (or end of this arc).
401 | Verifies that from_angle and to_angle are within the arc and properly ordered.
402 | If from_angle is None, start of this arc is used instead.
403 | If to_angle is None, end of this arc is used instead.
404 | Angles are given in degrees.
405 |
406 | >>> a = Arc((0, 0), 1, 0, 360, True)
407 | >>> a.subarc(None, None)
408 | Arc([0.000, 0.000], 1.000, 0.000, 360.000, True, degrees=360.000)
409 | >>> a.subarc(360, None)
410 | Arc([0.000, 0.000], 1.000, 360.000, 360.000, True, degrees=0.000)
411 | >>> a.subarc(0, None)
412 | Arc([0.000, 0.000], 1.000, 0.000, 360.000, True, degrees=360.000)
413 | >>> a.subarc(-10, None)
414 | Arc([0.000, 0.000], 1.000, 350.000, 360.000, True, degrees=10.000)
415 | >>> a.subarc(None, -10)
416 | Arc([0.000, 0.000], 1.000, 0.000, 350.000, True, degrees=350.000)
417 | >>> a.subarc(1, 359).subarc(2, 358).subarc()
418 | Arc([0.000, 0.000], 1.000, 2.000, 358.000, True, degrees=356.000)
419 | """
420 |
421 | if from_angle is None:
422 | from_angle = self.from_angle
423 | if to_angle is None:
424 | to_angle = self.to_angle
425 | cur_length = self.length_degrees()
426 | d_new_from = self.sign * (from_angle - self.from_angle)
427 | if d_new_from != 360.0:
428 | d_new_from = d_new_from % 360.0
429 | d_new_to = self.sign * (to_angle - self.from_angle)
430 | if d_new_to != 360.0:
431 | d_new_to = d_new_to % 360.0
432 | # Gracefully handle numeric precision issues for zero-length arcs
433 | if abs(d_new_from - d_new_to) < NUMERIC_TOLERANCE:
434 | d_new_from = d_new_to
435 | if d_new_to < d_new_from:
436 | raise ValueError("Subarc to-angle must be smaller than from-angle.")
437 | if d_new_to > cur_length + NUMERIC_TOLERANCE:
438 | raise ValueError("Subarc to-angle must lie within the current arc.")
439 | return Arc(
440 | self.center,
441 | self.radius,
442 | self.from_angle + self.sign * d_new_from,
443 | self.from_angle + self.sign * d_new_to,
444 | self.direction,
445 | )
446 |
447 | def subarc_between_points(
448 | self,
449 | p_from: Optional[Point2DInternal] = None,
450 | p_to: Optional[Point2DInternal] = None,
451 | ) -> "Arc":
452 | """
453 | Given two points on the arc, extract a sub-arc between those points.
454 | No check is made to verify the points are actually on the arc.
455 | It is basically a wrapper around subarc(point_as_angle(p_from), point_as_angle(p_to)).
456 | Either p_from or p_to may be None to denote first or last arc endpoints.
457 |
458 | >>> a = Arc((0, 0), 1, 0, 90, True)
459 | >>> a.subarc_between_points((1, 0), (math.cos(np.pi/4), math.sin(np.pi/4)))
460 | Arc([0.000, 0.000], 1.000, 0.000, 45.000, True, degrees=45.000)
461 | >>> a.subarc_between_points(None, None)
462 | Arc([0.000, 0.000], 1.000, 0.000, 90.000, True, degrees=90.000)
463 | >>> a.subarc_between_points((math.cos(np.pi/4), math.sin(np.pi/4)))
464 | Arc([0.000, 0.000], 1.000, 45.000, 90.000, True, degrees=45.000)
465 | """
466 | a_from = self.point_as_angle(p_from) if p_from is not None else None
467 | a_to = self.point_as_angle(p_to) if p_to is not None else None
468 | return self.subarc(a_from, a_to)
469 |
470 | def reversed(self) -> "Arc":
471 | """
472 | Returns a copy of this arc, with the direction flipped.
473 |
474 | >>> Arc((0, 0), 1, 0, 360, True).reversed()
475 | Arc([0.000, 0.000], 1.000, 360.000, 0.000, False, degrees=360.000)
476 | >>> Arc((0, 0), 1, 175, -175, True).reversed()
477 | Arc([0.000, 0.000], 1.000, -175.000, 175.000, False, degrees=10.000)
478 | >>> Arc((0, 0), 1, 0, 370, True).reversed()
479 | Arc([0.000, 0.000], 1.000, 370.000, 0.000, False, degrees=360.000)
480 | """
481 | return Arc(
482 | self.center, self.radius, self.to_angle, self.from_angle, not self.direction
483 | )
484 |
485 | def direction_vector(self, angle: float) -> np.ndarray:
486 | """
487 | Returns a unit vector, pointing in the arc's movement direction at a given (absolute) angle (in degrees).
488 | No check is made whether angle lies within the arc's span (the results for angles outside of the arc's span )
489 | Returns a 2x1 numpy array.
490 |
491 | >>> a = Arc((0, 0), 1, 0, 90, True)
492 | >>> tol = NUMERIC_TOLERANCE
493 | >>> assert all(abs(a.direction_vector(0) - np.array([0.0, 1.0])) < tol)
494 | >>> assert all(abs(a.direction_vector(45) - np.array([ -0.70710678, 0.70710678])) < 1e-6)
495 | >>> assert all(abs(a.direction_vector(90) - np.array([-1.0, 0.0])) < tol)
496 | >>> assert all(abs(a.direction_vector(135) - np.array([-0.70710678, -0.70710678])) < 1e-6)
497 | >>> assert all(abs(a.direction_vector(-180) - np.array([0.0, -1.0])) < tol)
498 | >>> assert all(abs(a.direction_vector(-90) - np.array([1.0, 0.0])) < tol)
499 | >>> a = a.reversed()
500 | >>> assert all(abs(a.direction_vector(0) - np.array([0.0, -1.0])) < tol)
501 | >>> assert all(abs(a.direction_vector(45) - np.array([ 0.70710678, -0.70710678])) < 1e-6)
502 | >>> assert all(abs(a.direction_vector(90) - np.array([1.0, 0.0])) < tol)
503 | >>> assert all(abs(a.direction_vector(135) - np.array([0.70710678, 0.70710678])) < 1e-6)
504 | >>> assert all(abs(a.direction_vector(-180) - np.array([0.0, 1.0])) < tol)
505 | >>> assert all(abs(a.direction_vector(-90) - np.array([-1.0, 0.0])) < tol)
506 | """
507 | a = angle + self.sign * 90
508 | a = a * np.pi / 180.0
509 | return np.array([math.cos(a), math.sin(a)])
510 |
511 | def fix_360_to_0(self) -> None:
512 | """
513 | Sometimes we have to create an arc using from_angle and to_angle computed numerically.
514 | If from_angle == to_angle, it may sometimes happen that a tiny discrepancy will make from_angle > to_angle, and instead of
515 | getting a 0-length arc we end up with a 360-degree arc.
516 | Sometimes we know for sure that a 360-degree arc is not what we want, and in those cases
517 | the problem is easy to fix. This helper method does that. It checks whether from_angle and to_angle are numerically similar,
518 | and if so makes them equal.
519 |
520 | >>> a = Arc((0, 0), 1, 0, -NUMERIC_TOLERANCE/2, True)
521 | >>> a
522 | Arc([0.000, 0.000], 1.000, 0.000, -0.000, True, degrees=360.000)
523 | >>> a.fix_360_to_0()
524 | >>> a
525 | Arc([0.000, 0.000], 1.000, -0.000, -0.000, True, degrees=0.000)
526 | """
527 | if abs(self.from_angle - self.to_angle) < NUMERIC_TOLERANCE:
528 | self.from_angle = self.to_angle
529 |
530 | def lies_on_circle(self, center: Point2DInternal, radius: float) -> bool:
531 | """Tests whether the arc circle's center and radius match the given ones within tolerance.
532 |
533 | >>> a = Arc((0, 0), 1, 0, 0, False)
534 | >>> tol = NUMERIC_TOLERANCE
535 | >>> a.lies_on_circle((tol/2, tol/2), 1+tol/2)
536 | True
537 | >>> a.lies_on_circle((tol/2, tol/2), 1-tol)
538 | False
539 | """
540 | return (
541 | np.all(abs(np.asarray(center) - self.center) < NUMERIC_TOLERANCE)
542 | and abs(radius - self.radius) < NUMERIC_TOLERANCE
543 | )
544 |
545 | def __repr__(self) -> str:
546 | return "Arc([%0.3f, %0.3f], %0.3f, %0.3f, %0.3f, %s, degrees=%0.3f)" % (
547 | self.center[0],
548 | self.center[1],
549 | self.radius,
550 | self.from_angle,
551 | self.to_angle,
552 | self.direction,
553 | self.length_degrees(),
554 | )
555 |
--------------------------------------------------------------------------------
/matplotlib_venn/_common.py:
--------------------------------------------------------------------------------
1 | """
2 | Venn diagram plotting routines.
3 | Functionality, common to venn2 and venn3.
4 |
5 | Copyright 2012-2024, Konstantin Tretyakov.
6 | http://kt.era.ee/
7 |
8 | Licensed under MIT license.
9 | """
10 |
11 | from typing import Optional, Sequence
12 | import numpy as np
13 | from matplotlib.axes import Axes
14 | from matplotlib.patches import Patch
15 | from matplotlib.text import Text
16 |
17 | from matplotlib_venn._math import Point2D
18 |
19 |
20 | class VennDiagram:
21 | """
22 | A container for a set of patches and patch labels and set labels, which make up the rendered venn diagram.
23 | This object is returned by a venn2 or venn3 function call.
24 | """
25 |
26 | id2idx = {
27 | "10": 0,
28 | "01": 1,
29 | "11": 2,
30 | "100": 0,
31 | "010": 1,
32 | "110": 2,
33 | "001": 3,
34 | "101": 4,
35 | "011": 5,
36 | "111": 6,
37 | "A": 0,
38 | "B": 1,
39 | "C": 2,
40 | }
41 |
42 | def __init__(
43 | self,
44 | patches: Sequence[Patch],
45 | subset_labels: Sequence[Text],
46 | set_labels: Sequence[Text],
47 | centers: Sequence[Point2D],
48 | radii: Sequence[float],
49 | ):
50 | self.patches = patches
51 | self.subset_labels = subset_labels
52 | self.set_labels = set_labels
53 | self.centers = centers
54 | self.radii = radii
55 |
56 | def get_patch_by_id(self, id: str) -> Patch:
57 | """Returns a patch by a "region id".
58 | A region id is a string '10', '01' or '11' for 2-circle diagram or a
59 | string like '001', '010', etc, for 3-circle diagram."""
60 | return self.patches[self.id2idx[id]]
61 |
62 | def get_label_by_id(self, id: str) -> Text:
63 | """
64 | Returns a subset label by a "region id".
65 | A region id is a string '10', '01' or '11' for 2-circle diagram or a
66 | string like '001', '010', etc, for 3-circle diagram.
67 | Alternatively, if the string 'A', 'B' (or 'C' for 3-circle diagram) is given, the label of the
68 | corresponding set is returned (or None)."""
69 | if len(id) == 1:
70 | return (
71 | self.set_labels[self.id2idx[id]]
72 | if self.set_labels is not None
73 | else None
74 | )
75 | else:
76 | return self.subset_labels[self.id2idx[id]]
77 |
78 | def get_circle_center(self, id: int) -> Point2D:
79 | """
80 | Returns the coordinates of the center of a circle as a numpy array (x,y)
81 | id must be 0, 1 or 2 (corresponding to the first, second, or third circle).
82 | This is a getter-only (i.e. changing this value does not affect the diagram)
83 | """
84 | return self.centers[id]
85 |
86 | def get_circle_radius(self, id: int) -> float:
87 | """
88 | Returns the radius of circle id (where id is 0, 1 or 2).
89 | This is a getter-only (i.e. changing this value does not affect the diagram)
90 | """
91 | return self.radii[id]
92 |
93 | def hide_zeroes(self) -> None:
94 | """
95 | Sometimes it makes sense to hide the labels for subsets whose size is zero.
96 | This utility method does this.
97 | """
98 | for v in self.subset_labels:
99 | if v is not None and v.get_text() == "0":
100 | v.set_visible(False)
101 |
102 |
103 | def mix_colors(
104 | col1: np.ndarray, col2: np.ndarray, col3: Optional[np.ndarray] = None
105 | ) -> np.ndarray:
106 | """
107 | Mixes two or three colors to compute a "mixed" color (for purposes of computing
108 | colors of the intersection regions based on the colors of the sets.
109 | Note that we do not simply compute averages of given colors as those seem
110 | too dark for some default configurations. Thus, we lighten the combination up a bit.
111 |
112 | Inputs are (up to) three RGB triples of floats 0.0-1.0 given as numpy arrays.
113 |
114 | >>> mix_colors(np.array([1.0, 0., 0.]), np.array([1.0, 0., 0.])).tolist()
115 | [1.0, 0.0, 0.0]
116 | >>> np.round(mix_colors(np.array([1.0, 1., 0.]), np.array([1.0, 0.9, 0.]), np.array([1.0, 0.8, 0.1])), 3).tolist()
117 | [1.0, 1.0, 0.04]
118 | """
119 | if col3 is None:
120 | mix_color = 0.7 * (col1 + col2)
121 | else:
122 | mix_color = 0.4 * (col1 + col2 + col3)
123 | mix_color = np.min([mix_color, [1.0, 1.0, 1.0]], 0)
124 | return mix_color
125 |
126 |
127 | def prepare_venn_axes(
128 | ax: Axes, centers: Sequence[Point2D], radii: Sequence[float]
129 | ) -> None:
130 | """
131 | Sets properties of the axis object to suit venn plotting. I.e. hides ticks, makes proper xlim/ylim.
132 | """
133 | ax.set_aspect("equal")
134 | ax.set_xticks([])
135 | ax.set_yticks([])
136 | min_x = min(c.x - r for (c, r) in zip(centers, radii))
137 | max_x = max(c.x + r for (c, r) in zip(centers, radii))
138 | min_y = min(c.y - r for (c, r) in zip(centers, radii))
139 | max_y = max(c.y + r for (c, r) in zip(centers, radii))
140 | ax.set_xlim([min_x - 0.1, max_x + 0.1])
141 | ax.set_ylim([min_y - 0.1, max_y + 0.1])
142 | ax.set_axis_off()
143 |
--------------------------------------------------------------------------------
/matplotlib_venn/_math.py:
--------------------------------------------------------------------------------
1 | """
2 | Venn diagram plotting routines.
3 | Math helper functions.
4 |
5 | Copyright 2012, Konstantin Tretyakov.
6 | http://kt.era.ee/
7 |
8 | Licensed under MIT license.
9 | """
10 |
11 | from typing import Optional, Sequence, Union
12 | from scipy.optimize import brentq
13 | import math
14 | import numpy as np
15 |
16 |
17 | class Point2D:
18 | """A simple representation of a 2D point.
19 |
20 | Note that the methods below use raw 2D np.arrays to represent points.
21 | This is due to the original desire to keep things simple and assumption that those
22 | are all internal.
23 | Since version 1.0.0 we expose layout logic for external interfacing and
24 | would rather use semantically meaningful structures.
25 | Maybe this should move over to a module without an underscore in the name.
26 | """
27 |
28 | __slots__ = ("x", "y")
29 |
30 | def __init__(self, x: float, y: float):
31 | self.x = x
32 | self.y = y
33 |
34 | def asarray(self):
35 | return np.array([self.x, self.y], float)
36 |
37 | def __add__(self, other: "Point2D") -> "Point2D":
38 | return Point2D(self.x + other.x, self.y + other.y)
39 |
40 | def __repr__(self):
41 | return "Point2D({}, {})".format(self.x, self.y)
42 |
43 |
44 | Point2DInternal = Union[Sequence[float], np.ndarray]
45 |
46 | NUMERIC_TOLERANCE = 1e-10
47 |
48 |
49 | def point_in_circle(
50 | pt: Point2DInternal, center: Point2DInternal, radius: float
51 | ) -> bool:
52 | """
53 | Returns true if a given point is located inside (or on the border) of a circle.
54 |
55 | >>> point_in_circle((0, 0), (0, 0), 1)
56 | True
57 | >>> point_in_circle((1, 0), (0, 0), 1)
58 | True
59 | >>> point_in_circle((1, 1), (0, 0), 1)
60 | False
61 | """
62 | d = np.linalg.norm(np.asarray(pt) - np.asarray(center))
63 | return bool(d <= radius)
64 |
65 |
66 | def box_product(v1: Point2DInternal, v2: Point2DInternal) -> float:
67 | """Returns a determinant |v1 v2|. The value is equal to the signed area of a parallelogram built on v1 and v2.
68 | The value is positive is v2 is to the left of v1.
69 |
70 | >>> box_product((0.0, 1.0), (0.0, 1.0))
71 | 0.0
72 | >>> box_product((1.0, 0.0), (0.0, 1.0))
73 | 1.0
74 | >>> box_product((0.0, 1.0), (1.0, 0.0))
75 | -1.0
76 | """
77 | return v1[0] * v2[1] - v1[1] * v2[0]
78 |
79 |
80 | def circle_intersection_area(r: float, R: float, d: float) -> float:
81 | """
82 | Formula from: http://mathworld.wolfram.com/Circle-CircleIntersection.html
83 | Does not make sense for negative r, R or d
84 |
85 | >>> circle_intersection_area(0.0, 0.0, 0.0)
86 | 0.0
87 | >>> circle_intersection_area(1.0, 1.0, 0.0)
88 | 3.1415...
89 | >>> circle_intersection_area(1.0, 1.0, 1.0)
90 | 1.2283...
91 | """
92 | if abs(d) < NUMERIC_TOLERANCE:
93 | minR = min(r, R)
94 | return np.pi * minR**2
95 | if abs(r - 0) < NUMERIC_TOLERANCE or abs(R - 0) < NUMERIC_TOLERANCE:
96 | return 0.0
97 | d2, r2, R2 = float(d**2), float(r**2), float(R**2)
98 | arg = (d2 + r2 - R2) / 2 / d / r
99 | # Even with valid arguments, the above computation may result in things like -1.001
100 | arg = max(min(arg, 1.0), -1.0)
101 | A = r2 * math.acos(arg)
102 | arg = (d2 + R2 - r2) / 2 / d / R
103 | arg = max(min(arg, 1.0), -1.0)
104 | B = R2 * math.acos(arg)
105 | arg = (-d + r + R) * (d + r - R) * (d - r + R) * (d + r + R)
106 | arg = max(arg, 0)
107 | C = -0.5 * math.sqrt(arg)
108 | return A + B + C
109 |
110 |
111 | def circle_line_intersection(
112 | center: Point2DInternal, r: float, a: Point2DInternal, b: Point2DInternal
113 | ) -> Optional[np.ndarray]:
114 | """
115 | Computes two intersection points between the circle centered at and radius and a line given by two points a and b.
116 | If no intersection exists, or if a==b, None is returned. If one intersection exists, it is repeated in the answer.
117 |
118 | >>> circle_line_intersection(np.array([0.0, 0.0]), 1, np.array([-1.0, 0.0]), np.array([1.0, 0.0]))
119 | array([[ 1., 0.],
120 | [-1., 0.]])
121 | >>> abs(np.round(circle_line_intersection(np.array([1.0, 1.0]), np.sqrt(2), np.array([-1.0, 1.0]), np.array([1.0, -1.0])), 6)).tolist()
122 | [[0.0, 0.0], [0.0, 0.0]]
123 | """
124 | s = b - a
125 | # Quadratic eqn coefs
126 | A = np.linalg.norm(s) ** 2
127 | if abs(A) < NUMERIC_TOLERANCE:
128 | return None
129 | B = 2 * np.dot(a - center, s)
130 | C = np.linalg.norm(a - center) ** 2 - r**2
131 | disc = B**2 - 4 * A * C
132 | if disc < 0.0:
133 | return None
134 | t1 = (-B + math.sqrt(disc)) / 2.0 / A
135 | t2 = (-B - math.sqrt(disc)) / 2.0 / A
136 | return np.array([a + t1 * s, a + t2 * s])
137 |
138 |
139 | def find_distance_by_area(
140 | r: float, R: float, a: float, numeric_correction: float = 0.0001
141 | ) -> float:
142 | """
143 | Solves circle_intersection_area(r, R, d) == a for d numerically (analytical solution seems to be too ugly to pursue).
144 | Assumes that a < pi * min(r, R)**2, will fail otherwise.
145 |
146 | The numeric correction parameter is used whenever the computed distance is exactly (R - r) (i.e. one circle must be inside another).
147 | In this case the result returned is (R-r+correction). This helps later when we position the circles and need to ensure they intersect.
148 |
149 | >>> find_distance_by_area(1, 1, 0, 0.0)
150 | 2.0
151 | >>> round(find_distance_by_area(1, 1, 3.1415, 0.0), 4)
152 | 0.0
153 | >>> d = find_distance_by_area(2, 3, 4, 0.0)
154 | >>> d
155 | 3.37...
156 | >>> round(circle_intersection_area(2, 3, d), 10)
157 | 4.0
158 | >>> find_distance_by_area(1, 2, np.pi)
159 | 1.0001
160 | """
161 | if r > R:
162 | r, R = R, r
163 | if abs(a) < NUMERIC_TOLERANCE:
164 | return float(r + R)
165 | if abs(min(r, R) ** 2 * np.pi - a) < NUMERIC_TOLERANCE:
166 | return abs(R - r + numeric_correction)
167 | return brentq(lambda x: circle_intersection_area(r, R, x) - a, R - r, R + r)
168 |
169 |
170 | def circle_circle_intersection(
171 | C_a: Point2DInternal, r_a: float, C_b: Point2DInternal, r_b: float
172 | ) -> Optional[np.ndarray]:
173 | """
174 | Finds the coordinates of the intersection points of two circles A and B.
175 | Circle center coordinates C_a and C_b, should be given as tuples (or 1x2 arrays).
176 | Returns a 2x2 array result with result[0] being the first intersection point (to the right of the vector C_a -> C_b)
177 | and result[1] being the second intersection point.
178 |
179 | If there is a single intersection point, it is repeated in output.
180 | If there are no intersection points or an infinite number of those, None is returned.
181 |
182 | >>> circle_circle_intersection([0, 0], 1, [1, 0], 1) # Two intersection points
183 | array([[ 0.5 , -0.866...],
184 | [ 0.5 , 0.866...]])
185 | >>> circle_circle_intersection([0, 0], 1, [2, 0], 1).tolist() # Single intersection point (circles touch from outside)
186 | [[1.0, 0.0], [1.0, 0.0]]
187 | >>> circle_circle_intersection([0, 0], 1, [0.5, 0], 0.5).tolist() # Single intersection point (circles touch from inside)
188 | [[1.0, 0.0], [1.0, 0.0]]
189 | >>> circle_circle_intersection([0, 0], 1, [0, 0], 1) is None # Infinite number of intersections (circles coincide)
190 | True
191 | >>> circle_circle_intersection([0, 0], 1, [0, 0.1], 0.8) is None # No intersections (one circle inside another)
192 | True
193 | >>> circle_circle_intersection([0, 0], 1, [2.1, 0], 1) is None # No intersections (one circle outside another)
194 | True
195 | """
196 | C_a, C_b = np.asarray(C_a, float), np.asarray(C_b, float)
197 | v_ab = C_b - C_a
198 | d_ab = np.linalg.norm(v_ab)
199 | if (
200 | np.abs(d_ab) < NUMERIC_TOLERANCE
201 | ): # No intersection points or infinitely many of them (circle centers coincide)
202 | return None
203 | cos_gamma = (d_ab**2 + r_a**2 - r_b**2) / 2.0 / d_ab / r_a
204 |
205 | if (
206 | abs(cos_gamma) > 1.0 + NUMERIC_TOLERANCE / 10
207 | ): # Allow for a tiny numeric tolerance here too (always better to be return something instead of None, if possible)
208 | return None # No intersection point (circles do not touch)
209 | if cos_gamma > 1.0:
210 | cos_gamma = 1.0
211 | if cos_gamma < -1.0:
212 | cos_gamma = -1.0
213 |
214 | sin_gamma = math.sqrt(1 - cos_gamma**2)
215 | u = v_ab / d_ab
216 | v = np.array([-u[1], u[0]])
217 | pt1 = C_a + r_a * cos_gamma * u - r_a * sin_gamma * v
218 | pt2 = C_a + r_a * cos_gamma * u + r_a * sin_gamma * v
219 | return np.array([pt1, pt2])
220 |
221 |
222 | def vector_angle_in_degrees(v: Point2DInternal) -> float:
223 | """
224 | Given a vector, returns its elevation angle in degrees (-180..180).
225 |
226 | >>> vector_angle_in_degrees([1, 0])
227 | 0.0
228 | >>> vector_angle_in_degrees([1, 1])
229 | 45.0
230 | >>> vector_angle_in_degrees([0, 1])
231 | 90.0
232 | >>> vector_angle_in_degrees([-1, 1])
233 | 135.0
234 | >>> vector_angle_in_degrees([-1, 0])
235 | 180.0
236 | >>> vector_angle_in_degrees([-1, -1])
237 | -135.0
238 | >>> vector_angle_in_degrees([0, -1])
239 | -90.0
240 | >>> vector_angle_in_degrees([1, -1])
241 | -45.0
242 | """
243 | return math.atan2(v[1], v[0]) * 180 / np.pi
244 |
245 |
246 | def normalize_by_center_of_mass(coords: np.ndarray, radii: np.ndarray) -> np.ndarray:
247 | """
248 | Given coordinates of circle centers and radii, as two arrays,
249 | returns new coordinates array, computed such that the center of mass of the
250 | three circles is (0, 0).
251 |
252 | >>> normalize_by_center_of_mass(np.array([[0.0, 0.0], [2.0, 0.0], [1.0, 3.0]]), np.array([1.0, 1.0, 1.0]))
253 | array([[-1., -1.],
254 | [ 1., -1.],
255 | [ 0., 2.]])
256 | >>> normalize_by_center_of_mass(np.array([[0.0, 0.0], [2.0, 0.0], [1.0, 2.0]]), np.array([1.0, 1.0, np.sqrt(2.0)]))
257 | array([[-1., -1.],
258 | [ 1., -1.],
259 | [ 0., 1.]])
260 | """
261 | # Now find the center of mass.
262 | radii = radii**2
263 | sum_r = np.sum(radii)
264 | if sum_r < NUMERIC_TOLERANCE:
265 | return coords
266 | else:
267 | return coords - np.dot(radii, coords) / sum_r
268 |
--------------------------------------------------------------------------------
/matplotlib_venn/_region.py:
--------------------------------------------------------------------------------
1 | """
2 | Venn diagram plotting routines.
3 | Math for computing with venn diagram regions.
4 |
5 | Copyright 2014-2024, Konstantin Tretyakov.
6 | http://kt.era.ee/
7 |
8 | Licensed under MIT license.
9 |
10 | The current logic of drawing the venn diagram is the following:
11 | - Position the circles.
12 | - Compute the regions of the diagram based on circles
13 | - Compute the position of the label within each region.
14 | - Create matplotlib PathPatch or Circle objects for each of the regions.
15 |
16 | This module contains functionality necessary for the second, third and fourth steps of this process.
17 |
18 | Note that the regions of an up to 3-circle Venn diagram may be of the following kinds:
19 | - No region
20 | - A circle
21 | - A 2, 3 or 4-arc "poly-arc-gon". (I.e. a polygon with up to 4 vertices, that are connected by circle arcs)
22 | - A set of two 3-arc-gons.
23 |
24 | We create each of the regions by starting with a circle, and then either intersecting or subtracting the second and the third circles.
25 | The classes below implement the region representation, the intersection/subtraction procedures and the conversion to matplotlib patches.
26 | In addition, each region type has a "label positioning" procedure assigned.
27 | """
28 |
29 | from typing import Optional, Sequence, Tuple
30 | import warnings
31 | import numpy as np
32 | from matplotlib.patches import Patch, Circle, PathPatch, Path
33 | from matplotlib.path import Path
34 | from matplotlib_venn._math import (
35 | Point2DInternal,
36 | NUMERIC_TOLERANCE,
37 | circle_circle_intersection,
38 | vector_angle_in_degrees,
39 | )
40 | from matplotlib_venn._math import point_in_circle, box_product
41 | from matplotlib_venn._arc import Arc
42 |
43 |
44 | class VennRegionException(Exception):
45 | pass
46 |
47 |
48 | class VennRegion:
49 | """
50 | This is a superclass of a Venn diagram region, defining the interface that has to be supported by the different region types.
51 | """
52 |
53 | def subtract_and_intersect_circle(
54 | self, center: Point2DInternal, radius: float
55 | ) -> Tuple["VennRegion", "VennRegion"]:
56 | """
57 | Given a circular region, compute two new regions:
58 | one obtained by subtracting the circle from this region, and another obtained by intersecting the circle with the region.
59 |
60 | In all implementations it is assumed that the circle to be subtracted is not completely within
61 | the current region without touching its borders, i.e. it will not form a "hole" when subtracted.
62 |
63 | Arguments:
64 | center (tuple): A two-element tuple-like, representing the coordinates of the center of the circle.
65 | radius (float): A nonnegative number, the radius of the circle.
66 |
67 | Returns:
68 | a tuple with two elements - the result of subtracting the circle, and the result of intersecting with the circle.
69 | """
70 | raise NotImplementedError("Method not implemented")
71 |
72 | def label_position(self) -> Optional[np.ndarray]:
73 | """Compute the position of a label for this region and return it as a 1x2 numpy array (x, y).
74 | May return None if label is not applicable."""
75 | raise NotImplementedError("Method not implemented")
76 |
77 | def size(self) -> float:
78 | """Return a number, representing the size of the region. It is not important that the number would be a precise
79 | measurement, as long as sizes of various regions can be compared to choose the largest one.
80 | """
81 | raise NotImplementedError("Method not implemented")
82 |
83 | def make_patch(self) -> Optional[Patch]:
84 | """Create a matplotlib patch object, corresponding to this region. May return None if no patch has to be created."""
85 | raise NotImplementedError("Method not implemented")
86 |
87 | def verify(self) -> None:
88 | """Self-verification routine for purposes of testing. Raises a VennRegionException if some inconsistencies of internal representation
89 | are discovered."""
90 | raise NotImplementedError("Method not implemented")
91 |
92 |
93 | class VennEmptyRegion(VennRegion):
94 | """
95 | An empty region. To save some memory, returns [self, self] on the subtract_and_intersect_circle operation.
96 | It is possible to create an empty region with a non-None label position, by providing it in the constructor.
97 |
98 | >>> v = VennEmptyRegion()
99 | >>> [a, b] = v.subtract_and_intersect_circle((1,2), 3)
100 | >>> assert a == v and b == v
101 | >>> assert v.label_position() is None
102 | >>> assert v.size() == 0
103 | >>> assert v.make_patch() is None
104 | >>> assert v.is_empty()
105 | >>> v = VennEmptyRegion((0, 0))
106 | >>> v.label_position().tolist()
107 | [0.0, 0.0]
108 | """
109 |
110 | def __init__(self, label_pos: Optional[Point2DInternal] = None):
111 | self.label_pos = None if label_pos is None else np.asarray(label_pos, float)
112 |
113 | def subtract_and_intersect_circle(
114 | self, center: Point2DInternal, radius: float
115 | ) -> Tuple[VennRegion, VennRegion]:
116 | return (self, self)
117 |
118 | def size(self) -> float:
119 | return 0
120 |
121 | def label_position(self) -> np.ndarray:
122 | return self.label_pos
123 |
124 | def make_patch(self) -> Optional[Patch]:
125 | return None
126 |
127 | def is_empty(
128 | self,
129 | ) -> bool: # We use this in tests as an equivalent of isinstance(VennEmptyRegion)
130 | return True
131 |
132 | def verify(self) -> None:
133 | pass
134 |
135 |
136 | class VennCircleRegion(VennRegion):
137 | """
138 | A circle-shaped region.
139 |
140 | >>> vcr = VennCircleRegion((0, 0), 1)
141 | >>> vcr.size()
142 | 3.1415...
143 | >>> vcr.label_position().tolist()
144 | [0.0, 0.0]
145 | >>> vcr.make_patch()
146 |
147 | >>> sr, ir = vcr.subtract_and_intersect_circle((0.5, 0), 1)
148 | >>> assert abs(sr.size() + ir.size() - vcr.size()) < NUMERIC_TOLERANCE
149 | """
150 |
151 | def __init__(self, center: Point2DInternal, radius: float):
152 | self.center = np.asarray(center, float)
153 | self.radius = abs(radius)
154 | if radius < -NUMERIC_TOLERANCE:
155 | raise VennRegionException("Circle with a negative radius is invalid")
156 |
157 | def subtract_and_intersect_circle(
158 | self, center: Point2DInternal, radius: float
159 | ) -> Tuple[VennRegion, VennRegion]:
160 | """Will throw a VennRegionException if the circle to be subtracted is completely inside and not touching the given region."""
161 |
162 | # Check whether the target circle intersects us
163 | center = np.asarray(center, float)
164 | d = np.linalg.norm(center - self.center)
165 | if d > (radius + self.radius - NUMERIC_TOLERANCE):
166 | return [self, VennEmptyRegion()] # The circle does not intersect us
167 | elif d < NUMERIC_TOLERANCE:
168 | if radius > self.radius - NUMERIC_TOLERANCE:
169 | # We are completely covered by that circle or we are the same circle
170 | return (VennEmptyRegion(), self)
171 | else:
172 | # That other circle is inside us and smaller than us - we can't deal with it
173 | raise VennRegionException(
174 | "Invalid configuration of circular regions (holes are not supported)."
175 | )
176 | else:
177 | # We *must* intersect the other circle. If it is not the case, then it is inside us completely,
178 | # and we'll complain.
179 | intersections = circle_circle_intersection(
180 | self.center, self.radius, center, radius
181 | )
182 |
183 | if intersections is None:
184 | raise VennRegionException(
185 | "Invalid configuration of circular regions (holes are not supported)."
186 | )
187 | elif (
188 | np.all(abs(intersections[0] - intersections[1]) < NUMERIC_TOLERANCE)
189 | and self.radius < radius
190 | ):
191 | # There is a single intersection point (i.e. we are touching the circle),
192 | # the circle to be subtracted is not outside of us (this was checked before), and is larger than us.
193 | # This is a particular corner case that is not dealt with correctly by the general-purpose code below and must
194 | # be handled separately
195 | return (VennEmptyRegion(), self)
196 | else:
197 | # Otherwise the subtracted region is a 2-arc-gon
198 | # Before we need to convert the intersection points as angles wrt each circle.
199 | a_1 = vector_angle_in_degrees(intersections[0] - self.center)
200 | a_2 = vector_angle_in_degrees(intersections[1] - self.center)
201 | b_1 = vector_angle_in_degrees(intersections[0] - center)
202 | b_2 = vector_angle_in_degrees(intersections[1] - center)
203 |
204 | # We must take care of the situation where the intersection points happen to be the same
205 | if abs(b_1 - b_2) < NUMERIC_TOLERANCE:
206 | b_1 = b_2 - NUMERIC_TOLERANCE / 2
207 | if abs(a_1 - a_2) < NUMERIC_TOLERANCE:
208 | a_2 = a_1 + NUMERIC_TOLERANCE / 2
209 |
210 | # The subtraction is a 2-arc-gon [(AB, B-), (BA, A+)]
211 | s_arc1 = Arc(center, radius, b_1, b_2, False)
212 | s_arc2 = Arc(self.center, self.radius, a_2, a_1, True)
213 | subtraction = VennArcgonRegion([s_arc1, s_arc2])
214 |
215 | # .. and the intersection is a 2-arc-gon [(AB, A+), (BA, B+)]
216 | i_arc1 = Arc(self.center, self.radius, a_1, a_2, True)
217 | i_arc2 = Arc(center, radius, b_2, b_1, True)
218 | intersection = VennArcgonRegion([i_arc1, i_arc2])
219 | return (subtraction, intersection)
220 |
221 | def size(self) -> float:
222 | """
223 | Return the area of the circle
224 |
225 | >>> VennCircleRegion((0, 0), 1).size()
226 | 3.1415...
227 | >>> VennCircleRegion((0, 0), 2).size()
228 | 12.56637...
229 | """
230 | return np.pi * self.radius**2
231 |
232 | def label_position(self) -> np.ndarray:
233 | """
234 | The label should be positioned in the center of the circle
235 |
236 | >>> VennCircleRegion((0, 0), 1).label_position().tolist()
237 | [0.0, 0.0]
238 | >>> VennCircleRegion((-1.2, 3.4), 1).label_position().tolist()
239 | [-1.2, 3.4]
240 | """
241 | return self.center
242 |
243 | def make_patch(self) -> Optional[Patch]:
244 | """
245 | Returns the corresponding circular patch.
246 |
247 | >>> patch = VennCircleRegion((1, 2), 3).make_patch()
248 | >>> patch
249 |
250 | >>> patch.center.tolist(), patch.radius
251 | ([1.0, 2.0], 3.0)
252 | """
253 | return Circle(self.center, self.radius)
254 |
255 | def verify(self) -> None:
256 | pass
257 |
258 |
259 | class VennArcgonRegion(VennRegion):
260 | """
261 | A poly-arc region.
262 | Note that we essentially only support 2, 3 and 4 arced regions,
263 | whereas intersections and subtractions only work for 2-arc regions.
264 | """
265 |
266 | def __init__(self, arcs: Sequence[Arc]):
267 | """
268 | Create a poly-arc region given a list of Arc objects.
269 | The arcs list must be of length 2, 3 or 4.
270 | The arcs must form a closed polygon, i.e. the last point of each arc must be the first point of the next arc.
271 | The vertices of a 3 or 4-arcgon must be listed in a CCW order. Arcs must not intersect.
272 |
273 | This is not verified in the constructor, but a special verify() method can be used to check
274 | for validity.
275 | """
276 | self.arcs = arcs
277 |
278 | def verify(self) -> None:
279 | """
280 | Verify the correctness of the region arcs. Throws an VennRegionException if verification fails
281 | (or any other exception if it happens during verification).
282 | """
283 | # Verify size of arcs list
284 | if len(self.arcs) < 2:
285 | raise VennRegionException("At least two arcs needed in a poly-arc region")
286 | if len(self.arcs) > 4:
287 | raise VennRegionException(
288 | "At most 4 arcs are supported currently for poly-arc regions"
289 | )
290 |
291 | TRIG_TOL = (
292 | 100 * NUMERIC_TOLERANCE
293 | ) # We need to use looser tolerance level here because conversion to angles and back is prone to large errors.
294 | # Verify connectedness of arcs
295 | for i in range(len(self.arcs)):
296 | if not np.all(
297 | self.arcs[i - 1].end_point() - self.arcs[i].start_point() < TRIG_TOL
298 | ):
299 | raise VennRegionException(
300 | "Arcs of an poly-arc-gon must be connected via endpoints"
301 | )
302 |
303 | # Verify that arcs do not cross-intersect except at endpoints
304 | for i in range(len(self.arcs) - 1):
305 | for j in range(i + 1, len(self.arcs)):
306 | ips = self.arcs[i].intersect_arc(self.arcs[j])
307 | for ip in ips:
308 | if not (
309 | np.all(abs(ip - self.arcs[i].start_point()) < TRIG_TOL)
310 | or np.all(abs(ip - self.arcs[i].end_point()) < TRIG_TOL)
311 | ):
312 | raise VennRegionException(
313 | "Arcs of a poly-arc-gon may only intersect at endpoints"
314 | )
315 |
316 | if (
317 | len(ips) != 0
318 | and (i - j) % len(self.arcs) > 1
319 | and (j - i) % len(self.arcs) > 1
320 | ):
321 | # Two non-consecutive arcs intersect. This is in general not good, but
322 | # may occasionally happen when all arcs inbetween have length 0.
323 | pass # raise VennRegionException("Non-consecutive arcs of a poly-arc-gon may not intersect")
324 |
325 | # Verify that vertices are ordered so that at each point the direction along the polyarc changes towards the left.
326 | # Note that this test only makes sense for polyarcs obtained using circle intersections & subtractions.
327 | # A "flower-like" polyarc may have its vertices ordered counter-clockwise yet the direction would turn to the right at each of them.
328 | for i in range(len(self.arcs)):
329 | prev_arc = self.arcs[i - 1]
330 | cur_arc = self.arcs[i]
331 | if (
332 | box_product(
333 | prev_arc.direction_vector(prev_arc.to_angle),
334 | cur_arc.direction_vector(cur_arc.from_angle),
335 | )
336 | < -NUMERIC_TOLERANCE
337 | ):
338 | raise VennRegionException(
339 | "Arcs must be ordered so that the direction at each vertex changes counter-clockwise"
340 | )
341 |
342 | def subtract_and_intersect_circle(
343 | self, center: Point2DInternal, radius: float
344 | ) -> Tuple[Patch, Patch]:
345 | """
346 | Circle subtraction / intersection only supported by 2-gon regions, otherwise a VennRegionException is thrown.
347 | In addition, such an exception will be thrown if the circle to be subtracted is completely within the region and forms a "hole".
348 |
349 | The result may be either a VennArcgonRegion or a VennMultipieceRegion (the latter happens when the circle "splits" a crescent in two).
350 | """
351 | if len(self.arcs) != 2:
352 | raise VennRegionException(
353 | "Circle subtraction and intersection with poly-arc regions is currently only supported for 2-arc-gons."
354 | )
355 |
356 | # In the following we consider the 2-arc-gon case.
357 | # Before we do anything, we check for a special case, where the circle of interest is one of the two circles forming the arcs.
358 | # In this case we can determine the answer quite easily.
359 | matching_arcs = [a for a in self.arcs if a.lies_on_circle(center, radius)]
360 | if len(matching_arcs) != 0:
361 | # If the circle matches a positive arc, the result is [empty, self], otherwise [self, empty]
362 | return (
363 | [VennEmptyRegion(), self]
364 | if matching_arcs[0].direction
365 | else [self, VennEmptyRegion()]
366 | )
367 |
368 | # Consider the intersection points of the circle with the arcs.
369 | # If any of the intersection points corresponds exactly to any of the arc's endpoints, we will end up with
370 | # a lot of messy special cases (as if the usual situation is not messy enough, eh).
371 | # To avoid that, we cheat by slightly increasing the circle's radius until this is not the case any more.
372 | center = np.asarray(center)
373 | illegal_intersections = [a.start_point() for a in self.arcs]
374 | while True:
375 | valid = True
376 | intersections = [a.intersect_circle(center, radius) for a in self.arcs]
377 | for ints in intersections:
378 | for pt in ints:
379 | for illegal_pt in illegal_intersections:
380 | if np.all(abs(pt - illegal_pt) < NUMERIC_TOLERANCE):
381 | valid = False
382 | if valid:
383 | break
384 | else:
385 | radius += NUMERIC_TOLERANCE
386 |
387 | # There must be an even number of those points in total.
388 | # (If this is not the case, then we have an unfortunate case with weird numeric errors [TODO: find examples and deal with it?]).
389 | # There are three possibilities with the following subcases:
390 | # I. No intersection points
391 | # a) The polyarc is completely within the circle.
392 | # result = [ empty, self ]
393 | # b) The polyarc is completely outside the circle.
394 | # result = [ self, empty ]
395 | # II. Four intersection points, two for each arc. Points x1, x2 for arc X and y1, y2 for arc Y, ordered along the arc.
396 | # a) The polyarc endpoints are both outside the circle.
397 | # result_subtraction = a combination of two 3-arc polyarcs:
398 | # 1: {X - start to x1,
399 | # x1 to y2 along circle (negative direction)),
400 | # Y - y2 to end}
401 | # 2: {Y start to y1,
402 | # y1 to x2 along circle (negative direction)),
403 | # X - x2 to end}
404 | # b) The polyarc endpoints are both inside the circle
405 | # same as above, but the "along circle" arc directions are flipped and subtraction/intersection parts are exchanged
406 | # III. Two intersection points
407 | # a) One arc, X, has two intersection points i & j, another arc, Y, has no intersection points
408 | # a.1) Polyarc endpoints are outside the circle
409 | # result_subtraction = {X from start to i, circle i to j (direction = negative), X j to end, Y}
410 | # result_intersection = {X i to j, circle j to i (direction = positive}
411 | # a.2) Polyarc endpoints are inside the circle
412 | # result_subtraction = {X i to j, circle j to i negative}
413 | # result_intersection = {X 0 to i, circle i to j positive, X j to end, Y}
414 | # b) Both arcs, X and Y, have one intersection point each. In this case one of the arc endpoints must be inside circle, another outside.
415 | # call the arc that starts with the outside point X, the other arc Y.
416 | # result_subtraction = {X start to intersection, intersection to intersection along circle (negative direction), Y from intersection to end}
417 | # result_intersection = {X intersection to end, Y start to intersecton, intersection to intersecion along circle (positive)}
418 | center = np.asarray(center)
419 | intersections = [a.intersect_circle(center, radius) for a in self.arcs]
420 |
421 | if len(intersections[0]) == 0 and len(intersections[1]) == 0:
422 | # Case I
423 | if point_in_circle(self.arcs[0].start_point(), center, radius):
424 | # Case I.a)
425 | return (VennEmptyRegion(), self)
426 | else:
427 | # Case I.b)
428 | return (self, VennEmptyRegion())
429 | elif len(intersections[0]) == 2 and len(intersections[1]) == 2:
430 | # Case II. a) or b)
431 | case_II_a = not point_in_circle(self.arcs[0].start_point(), center, radius)
432 |
433 | a1 = self.arcs[0].subarc_between_points(None, intersections[0][0])
434 | a2 = Arc(
435 | center,
436 | radius,
437 | vector_angle_in_degrees(intersections[0][0] - center),
438 | vector_angle_in_degrees(intersections[1][1] - center),
439 | not case_II_a,
440 | )
441 | a2.fix_360_to_0()
442 | a3 = self.arcs[1].subarc_between_points(intersections[1][1], None)
443 | piece1 = VennArcgonRegion([a1, a2, a3])
444 |
445 | b1 = self.arcs[1].subarc_between_points(None, intersections[1][0])
446 | b2 = Arc(
447 | center,
448 | radius,
449 | vector_angle_in_degrees(intersections[1][0] - center),
450 | vector_angle_in_degrees(intersections[0][1] - center),
451 | not case_II_a,
452 | )
453 | b2.fix_360_to_0()
454 | b3 = self.arcs[0].subarc_between_points(intersections[0][1], None)
455 | piece2 = VennArcgonRegion([b1, b2, b3])
456 |
457 | subtraction = VennMultipieceRegion([piece1, piece2])
458 |
459 | c1 = self.arcs[0].subarc(a1.to_angle, b3.from_angle)
460 | c2 = b2.reversed()
461 | c3 = self.arcs[1].subarc(b1.to_angle, a3.from_angle)
462 | c4 = a2.reversed()
463 | intersection = VennArcgonRegion([c1, c2, c3, c4])
464 |
465 | return (
466 | (subtraction, intersection)
467 | if case_II_a
468 | else (intersection, subtraction)
469 | )
470 | else:
471 | # Case III. Yuck.
472 | if len(intersections[0]) == 0 or len(intersections[1]) == 0:
473 | # Case III.a)
474 | x = 0 if len(intersections[0]) != 0 else 1
475 | y = 1 - x
476 | if len(intersections[x]) != 2:
477 | warnings.warn(
478 | "Numeric precision error during polyarc intersection, case IIIa. Expect wrong results."
479 | )
480 | intersections[x] = [
481 | intersections[x][0],
482 | intersections[x][0],
483 | ] # This way we'll at least produce some result, although it will probably be wrong
484 | if not point_in_circle(self.arcs[0].start_point(), center, radius):
485 | # Case III.a.1)
486 | # result_subtraction = {X from start to i, circle i to j (direction = negative), X j to end, Y}
487 | a1 = self.arcs[x].subarc_between_points(None, intersections[x][0])
488 | a2 = Arc(
489 | center,
490 | radius,
491 | vector_angle_in_degrees(intersections[x][0] - center),
492 | vector_angle_in_degrees(intersections[x][1] - center),
493 | False,
494 | )
495 | a3 = self.arcs[x].subarc_between_points(intersections[x][1], None)
496 | a4 = self.arcs[y]
497 | subtraction = VennArcgonRegion([a1, a2, a3, a4])
498 |
499 | # result_intersection = {X i to j, circle j to i (direction = positive)}
500 | b1 = self.arcs[x].subarc(a1.to_angle, a3.from_angle)
501 | b2 = a2.reversed()
502 | intersection = VennArcgonRegion([b1, b2])
503 |
504 | return (subtraction, intersection)
505 | else:
506 | # Case III.a.2)
507 | # result_subtraction = {X i to j, circle j to i negative}
508 | a1 = self.arcs[x].subarc_between_points(
509 | intersections[x][0], intersections[x][1]
510 | )
511 | a2 = Arc(
512 | center,
513 | radius,
514 | vector_angle_in_degrees(intersections[x][1] - center),
515 | vector_angle_in_degrees(intersections[x][0] - center),
516 | False,
517 | )
518 | subtraction = VennArcgonRegion([a1, a2])
519 |
520 | # result_intersection = {X 0 to i, circle i to j positive, X j to end, Y}
521 | b1 = self.arcs[x].subarc(None, a1.from_angle)
522 | b2 = a2.reversed()
523 | b3 = self.arcs[x].subarc(a1.to_angle, None)
524 | b4 = self.arcs[y]
525 | intersection = VennArcgonRegion([b1, b2, b3, b4])
526 |
527 | return (subtraction, intersection)
528 | else:
529 | # Case III.b)
530 | if len(intersections[0]) == 2 or len(intersections[1]) == 2:
531 | warnings.warn(
532 | "Numeric precision error during polyarc intersection, case IIIb. Expect wrong results."
533 | )
534 |
535 | # One of the arcs must start outside the circle, call it x
536 | x = (
537 | 0
538 | if not point_in_circle(self.arcs[0].start_point(), center, radius)
539 | else 1
540 | )
541 | y = 1 - x
542 |
543 | a1 = self.arcs[x].subarc_between_points(None, intersections[x][0])
544 | a2 = Arc(
545 | center,
546 | radius,
547 | vector_angle_in_degrees(intersections[x][0] - center),
548 | vector_angle_in_degrees(intersections[y][0] - center),
549 | False,
550 | )
551 | a3 = self.arcs[y].subarc_between_points(intersections[y][0], None)
552 | subtraction = VennArcgonRegion([a1, a2, a3])
553 |
554 | b1 = self.arcs[x].subarc(a1.to_angle, None)
555 | b2 = self.arcs[y].subarc(None, a3.from_angle)
556 | b3 = a2.reversed()
557 | intersection = VennArcgonRegion([b1, b2, b3])
558 | return (subtraction, intersection)
559 |
560 | def label_position(self) -> np.ndarray:
561 | # Position the label right inbetween the midpoints of the arcs
562 | midpoints = [a.mid_point() for a in self.arcs]
563 | # For two-arc regions take the usual average
564 | # For more than two arcs, use arc lengths as the weights.
565 | if len(self.arcs) == 2:
566 | return np.mean(midpoints, 0)
567 | else:
568 | lengths = [a.length_degrees() for a in self.arcs]
569 | avg = np.sum([mp * l for (mp, l) in zip(midpoints, lengths)], 0)
570 | return avg / np.sum(lengths)
571 |
572 | def size(self) -> float:
573 | """Return the area of the patch.
574 |
575 | The area can be computed using the standard polygon area formula + signed segment areas of each arc.
576 | """
577 | polygon_area = 0
578 | for a in self.arcs:
579 | polygon_area += box_product(a.start_point(), a.end_point())
580 | polygon_area /= 2.0
581 | return polygon_area + sum([a.sign * a.segment_area() for a in self.arcs])
582 |
583 | def make_patch(self) -> Optional[Patch]:
584 | """
585 | Retuns a matplotlib PathPatch representing the current region.
586 | """
587 | path = [self.arcs[0].start_point()]
588 | for a in self.arcs:
589 | if a.direction:
590 | vertices = Path.arc(a.from_angle, a.to_angle).vertices
591 | else:
592 | vertices = Path.arc(a.to_angle, a.from_angle).vertices
593 | vertices = vertices[np.arange(len(vertices) - 1, -1, -1)]
594 | vertices = vertices * a.radius + a.center
595 | path = path + list(vertices[1:])
596 | codes = [1] + [4] * (
597 | len(path) - 1
598 | ) # NB: We could also add a CLOSEPOLY code (and a random vertex) to the end
599 | return PathPatch(Path(path, codes))
600 |
601 |
602 | class VennMultipieceRegion(VennRegion):
603 | """
604 | A region containing several pieces.
605 | In principle, any number of pieces is supported,
606 | although no more than 2 should ever be needed in a 3-circle Venn diagram.
607 | Although subtraction/intersection are straightforward to implement we do
608 | not need those for matplotlib-venn, we raise exceptions in those methods.
609 | """
610 |
611 | def __init__(self, pieces: Sequence[VennRegion]):
612 | """
613 | Create a multi-piece region from a list of VennRegion objects.
614 | The list may be empty or contain a single item (although those regions can be converted to a
615 | VennEmptyRegion or a single region of the necessary type.
616 | """
617 | self.pieces = pieces
618 |
619 | def label_position(self) -> np.ndarray:
620 | """
621 | Find the largest region and position the label in that.
622 | """
623 | reg_sizes = [(r.size(), r) for r in self.pieces]
624 | reg_sizes.sort()
625 | return reg_sizes[-1][1].label_position()
626 |
627 | def size(self) -> float:
628 | return sum([p.size() for p in self.pieces])
629 |
630 | def make_patch(self) -> Optional[Patch]:
631 | """Currently only works if all the pieces are Arcgons.
632 | In this case returns a multiple-piece path. Otherwise throws an exception."""
633 | paths = [p.make_patch().get_path() for p in self.pieces]
634 | vertices = np.concatenate([p.vertices for p in paths])
635 | codes = np.concatenate([p.codes for p in paths])
636 | return PathPatch(Path(vertices, codes))
637 |
638 | def verify(self) -> None:
639 | for p in self.pieces:
640 | p.verify()
641 |
--------------------------------------------------------------------------------
/matplotlib_venn/_util.py:
--------------------------------------------------------------------------------
1 | """
2 | Venn diagram plotting routines.
3 | Utility routines
4 |
5 | Copyright 2012-2024, Konstantin Tretyakov.
6 | http://kt.era.ee/
7 |
8 | Licensed under MIT license.
9 | """
10 |
11 | import warnings
12 | from matplotlib_venn._venn2 import venn2
13 | from matplotlib_venn._venn3 import venn3
14 | from matplotlib_venn.layout.venn2 import DefaultLayoutAlgorithm as Venn2Layout
15 | from matplotlib_venn.layout.venn3 import DefaultLayoutAlgorithm as Venn3Layout
16 |
17 |
18 | def venn2_unweighted(
19 | subsets,
20 | set_labels=("A", "B"),
21 | set_colors=("r", "g"),
22 | alpha=0.4,
23 | normalize_to=1.0,
24 | subset_areas=(1, 1, 1),
25 | ax=None,
26 | subset_label_formatter=None,
27 | ):
28 | """
29 | This function is deprecated and will be removed in a future version.
30 | Use venn2(..., layout_algorithm=matplotlib_venn.layout.venn2.DefaultLayoutAlgorithm(fixed_subset_sizes=(1,1,1))) instead.
31 | """
32 | warnings.warn(
33 | "venn2_unweighted is deprecated. Use venn2 with the appropriate layout_algorithm instead."
34 | )
35 | return venn2(
36 | subsets,
37 | set_labels,
38 | set_colors,
39 | alpha,
40 | ax,
41 | subset_label_formatter=subset_label_formatter,
42 | layout_algorithm=Venn2Layout(
43 | normalize_to=normalize_to, fixed_subset_sizes=subset_areas
44 | ),
45 | )
46 |
47 |
48 | def venn3_unweighted(
49 | subsets,
50 | set_labels=("A", "B", "C"),
51 | set_colors=("r", "g", "b"),
52 | alpha=0.4,
53 | normalize_to=1.0,
54 | subset_areas=(1, 1, 1, 1, 1, 1, 1),
55 | ax=None,
56 | subset_label_formatter=None,
57 | ):
58 | """
59 | This function is deprecated and will be removed in a future version.
60 | Use venn3(..., layout_algorithm=matplotlib_venn.layout.venn3.DefaultLayoutAlgorithm(fixed_subset_sizes=(1,1,1,1,1,1,1))) instead.
61 | """
62 | warnings.warn(
63 | "venn3_unweighted is deprecated. Use venn3 with the appropriate layout_algorithm instead."
64 | )
65 | return venn3(
66 | subsets,
67 | set_labels,
68 | set_colors,
69 | alpha,
70 | ax,
71 | subset_label_formatter=subset_label_formatter,
72 | layout_algorithm=Venn3Layout(
73 | normalize_to=normalize_to, fixed_subset_sizes=subset_areas
74 | ),
75 | )
76 |
--------------------------------------------------------------------------------
/matplotlib_venn/_venn2.py:
--------------------------------------------------------------------------------
1 | """
2 | Venn diagram plotting routines.
3 | Two-circle venn plotter.
4 |
5 | Copyright 2012, Konstantin Tretyakov.
6 | http://kt.era.ee/
7 |
8 | Licensed under MIT license.
9 | """
10 |
11 | # Make sure we don't try to do GUI stuff when running tests
12 | import sys, os
13 |
14 | if "py.test" in os.path.basename(sys.argv[0]): # (XXX: Ugly hack)
15 | import matplotlib
16 |
17 | matplotlib.use("Agg")
18 |
19 | from typing import Any, Callable, Dict, Optional, Tuple, Union
20 | import numpy as np
21 | import warnings
22 | from collections import Counter
23 |
24 | from matplotlib.axes import Axes
25 | from matplotlib.patches import Circle
26 | from matplotlib.colors import ColorConverter
27 | from matplotlib.pyplot import gca
28 |
29 | from matplotlib_venn._math import Point2D
30 | from matplotlib_venn._common import VennDiagram, prepare_venn_axes, mix_colors
31 | from matplotlib_venn._region import VennRegion, VennCircleRegion
32 | from matplotlib_venn.layout.api import VennLayout, VennLayoutAlgorithm
33 | from matplotlib_venn.layout.venn2 import DefaultLayoutAlgorithm
34 |
35 | Venn2SubsetSizes = Tuple[float, float, float]
36 |
37 |
38 | def venn2_circles(
39 | subsets: Union[Tuple[set, set], Dict[str, float], Venn2SubsetSizes],
40 | normalize_to: Optional[float] = None,
41 | alpha: float = 1.0,
42 | color: Any = "black",
43 | linestyle: str = "solid",
44 | linewidth: float = 2.0,
45 | ax: Axes = None,
46 | layout_algorithm: Optional[VennLayoutAlgorithm] = None,
47 | **kwargs
48 | ):
49 | """
50 | Plots only the two circles for the corresponding Venn diagram.
51 | Useful for debugging or enhancing the basic venn diagram.
52 |
53 | Args:
54 | subsets: Same as in `venn2`.
55 | normalize_to: Same as in `venn2`.
56 | alpha: The alpha parameter of the circle patches.
57 | color: The edgecolor of the circle patches (as understood by matplotlib).
58 | linestyle: The linestyle of the circle patches.
59 | linewidth: The line width of the circle patches.
60 | ax: Axis to draw upon, defaults to gca().
61 | layout_algorithm: The layout algorithm to be used. Defaults to matplotlib_venn.layout.venn2.DefaultLayoutAlgorithm(normalize_to).
62 | **kwargs: passed as-is to matplotlib.patches.Circle.
63 |
64 | Returns:
65 | a list of two Circle patches plotted.
66 |
67 | >>> c = venn2_circles((1, 2, 3))
68 | >>> c = venn2_circles({'10': 1, '01': 2, '11': 3}) # Same effect
69 | >>> c = venn2_circles([set([1,2,3,4]), set([2,3,4,5,6])]) # Also same effect
70 | """
71 | if isinstance(subsets, dict):
72 | subsets = [subsets.get(t, 0) for t in ["10", "01", "11"]]
73 | elif len(subsets) == 2:
74 | subsets = _compute_subset_sizes(*subsets)
75 |
76 | if normalize_to is not None:
77 | if layout_algorithm is None:
78 | warnings.warn(
79 | "normalize_to is deprecated. Please use layout_algorithm=matplotlib_venn.layout.venn2.DefaultLayoutAlgorithm(normalize_to) instead."
80 | )
81 | else:
82 | raise ValueError(
83 | "normalize_to is deprecated and may not be specified together with a custom layout algorithm."
84 | )
85 | if layout_algorithm is None:
86 | layout_algorithm = DefaultLayoutAlgorithm(normalize_to=normalize_to or 1.0)
87 |
88 | layout = layout_algorithm(subsets)
89 | if ax is None:
90 | ax = gca()
91 | prepare_venn_axes(ax, layout.centers, layout.radii)
92 | result = []
93 | for c, r in zip(layout.centers, layout.radii):
94 | circle = Circle(
95 | c.asarray(),
96 | r,
97 | alpha=alpha,
98 | edgecolor=color,
99 | facecolor="none",
100 | linestyle=linestyle,
101 | linewidth=linewidth,
102 | **kwargs
103 | )
104 | ax.add_patch(circle)
105 | result.append(circle)
106 | return tuple(result)
107 |
108 |
109 | def venn2(
110 | subsets: Union[Tuple[set, set], Dict[str, float], Venn2SubsetSizes],
111 | set_labels: Optional[Tuple[str, str]] = ("A", "B"),
112 | set_colors: Tuple[Any, Any] = ("r", "g"),
113 | alpha: float = 0.4,
114 | normalize_to: Optional[float] = None,
115 | ax: Optional[Axes] = None,
116 | subset_label_formatter: Optional[Callable[[float], str]] = None,
117 | layout_algorithm: Optional[VennLayoutAlgorithm] = None,
118 | ):
119 | """Plots a 2-set area-weighted Venn diagram.
120 |
121 | Args:
122 | subsets: one of the following:
123 | - A tuple of two set objects.
124 | - A dict, providing relative sizes of the three diagram regions.
125 | The regions are identified via two-letter binary codes ('10', '01', '11'), hence a valid artgument could look like:
126 | {'01': 10, '11': 20}. Unmentioned codes are considered to map to 0.
127 | - A tuple with 3 numbers, denoting the sizes of the regions in the following order:
128 | (10, 01, 11).
129 | set_labels: An optional tuple of two strings - set labels. Set it to None to disable set labels.
130 | set_colors: A tuple of two color specifications, specifying the base colors of the two circles.
131 | The colors of circle intersection will be computed based on those.
132 | normalize_to: Deprecated. Use normalize_to argument of matplotlib_venn.layout.venn2.DefaultLayoutAlgorithm instead.
133 | ax: The axes to plot upon. Defaults to gca().
134 | subset_label_formatter: A function that converts numeric subset sizes to strings to be shown on the subset patches in the diagram.
135 | Defaults to "str".
136 | layout_algorithm: The layout algorithm to determine the scale and position of the three circles. Defaults to
137 | matplotlib_venn.layout.venn2.DefaultLayoutAlgorithm().
138 |
139 | Returns:
140 | a `VennDiagram` object that keeps references to the layout information, ``Text`` and ``Patch`` objects used on the plot.
141 |
142 | >>> from matplotlib_venn import *
143 | >>> v = venn2(subsets={'10': 1, '01': 1, '11': 1}, set_labels = ('A', 'B'))
144 | >>> c = venn2_circles(subsets=(1, 1, 1), linestyle='dashed')
145 | >>> v.get_patch_by_id('10').set_alpha(1.0)
146 | >>> v.get_patch_by_id('10').set_color('white')
147 | >>> v.get_label_by_id('10').set_text('Unknown')
148 | >>> v.get_label_by_id('A').set_text('Set A')
149 |
150 | You can provide sets themselves rather than subset sizes:
151 | >>> v = venn2(subsets=[set([1,2]), set([2,3,4,5])], set_labels = ('A', 'B'))
152 | >>> c = venn2_circles(subsets=[set([1,2]), set([2,3,4,5])], linestyle='dashed')
153 | >>> print("%0.2f" % (v.get_circle_radius(1)/v.get_circle_radius(0)))
154 | 1.41
155 | """
156 | if isinstance(subsets, dict):
157 | subsets = [subsets.get(t, 0) for t in ["10", "01", "11"]]
158 | elif len(subsets) == 2:
159 | subsets = _compute_subset_sizes(*subsets)
160 | if normalize_to is not None:
161 | if layout_algorithm is None:
162 | warnings.warn(
163 | "normalize_to is deprecated. Please use layout_algorithm=matplotlib_venn.layout.venn2.DefaultLayoutAlgorithm(normalize_to) instead."
164 | )
165 | else:
166 | raise ValueError(
167 | "normalize_to is deprecated and may not be specified together with a custom layout algorithm."
168 | )
169 | if layout_algorithm is None:
170 | layout_algorithm = DefaultLayoutAlgorithm(normalize_to=normalize_to or 1.0)
171 |
172 | layout = layout_algorithm(subsets, set_labels)
173 | return _render_layout(
174 | layout, subsets, set_labels, set_colors, alpha, ax, subset_label_formatter
175 | )
176 |
177 |
178 | def _render_layout(
179 | layout: VennLayout,
180 | subsets: Venn2SubsetSizes,
181 | set_labels: Optional[Tuple[str, str]] = ("A", "B"),
182 | set_colors: Tuple[Any, Any] = ("r", "g"),
183 | alpha: float = 0.4,
184 | ax: Optional[Axes] = None,
185 | subset_label_formatter: Optional[Callable[[float], str]] = None,
186 | ) -> VennDiagram:
187 | """Renders the layout."""
188 | if subset_label_formatter is None:
189 | subset_label_formatter = str
190 | if ax is None:
191 | ax = gca()
192 | prepare_venn_axes(ax, layout.centers, layout.radii)
193 | colors = _compute_colors(*set_colors)
194 | regions = _compute_regions(layout.centers, layout.radii)
195 | patches = [r.make_patch() for r in regions]
196 | for p, c in zip(patches, colors):
197 | if p is not None:
198 | p.set_facecolor(c)
199 | p.set_edgecolor("none")
200 | p.set_alpha(alpha)
201 | ax.add_patch(p)
202 | label_positions = [r.label_position() for r in regions]
203 | subset_labels = [
204 | (
205 | ax.text(lbl[0], lbl[1], subset_label_formatter(s), va="center", ha="center")
206 | if lbl is not None
207 | else None
208 | )
209 | for (lbl, s) in zip(label_positions, subsets)
210 | ]
211 | if set_labels is not None:
212 | labels = [
213 | ax.text(lbl.position.x, lbl.position.y, txt, size="large", **lbl.kwargs)
214 | for (lbl, txt) in zip(layout.set_labels_layout, set_labels)
215 | ]
216 | else:
217 | labels = None
218 | return VennDiagram(patches, subset_labels, labels, layout.centers, layout.radii)
219 |
220 |
221 | def _compute_regions(
222 | centers: Tuple[Point2D, Point2D], radii: Tuple[float, float]
223 | ) -> Tuple[VennRegion, VennRegion, VennRegion]:
224 | """
225 | Returns a triple of VennRegion objects, describing the three regions of the diagram, corresponding to sets
226 | (Ab, aB, AB)
227 |
228 | >>> layout = DefaultLayoutAlgorithm()((1, 1, 0.5))
229 | >>> regions = _compute_regions(layout.centers, layout.radii)
230 | """
231 | A = VennCircleRegion(centers[0].asarray(), radii[0])
232 | B = VennCircleRegion(centers[1].asarray(), radii[1])
233 | Ab, AB = A.subtract_and_intersect_circle(B.center, B.radius)
234 | aB, _ = B.subtract_and_intersect_circle(A.center, A.radius)
235 | return (Ab, aB, AB)
236 |
237 |
238 | def _compute_colors(
239 | color_a: Any, color_b: Any
240 | ) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
241 | """
242 | Given two base colors, computes combinations of colors corresponding to all regions of the venn diagram.
243 | returns a list of 3 elements, providing colors for regions (10, 01, 11).
244 |
245 | >>> str(_compute_colors('r', 'g')).replace(' ', '')
246 | '(array([1.,0.,0.]),array([0.,0.5,0.]),array([0.7,0.35,0.]))'
247 | """
248 | ccv = ColorConverter()
249 | base_colors = [np.array(ccv.to_rgb(c)) for c in [color_a, color_b]]
250 | return (base_colors[0], base_colors[1], mix_colors(base_colors[0], base_colors[1]))
251 |
252 |
253 | def _compute_subset_sizes(
254 | a: Union[set, Counter], b: Union[set, Counter]
255 | ) -> Tuple[float, float, float]:
256 | """
257 | Given two set or Counter objects, computes the sizes of (a & ~b, b & ~a, a & b).
258 | Returns the result as a tuple.
259 |
260 | >>> _compute_subset_sizes(set([1,2,3,4]), set([2,3,4,5,6]))
261 | (1, 2, 3)
262 | >>> _compute_subset_sizes(Counter([1,2,3,4]), Counter([2,3,4,5,6]))
263 | (1, 2, 3)
264 | >>> _compute_subset_sizes(Counter([]), Counter([]))
265 | (0, 0, 0)
266 | >>> _compute_subset_sizes(set([]), set([]))
267 | (0, 0, 0)
268 | >>> _compute_subset_sizes(set([1]), set([]))
269 | (1, 0, 0)
270 | >>> _compute_subset_sizes(set([1]), set([1]))
271 | (0, 0, 1)
272 | >>> _compute_subset_sizes(Counter([1]), Counter([1]))
273 | (0, 0, 1)
274 | >>> _compute_subset_sizes(set([1,2]), set([1]))
275 | (1, 0, 1)
276 | >>> _compute_subset_sizes(Counter([1,1,2,2,2]), Counter([1,2,3,3]))
277 | (3, 2, 2)
278 | >>> _compute_subset_sizes(Counter([1,1,2]), Counter([1,2,2]))
279 | (1, 1, 2)
280 | >>> _compute_subset_sizes(Counter([1,1]), set([]))
281 | Traceback (most recent call last):
282 | ...
283 | ValueError: Both arguments must be of the same type
284 | """
285 | if not (type(a) == type(b)):
286 | raise ValueError("Both arguments must be of the same type")
287 | set_size = (
288 | len if type(a) != Counter else lambda x: sum(x.values())
289 | ) # We cannot use len to compute the cardinality of a Counter
290 | return (set_size(a - b), set_size(b - a), set_size(a & b))
291 |
--------------------------------------------------------------------------------
/matplotlib_venn/_venn3.py:
--------------------------------------------------------------------------------
1 | """
2 | Venn diagram plotting routines.
3 | Three-circle venn plotter.
4 |
5 | Copyright 2012-2024, Konstantin Tretyakov.
6 | http://kt.era.ee/
7 |
8 | Licensed under MIT license.
9 | """
10 |
11 | from typing import Any, Callable, Dict, Optional, Tuple, Union
12 | import numpy as np
13 | import warnings
14 | from collections import Counter
15 |
16 | from matplotlib.axes import Axes
17 | from matplotlib.patches import Circle, PathPatch
18 | from matplotlib.path import Path
19 | from matplotlib.colors import ColorConverter
20 | from matplotlib.pyplot import gca
21 |
22 | from matplotlib_venn._math import circle_circle_intersection, NUMERIC_TOLERANCE, Point2D
23 | from matplotlib_venn._common import VennDiagram, prepare_venn_axes, mix_colors
24 | from matplotlib_venn._region import VennRegion, VennCircleRegion, VennEmptyRegion
25 | from matplotlib_venn.layout.api import VennLayout, VennLayoutAlgorithm
26 | from matplotlib_venn.layout.venn3 import DefaultLayoutAlgorithm
27 |
28 | Venn3SubsetSizes = Tuple[float, float, float, float, float, float, float]
29 |
30 |
31 | def venn3_circles(
32 | subsets: Union[Tuple[set, set, set], Dict[str, float], Venn3SubsetSizes],
33 | normalize_to: Optional[float] = None,
34 | alpha: float = 1.0,
35 | color: Any = "black",
36 | linestyle: str = "solid",
37 | linewidth: str = 2.0,
38 | ax: Optional[Axes] = None,
39 | layout_algorithm: Optional[VennLayoutAlgorithm] = None,
40 | **kwargs
41 | ) -> Tuple[Circle, Circle, Circle]:
42 | """
43 | Plots only the three circles for the corresponding Venn diagram.
44 | Useful for debugging or enhancing the basic venn diagram.
45 |
46 | Args:
47 | subsets: Same as in `venn3`.
48 | normalize_to: Same as in `venn3`.
49 | alpha: The alpha parameter of the circle patches.
50 | color: The edgecolor of the circle patches (as understood by matplotlib).
51 | linestyle: The linestyle of the circle patches.
52 | linewidth: The line width of the circle patches.
53 | ax: Axis to draw upon, defaults to gca().
54 | layout_algorithm: The layout algorithm to be used. Defaults to matplotlib_venn.layout.venn3.DefaultLayoutAlgorithm(normalize_to).
55 | **kwargs: passed as-is to matplotlib.patches.Circle.
56 |
57 | Returns:
58 | a list of three Circle patches plotted.
59 |
60 | >>> plot = venn3_circles({'001': 10, '100': 20, '010': 21, '110': 13, '011': 14})
61 | >>> plot = venn3_circles([set(['A','B','C']), set(['A','D','E','F']), set(['D','G','H'])])
62 | """
63 | # Prepare parameters
64 | if isinstance(subsets, dict):
65 | subsets = [
66 | subsets.get(t, 0) for t in ["100", "010", "110", "001", "101", "011", "111"]
67 | ]
68 | elif len(subsets) == 3:
69 | subsets = _compute_subset_sizes(*subsets)
70 |
71 | if normalize_to is not None:
72 | if layout_algorithm is None:
73 | warnings.warn(
74 | "normalize_to is deprecated. Please use layout_algorithm=matplotlib_venn.layout.venn3.DefaultLayoutAlgorithm(normalize_to) instead."
75 | )
76 | else:
77 | raise ValueError(
78 | "normalize_to is deprecated and may not be specified together with a custom layout algorithm."
79 | )
80 | if layout_algorithm is None:
81 | layout_algorithm = DefaultLayoutAlgorithm(normalize_to=normalize_to or 1.0)
82 |
83 | layout = layout_algorithm(subsets)
84 | if ax is None:
85 | ax = gca()
86 | prepare_venn_axes(ax, layout.centers, layout.radii)
87 | result = []
88 | for c, r in zip(layout.centers, layout.radii):
89 | circle = Circle(
90 | c.asarray(),
91 | r,
92 | alpha=alpha,
93 | edgecolor=color,
94 | facecolor="none",
95 | linestyle=linestyle,
96 | linewidth=linewidth,
97 | **kwargs
98 | )
99 | ax.add_patch(circle)
100 | result.append(circle)
101 | return tuple(result)
102 |
103 |
104 | def venn3(
105 | subsets: Union[Tuple[set, set, set], Dict[str, float], Venn3SubsetSizes],
106 | set_labels: Optional[Tuple[str, str, str]] = ("A", "B", "C"),
107 | set_colors: Tuple[Any, Any, Any] = ("r", "g", "b"),
108 | alpha: float = 0.4,
109 | normalize_to: Optional[float] = None,
110 | ax: Optional[Axes] = None,
111 | subset_label_formatter: Optional[Callable[[float], str]] = None,
112 | layout_algorithm: Optional[VennLayoutAlgorithm] = None,
113 | ) -> VennDiagram:
114 | """Plots a 3-set area-weighted Venn diagram.
115 |
116 | Note: if some of the circles happen to have zero area, you will probably not get a nice picture.
117 |
118 | Args:
119 | subsets: one of the following:
120 | - A tuple of three set objects.
121 | - A dict, providing relative sizes of the seven diagram regions.
122 | The regions are identified via three-letter binary codes ('100', '010', etc), hence a valid artgument could look like:
123 | {'001': 10, '010': 20, '110':30, ...}. Unmentioned codes are considered to map to 0.
124 | - A tuple with 7 numbers, denoting the sizes of the regions in the following order:
125 | (100, 010, 110, 001, 101, 011, 111).
126 | set_labels: An optional tuple of three strings - set labels. Set it to None to disable set labels.
127 | set_colors: A tuple of three color specifications, specifying the base colors of the three circles.
128 | The colors of circle intersections will be computed based on those.
129 | normalize_to: Deprecated. Use normalize_to argument of matplotlib_venn.layout.venn3.DefaultLayoutAlgorithm instead.
130 | ax: The axes to plot upon. Defaults to gca().
131 | subset_label_formatter: A function that converts numeric subset sizes to strings to be shown on the subset patches in the diagram.
132 | Defaults to "str".
133 | layout_algorithm: The layout algorithm to determine the scale and position of the three circles. Defaults to
134 | matplotlib_venn.layout.venn3.DefaultLayoutAlgorithm().
135 |
136 | Returns:
137 | a `VennDiagram` object that keeps references to the layout information, ``Text`` and ``Patch`` objects used on the plot.
138 |
139 | >>> import matplotlib # (The first two lines prevent the doctest from falling when TCL not installed. Not really necessary in most cases)
140 | >>> matplotlib.use('Agg')
141 | >>> from matplotlib_venn import *
142 | >>> v = venn3(subsets=(1, 1, 1, 1, 1, 1, 1), set_labels = ('A', 'B', 'C'))
143 | >>> c = venn3_circles(subsets=(1, 1, 1, 1, 1, 1, 1), linestyle='dashed')
144 | >>> v.get_patch_by_id('100').set_alpha(1.0)
145 | >>> v.get_patch_by_id('100').set_color('white')
146 | >>> v.get_label_by_id('100').set_text('Unknown')
147 | >>> v.get_label_by_id('C').set_text('Set C')
148 |
149 | You can provide sets themselves rather than subset sizes:
150 | >>> v = venn3(subsets=[set([1,2]), set([2,3,4,5]), set([4,5,6,7,8,9,10,11])])
151 | >>> print("%0.2f %0.2f %0.2f" % (v.get_circle_radius(0), v.get_circle_radius(1)/v.get_circle_radius(0), v.get_circle_radius(2)/v.get_circle_radius(0)))
152 | 0.24 1.41 2.00
153 | >>> c = venn3_circles(subsets=[set([1,2]), set([2,3,4,5]), set([4,5,6,7,8,9,10,11])])
154 | """
155 | # Prepare parameters
156 | if isinstance(subsets, dict):
157 | subsets = [
158 | subsets.get(t, 0) for t in ["100", "010", "110", "001", "101", "011", "111"]
159 | ]
160 | elif len(subsets) == 3:
161 | subsets = _compute_subset_sizes(*subsets)
162 |
163 | if normalize_to is not None:
164 | if layout_algorithm is None:
165 | warnings.warn(
166 | "normalize_to is deprecated. Please use layout_algorithm=matplotlib_venn.layout.venn3.DefaultLayoutAlgorithm(normalize_to) instead."
167 | )
168 | else:
169 | raise ValueError(
170 | "normalize_to is deprecated and may not be specified together with a custom layout algorithm."
171 | )
172 |
173 | if layout_algorithm is None:
174 | layout_algorithm = DefaultLayoutAlgorithm(normalize_to=normalize_to or 1.0)
175 |
176 | layout = layout_algorithm(subsets, set_labels)
177 | return _render_layout(
178 | layout, subsets, set_labels, set_colors, alpha, ax, subset_label_formatter
179 | )
180 |
181 |
182 | def _render_layout(
183 | layout: VennLayout,
184 | subsets: Venn3SubsetSizes,
185 | set_labels: Optional[Tuple[str, str, str]] = ("A", "B", "C"),
186 | set_colors: Tuple[Any, Any, Any] = ("r", "g", "b"),
187 | alpha: float = 0.4,
188 | ax: Optional[Axes] = None,
189 | subset_label_formatter: Optional[Callable[[float], str]] = None,
190 | ) -> VennDiagram:
191 | """Given a VennLayout and the relevant rendering information, generates the diagram."""
192 | if subset_label_formatter is None:
193 | subset_label_formatter = str
194 | if ax is None:
195 | ax = gca()
196 | prepare_venn_axes(ax, layout.centers, layout.radii)
197 | colors = _compute_colors(*set_colors)
198 | regions = list(_compute_regions(layout.centers, layout.radii))
199 |
200 | # Remove regions that are too small from the diagram
201 | MIN_REGION_SIZE = 1e-4
202 | for i in range(len(regions)):
203 | if regions[i].size() < MIN_REGION_SIZE and subsets[i] == 0:
204 | regions[i] = VennEmptyRegion()
205 |
206 | # There is a rare case (Issue #12) when the middle region is visually empty
207 | # (the positioning of the circles does not let them intersect), yet the corresponding value is not 0.
208 | # we address it separately here by positioning the label of that empty region in a custom way
209 | if isinstance(regions[6], VennEmptyRegion) and subsets[6] > 0:
210 | intersections = [
211 | circle_circle_intersection(
212 | layout.centers[i].asarray(),
213 | layout.radii[i] + 0.001,
214 | layout.centers[j].asarray(),
215 | layout.radii[j] + 0.001,
216 | )
217 | for (i, j) in [(0, 1), (1, 2), (2, 0)]
218 | ]
219 | middle_pos = np.mean([i[0] for i in intersections], 0)
220 | regions[6] = VennEmptyRegion(middle_pos)
221 |
222 | # Create and add patches and text
223 | patches = [r.make_patch() for r in regions]
224 | for p, c in zip(patches, colors):
225 | if p is not None:
226 | p.set_facecolor(c)
227 | p.set_edgecolor("none")
228 | p.set_alpha(alpha)
229 | ax.add_patch(p)
230 | label_positions = [r.label_position() for r in regions]
231 | subset_labels = [
232 | (
233 | ax.text(lbl[0], lbl[1], subset_label_formatter(s), va="center", ha="center")
234 | if lbl is not None
235 | else None
236 | )
237 | for (lbl, s) in zip(label_positions, subsets)
238 | ]
239 |
240 | # Position set labels
241 | if set_labels is not None:
242 | labels = [
243 | ax.text(lbl.position.x, lbl.position.y, txt, size="large", **lbl.kwargs)
244 | for (lbl, txt) in zip(layout.set_labels_layout, set_labels)
245 | ]
246 | else:
247 | labels = None
248 | return VennDiagram(patches, subset_labels, labels, layout.centers, layout.radii)
249 |
250 |
251 | def _compute_regions(
252 | centers: Tuple[Point2D, Point2D, Point2D], radii: Tuple[float, float, float]
253 | ) -> Tuple[
254 | VennRegion, VennRegion, VennRegion, VennRegion, VennRegion, VennRegion, VennRegion
255 | ]:
256 | """
257 | Given the three centers and radii of circles, returns the 7 regions, comprising the venn diagram, as VennRegion objects.
258 |
259 | Regions are returned in order (Abc, aBc, ABc, abC, AbC, aBC, ABC)
260 |
261 | >>> layout = DefaultLayoutAlgorithm()((1, 1, 1, 1, 1, 1, 1))
262 | >>> regions = _compute_regions(layout.centers, layout.radii)
263 | """
264 | A = VennCircleRegion(centers[0].asarray(), radii[0])
265 | B = VennCircleRegion(centers[1].asarray(), radii[1])
266 | C = VennCircleRegion(centers[2].asarray(), radii[2])
267 | Ab, AB = A.subtract_and_intersect_circle(B.center, B.radius)
268 | ABc, ABC = AB.subtract_and_intersect_circle(C.center, C.radius)
269 | Abc, AbC = Ab.subtract_and_intersect_circle(C.center, C.radius)
270 | aB, _ = B.subtract_and_intersect_circle(A.center, A.radius)
271 | aBc, aBC = aB.subtract_and_intersect_circle(C.center, C.radius)
272 | aC, _ = C.subtract_and_intersect_circle(A.center, A.radius)
273 | abC, _ = aC.subtract_and_intersect_circle(B.center, B.radius)
274 | return (Abc, aBc, ABc, abC, AbC, aBC, ABC)
275 |
276 |
277 | def _compute_colors(
278 | color_a: Any, color_b: Any, color_c: Any
279 | ) -> Tuple[
280 | np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray
281 | ]:
282 | """
283 | Given three base colors, computes combinations of colors corresponding to all regions of the venn diagram.
284 | returns a list of 7 elements, providing colors for regions (100, 010, 110, 001, 101, 011, 111).
285 |
286 | >>> str(_compute_colors('r', 'g', 'b')).replace(' ', '')
287 | '(array([1.,0.,0.]),...,array([0.4,0.2,0.4]))'
288 | """
289 | ccv = ColorConverter()
290 | base_colors = [np.array(ccv.to_rgb(c)) for c in [color_a, color_b, color_c]]
291 | return (
292 | base_colors[0],
293 | base_colors[1],
294 | mix_colors(base_colors[0], base_colors[1]),
295 | base_colors[2],
296 | mix_colors(base_colors[0], base_colors[2]),
297 | mix_colors(base_colors[1], base_colors[2]),
298 | mix_colors(base_colors[0], base_colors[1], base_colors[2]),
299 | )
300 |
301 |
302 | def _compute_subset_sizes(
303 | a: Union[set, Counter], b: Union[set, Counter], c: Union[set, Counter]
304 | ) -> Venn3SubsetSizes:
305 | """
306 | Given three set or Counter objects, computes the sizes of (a & ~b & ~c, ~a & b & ~c, a & b & ~c, ....),
307 | as needed by the subsets parameter of venn3 and venn3_circles.
308 | Returns the result as a tuple.
309 |
310 | >>> _compute_subset_sizes(set([1,2,3]), set([2,3,4]), set([3,4,5,6]))
311 | (1, 0, 1, 2, 0, 1, 1)
312 | >>> _compute_subset_sizes(Counter([1,2,3]), Counter([2,3,4]), Counter([3,4,5,6]))
313 | (1, 0, 1, 2, 0, 1, 1)
314 | >>> _compute_subset_sizes(Counter([1,1,1]), Counter([1,1,1]), Counter([1,1,1,1]))
315 | (0, 0, 0, 1, 0, 0, 3)
316 | >>> _compute_subset_sizes(Counter([1,1,2,2,3,3]), Counter([2,2,3,3,4,4]), Counter([3,3,4,4,5,5,6,6]))
317 | (2, 0, 2, 4, 0, 2, 2)
318 | >>> _compute_subset_sizes(Counter([1,2,3]), Counter([2,2,3,3,4,4]), Counter([3,3,4,4,4,5,5,6]))
319 | (1, 1, 1, 4, 0, 3, 1)
320 | >>> _compute_subset_sizes(set([]), set([]), set([]))
321 | (0, 0, 0, 0, 0, 0, 0)
322 | >>> _compute_subset_sizes(set([1]), set([]), set([]))
323 | (1, 0, 0, 0, 0, 0, 0)
324 | >>> _compute_subset_sizes(set([]), set([1]), set([]))
325 | (0, 1, 0, 0, 0, 0, 0)
326 | >>> _compute_subset_sizes(set([]), set([]), set([1]))
327 | (0, 0, 0, 1, 0, 0, 0)
328 | >>> _compute_subset_sizes(Counter([]), Counter([]), Counter([1]))
329 | (0, 0, 0, 1, 0, 0, 0)
330 | >>> _compute_subset_sizes(set([1]), set([1]), set([1]))
331 | (0, 0, 0, 0, 0, 0, 1)
332 | >>> _compute_subset_sizes(set([1,3,5,7]), set([2,3,6,7]), set([4,5,6,7]))
333 | (1, 1, 1, 1, 1, 1, 1)
334 | >>> _compute_subset_sizes(Counter([1,3,5,7]), Counter([2,3,6,7]), Counter([4,5,6,7]))
335 | (1, 1, 1, 1, 1, 1, 1)
336 | >>> _compute_subset_sizes(Counter([1,3,5,7]), set([2,3,6,7]), set([4,5,6,7]))
337 | Traceback (most recent call last):
338 | ...
339 | ValueError: All arguments must be of the same type
340 | """
341 | if not (type(a) == type(b) == type(c)):
342 | raise ValueError("All arguments must be of the same type")
343 | set_size = (
344 | len if type(a) != Counter else lambda x: sum(x.values())
345 | ) # We cannot use len to compute the cardinality of a Counter
346 | return (
347 | set_size(
348 | a - (b | c)
349 | ), # TODO: This is certainly not the most efficient way to compute.
350 | set_size(b - (a | c)),
351 | set_size((a & b) - c),
352 | set_size(c - (a | b)),
353 | set_size((a & c) - b),
354 | set_size((b & c) - a),
355 | set_size(a & b & c),
356 | )
357 |
--------------------------------------------------------------------------------
/matplotlib_venn/layout/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/konstantint/matplotlib-venn/7a69a549579faa0286c8de57f6cc3216a2ac4f5e/matplotlib_venn/layout/__init__.py
--------------------------------------------------------------------------------
/matplotlib_venn/layout/api.py:
--------------------------------------------------------------------------------
1 | """
2 | Specification of the "layout algorithm" interface.
3 |
4 | A layout algorithm is a method for mapping from subset sizes to circle centers, radii and label locations.
5 |
6 | Copyright 2024, Konstantin Tretyakov.
7 | http://kt.era.ee/
8 |
9 | Licensed under MIT license.
10 | """
11 |
12 | from typing import Any, Dict, Sequence, Optional
13 | from abc import ABC, abstractmethod
14 | from matplotlib_venn._math import Point2D
15 |
16 | SubsetSizes = Sequence[float] # .. of length 3 for venn2 and length 7 for venn3
17 |
18 | # Failures that may be reported from the layout algorithm.
19 | class LayoutException(Exception):
20 | pass
21 |
22 | class LabelLayout:
23 | """Text label position in the diagram.
24 |
25 | Given via coordinates and a set of keyword arguments (e.g. "ha" or "va").
26 | """
27 |
28 | def __init__(self, position: Point2D, kwargs: Dict[str, Any]):
29 | self.position = position
30 | self.kwargs = kwargs
31 |
32 |
33 | class VennLayout:
34 | """The circle layout specification for a Venn diagram."""
35 |
36 | # Centers of the (2 / 3) circles (in the Axes coordinates).
37 | # centers: Sequence[Point2D]
38 | # # Radii of the circles.
39 | # radii: Sequence[float]
40 | # # Layout information of set labels. If labels are missing, then None.
41 | # set_labels_layout: Optional[Sequence[LabelLayout]] = None
42 |
43 | def __init__(
44 | self,
45 | centers: Sequence[Point2D],
46 | radii: Sequence[float],
47 | set_labels_layout: Optional[Sequence[LabelLayout]] = None,
48 | ):
49 | self.centers = centers
50 | self.radii = radii
51 | self.set_labels_layout = set_labels_layout
52 |
53 |
54 | class VennLayoutAlgorithm(ABC):
55 | """Interface for a Venn layout algorithm."""
56 |
57 | @abstractmethod
58 | def __call__(
59 | self,
60 | subsets: SubsetSizes,
61 | set_labels: Optional[Sequence[str]] = None,
62 | ) -> VennLayout:
63 | """Lay out the Venn circles, returning the diagram layout specification as VennLayout.
64 | Args:
65 | subsets: A tuple with 3 (for venn2) or 7 (for venn3) numbers, denoting the sizes of the
66 | Venn diagram regions in the following order:
67 | for venn2: (10, 01, 11)
68 | for venn3: (100, 010, 110, 001, 101, 011, 111).
69 | set_labels: Optional tuple of set labels. If None, resulting layout provides no label information.
70 | """
71 | pass
72 |
--------------------------------------------------------------------------------
/matplotlib_venn/layout/venn2/__init__.py:
--------------------------------------------------------------------------------
1 | from matplotlib_venn.layout.venn2.exact import LayoutAlgorithm as DefaultLayoutAlgorithm
2 |
3 | __all__ = ["DefaultLayoutAlgorithm"]
4 |
--------------------------------------------------------------------------------
/matplotlib_venn/layout/venn2/exact.py:
--------------------------------------------------------------------------------
1 | """
2 | The exact area-weighted layout algorithm implementation.
3 | This is the default, original layout method.
4 |
5 | Copyright 2012-2024, Konstantin Tretyakov.
6 | http://kt.era.ee/
7 |
8 | Licensed under MIT license.
9 | """
10 |
11 | from typing import Optional, Sequence
12 | import warnings
13 | import numpy as np
14 |
15 | from matplotlib_venn._math import (
16 | NUMERIC_TOLERANCE,
17 | Point2D,
18 | find_distance_by_area,
19 | normalize_by_center_of_mass,
20 | )
21 | from matplotlib_venn.layout.api import (
22 | LabelLayout,
23 | VennLayout,
24 | VennLayoutAlgorithm,
25 | SubsetSizes,
26 | )
27 |
28 | # The format is the same but the semantics is different.
29 | VennAreas = SubsetSizes
30 |
31 |
32 | class LayoutAlgorithm(VennLayoutAlgorithm):
33 | def __init__(
34 | self,
35 | normalize_to: float = 1.0,
36 | fixed_subset_sizes: Optional[SubsetSizes] = None,
37 | ):
38 | """Initialize the layout algorithm.
39 |
40 | Args:
41 | normalize_to: Specifies the total (on-axes) area of the circles to be drawn. Sometimes tuning it (together
42 | with the overall figure size) can be useful to fit the text labels better.
43 | fixed_subset_sizes: If specified, the layout will always use these subset sizes, ignoring anything provided
44 | to the actual __call__. E.g. passing (1,1,1) here will result in a non-area-weighted layout algorithm.
45 | """
46 | self._normalize_to = normalize_to
47 | self._fixed_subset_sizes = fixed_subset_sizes
48 |
49 | def __call__(
50 | self,
51 | subsets: SubsetSizes,
52 | set_labels: Optional[Sequence[str]] = None, # Not used in the layout algorithm
53 | ) -> VennLayout:
54 | if self._fixed_subset_sizes is not None:
55 | subsets = self._fixed_subset_sizes
56 | areas = _compute_areas(subsets, self._normalize_to)
57 | return _compute_layout(areas)
58 |
59 |
60 | def _compute_areas(
61 | subset_sizes: SubsetSizes, normalize_to: float = 1.0, _minimal_area: float = 1e-6
62 | ) -> VennAreas:
63 | """
64 | Convert the sizes of individual regions (Ab, aB, AB) into areas (A, B, AB), used to lay out the diagram,
65 | normalizing the areas to sum to a given number.
66 |
67 | If total area was 0, returns (1e-06, 1e-06, 0.0)
68 |
69 | Assumes all input values are nonnegative (to be more precise, all areas are passed through and abs() function)
70 | >>> _compute_areas((1, 1, 0))
71 | (0.5, 0.5, 0.0)
72 | >>> _compute_areas((0, 0, 0))
73 | (1e-06, 1e-06, 0.0)
74 | >>> _compute_areas((1, 1, 1), normalize_to=3)
75 | (2.0, 2.0, 1.0)
76 | >>> _compute_areas((1, 2, 3), normalize_to=6)
77 | (4.0, 5.0, 3.0)
78 | """
79 | # Normalize input values to sum to 1
80 | areas = np.array(np.abs(subset_sizes), float)
81 | total_area = np.sum(areas)
82 | if abs(total_area) < NUMERIC_TOLERANCE:
83 | warnings.warn("Both circles have zero area")
84 | return (1e-06, 1e-06, 0.0)
85 | else:
86 | areas = areas / total_area * normalize_to
87 | return (float(areas[0] + areas[2]), float(areas[1] + areas[2]), float(areas[2]))
88 |
89 |
90 | def _compute_layout(venn_areas: VennAreas) -> VennLayout:
91 | """
92 | Given the list of "venn areas" (as output from compute_venn2_areas, i.e. [A, B, AB]),
93 | finds the positions and radii of the two circles.
94 |
95 | Assumes the input values to be nonnegative and not all zero.
96 | In particular, the first two values must be positive.
97 |
98 | >>> layout = _compute_layout((1, 1, 0))
99 | >>> np.round(layout.radii, 3).tolist()
100 | [0.564, 0.564]
101 | >>> layout = _compute_layout(_compute_areas((1, 2, 3)))
102 | >>> np.round(layout.radii, 3).tolist()
103 | [0.461, 0.515]
104 | """
105 | (A_a, A_b, A_ab) = list(map(float, venn_areas))
106 | r_a, r_b = np.sqrt(A_a / np.pi), np.sqrt(A_b / np.pi)
107 | radii = np.array([r_a, r_b])
108 | if A_ab > NUMERIC_TOLERANCE:
109 | # Nonzero intersection
110 | coords = np.zeros((2, 2))
111 | coords[1][0] = find_distance_by_area(radii[0], radii[1], A_ab)
112 | else:
113 | # Zero intersection
114 | coords = np.zeros((2, 2))
115 | coords[1][0] = (
116 | radii[0] + radii[1] + max(np.mean(radii) * 1.1, 0.2)
117 | ) # The max here is needed for the case r_a = r_b = 0
118 | coords = normalize_by_center_of_mass(coords, radii)
119 | layout = VennLayout(
120 | (Point2D(*coords[0]), Point2D(*coords[1])), (radii[0], radii[1])
121 | )
122 | _compute_set_labels_positions(layout)
123 | return layout
124 |
125 |
126 | def _compute_set_labels_positions(layout: VennLayout):
127 | """Updates the set_labels_positions field of the given layout object."""
128 | padding = np.mean([r * 0.1 for r in layout.radii])
129 | layout.set_labels_layout = (
130 | LabelLayout(
131 | position=layout.centers[0] + Point2D(0.0, -layout.radii[0] - padding),
132 | kwargs={"ha": "right", "va": "top"},
133 | ),
134 | LabelLayout(
135 | position=layout.centers[1] + Point2D(0.0, -layout.radii[1] - padding),
136 | kwargs={"ha": "left", "va": "top"},
137 | ),
138 | )
139 |
--------------------------------------------------------------------------------
/matplotlib_venn/layout/venn3/__init__.py:
--------------------------------------------------------------------------------
1 | from matplotlib_venn.layout.venn3.pairwise import (
2 | LayoutAlgorithm as DefaultLayoutAlgorithm,
3 | )
4 |
5 | __all__ = ["DefaultLayoutAlgorithm"]
6 |
--------------------------------------------------------------------------------
/matplotlib_venn/layout/venn3/cost_based.py:
--------------------------------------------------------------------------------
1 | """
2 | Cost optimization-based layout algorithm for 3-way Venn diagrams.
3 |
4 | Unlike the rest of the code in the package, this implementation depends on the shapely library.
5 | To include the dependency, the library should be installed as
6 |
7 | ```
8 | pip install 'matplotlib-venn[shapely]'
9 | ```
10 |
11 | (Shapely will quite probably become a core dependency in a future version).
12 |
13 | Usage
14 | -----
15 |
16 | This layout algorithm makes most sense in the cases when the default, "pairwise" layout
17 | does not work well enough for your data (which is usually the case for very skewed subset sizes).
18 |
19 | In this case just try doing:
20 |
21 | >>> from matplotlib_venn.layout.venn3 import cost_based
22 | >>> from matplotlib_venn import venn3
23 | >>> subset_sizes = (100,200,10000,10,20,3,1)
24 | >>> venn3(subset_sizes, layout_algorithm=cost_based.LayoutAlgorithm())
25 |
26 |
27 | You may further tune the behaviour of the algorithm by redefining the cost function.
28 | By default the algorithm tries to optimize the sum of |log(1+target_size)-log(1+actual_size)|
29 | over all 7 regions. If for some reason you believe |target_size - actual_size| should work better
30 | for your case, you can achieve it as follows:
31 |
32 | >>> alg = cost_based.LayoutAlgorithm(cost_fn=cost_based.WeightedAggregateCost(transform_fn=lambda x: x))
33 | >>> venn3(subset_sizes, layout_algorithm=alg)
34 |
35 |
36 | Alternatively, you may want the optimization to give more weight to some of the regions or even ignore some of the
37 | larger ones.
38 |
39 | >>> alg = cost_based.LayoutAlgorithm(cost_fn=cost_based.WeightedAggregateCost(weights=(0,0,0,1,1,1,1)))
40 | >>> venn3(subset_sizes, layout_algorithm=alg)
41 |
42 |
43 | In theory, if the cost is defined as a difference in sizes of "pairwise" regions (AB, BC, AC), the result of optimizing
44 | it should be equivalent to what the default ('pairwise') algorithm does. To play with this idea, the module defines the
45 | respective `pairwise_cost` function. The result is not exactly the same as that of the default algorithm, but it would
46 | nearly always succeed, even when the default algorithm sometimes fails. E.g.:
47 |
48 | >>> subset_sizes = (1, 0, 0, 650, 0, 76, 13)
49 | >>> # Fails
50 | >>> venn3(subset_sizes) # doctest: +IGNORE_EXCEPTION_DETAIL
51 | Traceback (most recent call last):
52 | matplotlib_venn._region.VennRegionException: Invalid configuration of circular regions (holes are not supported).
53 |
54 | >>> # Succeeds, producing what the default algorithm should have produced
55 | >>> venn3(subset_sizes, layout_algorithm=cost_based.LayoutAlgorithm(cost_fn=cost_based.pairwise_cost))
56 |
57 |
58 | NB: This implementation is still in "alpha" stage, the code and behaviour may change in backwards-incompatible ways.
59 |
60 | Copyright 2024, Konstantin Tretyakov.
61 | http://kt.era.ee/
62 |
63 | Based on a prototype by Paul Brodersen (https://github.com/konstantint/matplotlib-venn/issues/35).
64 |
65 | Licensed under MIT license.
66 | """
67 |
68 | from typing import Callable, Optional, Sequence
69 | import warnings
70 | import numpy as np
71 | from shapely.geometry import Point
72 | from scipy.optimize import minimize, NonlinearConstraint
73 | from scipy.spatial.distance import pdist
74 | from matplotlib_venn._math import NUMERIC_TOLERANCE
75 | from matplotlib_venn.layout.venn3 import pairwise
76 | from matplotlib_venn.layout.api import (
77 | LayoutException,
78 | Point2D,
79 | SubsetSizes,
80 | VennLayout,
81 | VennLayoutAlgorithm,
82 | )
83 |
84 |
85 | def _initialize_centers(radii: np.ndarray) -> np.ndarray:
86 | """Initialize centers on a small circle around (0, 0).
87 |
88 | The centers are positioned at 90+60, 90-60, -90, matching the
89 | positioning logic of the pairwise algorithm.
90 |
91 | >>> centers = _initialize_centers(np.asarray([1, 2, 3]))
92 | >>> np.allclose(centers, \
93 | np.array([[-0.866, 0.5], \
94 | [ 0.866, 0.5], \
95 | [ 0, -1]]), atol=0.001)
96 | True
97 | """
98 | angles = 2 * np.pi * np.array([1 / 12 + 1 / 3, 1 / 12, 1 / 12 + 2 / 3])
99 | return np.vstack([np.cos(angles), np.sin(angles)]).T * np.min(radii)
100 |
101 |
102 | def _normalize_subset_sizes(
103 | subset_sizes: SubsetSizes, normalize_to: float = 1.0
104 | ) -> SubsetSizes:
105 | """Normalize provided subset sizes to areas with total area equal to .
106 | If the total area is less than _minimal_area falls back to a normalized version of the
107 | 'unweighted' set of areas (1,1,1,1,1,1,1).
108 |
109 | >>> _normalize_subset_sizes((1,0,0,0,0,0,0))
110 | array([1., 0., 0., 0., 0., 0., 0.])
111 | >>> _normalize_subset_sizes((1,1,0,0,0,0,0))
112 | array([0.5, 0.5, 0. , 0. , 0. , 0. , 0. ])
113 | >>> _normalize_subset_sizes((0,0,0,0,0,0,0))
114 | array([0.14..., 0.14..., 0.14..., 0.14..., 0.14..., 0.14..., 0.14...])
115 | """
116 | areas = np.array(np.abs(subset_sizes), float)
117 | total_area = np.sum(areas)
118 | if np.abs(total_area) < NUMERIC_TOLERANCE:
119 | warnings.warn(
120 | "All regions have zero area. Falling back to an unweighted diagram."
121 | )
122 | return _normalize_subset_sizes((1, 1, 1, 1, 1, 1, 1), normalize_to)
123 | else:
124 | return areas / total_area * normalize_to
125 |
126 |
127 | def _compute_radii(areas: SubsetSizes) -> np.ndarray:
128 | """Compute radii of the three circles based on a given SubsetSizes vector.
129 |
130 | Returns an array of three radii for the three circles.
131 |
132 | >>> regions = np.pi*np.array([1, 2, 0, 3, 0, 0, 0])**2
133 | >>> _compute_radii(regions)
134 | array([1., 2., 3.])
135 | >>> deltas = np.array([-0.3, -0.3, 0.1, -0.3, 0.1, 0.1, 0.1])
136 | >>> _compute_radii(regions + deltas)
137 | array([1., 2., 3.])
138 | """
139 | (Abc, aBc, ABc, abC, AbC, aBC, ABC) = areas
140 | A = Abc + ABc + AbC + ABC
141 | B = aBc + ABc + aBC + ABC
142 | C = abC + AbC + aBC + ABC
143 | return np.sqrt(np.array([A, B, C]) / np.pi)
144 |
145 |
146 | def _compute_subset_areas(centers: np.ndarray, radii: np.ndarray) -> SubsetSizes:
147 | """Given centers and radii of a venn3 diagram, return the respective subset areas.
148 |
149 | >>> areas = _compute_subset_areas(np.asarray([[0,0], [2,2], [4,4]]), np.asarray([1, 1, 1]))
150 | >>> np.allclose(areas, [np.pi, np.pi, 0, np.pi, 0, 0, 0], atol=0.01)
151 | True
152 | >>> from matplotlib_venn.layout.venn3 import DefaultLayoutAlgorithm
153 | >>> layout = DefaultLayoutAlgorithm()((1,1,1,1,1,1,1))
154 | >>> areas = _compute_subset_areas(\
155 | np.asarray([c.asarray() for c in layout.centers]),\
156 | np.asarray(layout.radii))
157 | >>> layout = DefaultLayoutAlgorithm()(areas)
158 | >>> new_areas = _compute_subset_areas(\
159 | np.asarray([c.asarray() for c in layout.centers]),\
160 | np.asarray(layout.radii))
161 | >>> np.allclose(areas, new_areas, atol=0.01)
162 | True
163 | """
164 | a, b, c = [Point(*center).buffer(radius) for center, radius in zip(centers, radii)]
165 | regions = [
166 | a.difference(b).difference(c), # Abc
167 | b.difference(a).difference(c), # aBc
168 | a.intersection(b).difference(c), # ABc
169 | c.difference(a).difference(b), # abC
170 | a.intersection(c).difference(b), # AbC
171 | b.intersection(c).difference(a), # aBC
172 | a.intersection(b).intersection(c), # ABC
173 | ]
174 | return np.array([region.area for region in regions])
175 |
176 |
177 | # A cost function is a callable that accepts the desired subset sizes
178 | # and the actual sizes (for the current layout) and returns the cost ("loss")
179 | # of the discrepancy.
180 | CostFunction = Callable[[SubsetSizes, SubsetSizes], float]
181 |
182 |
183 | class WeightedAggregateCost(CostFunction):
184 | """A cost function that aggregates differences over all regions.
185 |
186 | The function computes:
187 | np.dot(weights, np.abs(fn(target_size) - fn(current_size))**power).
188 |
189 | >>> fn = WeightedAggregateCost()
190 | >>> fn([1]*7, [1]*7)
191 | 0.0
192 | >>> fn([1]*7, [0]*7)
193 | 7.0
194 | >>> fn = WeightedAggregateCost(lambda x: x**2)
195 | >>> fn([1]*7, [1]*7)
196 | 0.0
197 | >>> fn([2]*7, [0]*7)
198 | 28.0
199 | >>> fn = WeightedAggregateCost(weights=(1,2,3))
200 | >>> fn([1,2,0], [0,0,3])
201 | 14.0
202 | >>> fn = WeightedAggregateCost(weights=(0,0,1), power=2)
203 | >>> fn([1,2,0], [0,0,3])
204 | 9.0
205 | """
206 |
207 | def __init__(
208 | self,
209 | transform_fn: Callable[[np.ndarray], np.ndarray] = lambda x: x,
210 | weights: Sequence[float] = (1, 1, 1, 1, 1, 1, 1),
211 | power: float = 1,
212 | ):
213 | self.transform_fn = transform_fn
214 | self.weights = np.asarray(weights)
215 | self.power = power
216 |
217 | def __call__(self, target_areas: SubsetSizes, current_areas: SubsetSizes) -> float:
218 | targets = self.transform_fn(np.asarray(target_areas))
219 | current = self.transform_fn(np.asarray(current_areas))
220 | return float(np.dot(self.weights, np.abs(targets - current) ** self.power))
221 |
222 |
223 | def pairwise_cost(target_areas: SubsetSizes, actual_areas: SubsetSizes) -> float:
224 | """The cost, computed as the absolute difference between pairwise (A&B, B&C, A&C) areas.
225 |
226 | This matches the logic of the default ("pairwise") layout algorithm and thus
227 | produces mostly the same results (not exactly the same due to some randomness
228 | involved in the iterative nature of the optimization).
229 |
230 | It is here primarily for experimentation and "completeness' sake".
231 |
232 | >>> pairwise_cost([1]*7, [1]*7)
233 | 0.0
234 | >>> pairwise_cost([1]*7, (2,2,1,2,1,1,1))
235 | 0.0
236 | >>> pairwise_cost([1]*7, (2,2,1,2,1,1,1.5))
237 | 1.5
238 | >>> pairwise_cost([1]*7, (2,2,1.5,2,1,1,1))
239 | 0.5
240 | >>> pairwise_cost([1]*7, (2,2,1.5,2,1.1,1,1))
241 | 0.6...
242 | """
243 | (tAbc, taBc, tABc, tabC, tAbC, taBC, tABC) = target_areas
244 | (Abc, aBc, ABc, abC, AbC, aBC, ABC) = actual_areas
245 | dAB = tABC + tABc - (ABC + ABc)
246 | dBC = tABC + taBC - (ABC + aBC)
247 | dAC = tABC + tAbC - (ABC + AbC)
248 | return float(abs(dAB) + abs(dBC) + abs(dAC))
249 |
250 |
251 | class LayoutAlgorithm(VennLayoutAlgorithm):
252 | """3-way Venn layout that positions circles by numerically optimizing a given discrepancy cost.
253 |
254 | >>> alg = LayoutAlgorithm()
255 | >>> layout = alg((1,1,1,1,1,1,1), ("A", "B", "C"))
256 | >>> layout.radii
257 | [0.42..., 0.42..., 0.42...]
258 | >>> np.allclose(\
259 | np.asarray([c.asarray() for c in layout.centers]),\
260 | np.asarray([[-0.134, 0.077], [0.134, 0.077], [0, -0.154]]), atol=0.001\
261 | )
262 | True
263 | """
264 |
265 | def __init__(self, cost_fn: Optional[CostFunction] = None, fallback: bool = True):
266 | """Initialize the cost-based layout algorithm.
267 |
268 | Args:
269 | cost_fn: A cost function to be optimized.
270 | Default is WeightedAggregateCost(lambda x: np.log(1 + x)).
271 | This has been determined to work well enough in practice.
272 | fallback: Whether to fall back to the default ("pairwise") layout
273 | algorithm if optimization does not converge. True by default.
274 | If there is no fallback, a LayoutException will be raised if
275 | optimization fails.
276 | """
277 | self._cost_fn = cost_fn or WeightedAggregateCost(lambda x: np.log(1 + x))
278 | self._fallback = fallback
279 | # This is a convenience field that will carry the result of the most
280 | # recent "minimize" call.
281 | self.last_optimization_result = None
282 |
283 | def __call__(
284 | self,
285 | subsets: SubsetSizes,
286 | set_labels: Optional[Sequence[str]] = None,
287 | ) -> VennLayout:
288 | target_areas = _normalize_subset_sizes(subsets)
289 | radii = _compute_radii(target_areas)
290 | centers = _initialize_centers(radii)
291 |
292 | # We will position the circles by optimizing this cost function ...
293 | def _cost_function(centers_flattened: np.ndarray) -> np.ndarray:
294 | """Computes the cost of positioning circles at given centers."""
295 | current_areas = _compute_subset_areas(
296 | centers_flattened.reshape(-1, 2), radii
297 | )
298 | return self._cost_fn(target_areas, current_areas)
299 |
300 | # ... while making sure the pairwise distances between circles do not exceed sum of radii:
301 | # (this is the order in which pdist computes pairwise distances).
302 | upper_bounds = np.array(
303 | [radii[0] + radii[1], radii[0] + radii[2], radii[1] + radii[2]]
304 | )
305 |
306 | # ... and are not below differences between radii:
307 | lower_bounds = np.abs(
308 | np.array([radii[0] - radii[1], radii[0] - radii[2], radii[1] - radii[2]])
309 | )
310 |
311 | def _pairwise_distances(centers_flattened: np.ndarray) -> np.ndarray:
312 | return pdist(np.reshape(centers_flattened, (-1, 2)))
313 |
314 | result = minimize(
315 | _cost_function,
316 | centers.flatten(),
317 | method="SLSQP",
318 | constraints=[
319 | NonlinearConstraint(
320 | _pairwise_distances, ub=upper_bounds, lb=lower_bounds
321 | )
322 | ],
323 | )
324 | self.last_optimization_result = result
325 |
326 | if not result.success:
327 | warnings.warn("Optimization failed: {0}".format(result.message))
328 | if self._fallback:
329 | # Fall back to _pairwise
330 | return pairwise.LayoutAlgorithm()(subsets, set_labels)
331 | else:
332 | raise LayoutException("Optimization failed: {0}".format(result.message))
333 |
334 | centers = result.x.reshape((-1, 2))
335 | result = VennLayout(
336 | centers=[Point2D(*center) for center in centers],
337 | radii=list(map(float, radii)),
338 | )
339 | # TODO: We reuse the pairwise algorithm implementation for set label positioning.
340 | # It does not always do the most correct job.
341 | pairwise._compute_set_labels_positions(result)
342 | return result
343 |
--------------------------------------------------------------------------------
/matplotlib_venn/layout/venn3/pairwise.py:
--------------------------------------------------------------------------------
1 | """
2 | The pairwise intersection-based layout algorithm implementation.
3 | This is the default, original layout method.
4 |
5 | Makes sure the full circle areas and the areas of their pairwise intersections exactly match the subset areas.
6 | The area of the triple intersection is not necessarily correct.
7 |
8 | For situations where the triple intersection is too small in comparison to other areas it often results in bad layout.
9 |
10 | Copyright 2012-2024, Konstantin Tretyakov.
11 | http://kt.era.ee/
12 |
13 | Licensed under MIT license.
14 | """
15 |
16 | from typing import Optional, Tuple
17 | import warnings
18 | import numpy as np
19 |
20 | from matplotlib_venn._math import (
21 | NUMERIC_TOLERANCE,
22 | Point2D,
23 | find_distance_by_area,
24 | normalize_by_center_of_mass,
25 | )
26 | from matplotlib_venn.layout.api import (
27 | LabelLayout,
28 | VennLayout,
29 | VennLayoutAlgorithm,
30 | SubsetSizes,
31 | )
32 |
33 | # The format is the same but the semantics is different.
34 | VennAreas = SubsetSizes
35 |
36 |
37 | class LayoutAlgorithm(VennLayoutAlgorithm):
38 | def __init__(
39 | self,
40 | normalize_to: float = 1.0,
41 | fixed_subset_sizes: Optional[SubsetSizes] = None,
42 | ):
43 | """Initialize the layout algorithm.
44 |
45 | Args:
46 | normalize_to: Specifies the total (on-axes) area of the circles to be drawn. Sometimes tuning it (together
47 | with the overall figure size) can be useful to fit the text labels better.
48 | fixed_subset_sizes: If specified, the layout will always use these subset sizes, ignoring anything provided
49 | to the actual __call__. E.g. passing (1,1,1,1,1,1,1) here will result in a non-area-weighted layout algorithm.
50 | """
51 | self._normalize_to = normalize_to
52 | self._fixed_subset_sizes = fixed_subset_sizes
53 |
54 | def __call__(
55 | self,
56 | subsets: SubsetSizes,
57 | set_labels: Optional[
58 | Tuple[str, str, str]
59 | ] = None, # Not used in the layout algorithm.
60 | ) -> VennLayout:
61 | if self._fixed_subset_sizes is not None:
62 | subsets = self._fixed_subset_sizes
63 | areas = _compute_areas(subsets, self._normalize_to)
64 | return _compute_layout(areas)
65 |
66 |
67 | def _compute_areas(
68 | subset_sizes: SubsetSizes, normalize_to: float = 1.0, _minimal_area: float = 1e-6
69 | ) -> VennAreas:
70 | """
71 | Compute areas of circles and their pairwise and triple intersections.
72 | Assumes all input values are nonnegative (to be more precise, all areas are passed through the abs() function)
73 |
74 | Args:
75 | subset_sizes: The relative sizes of the 7 diagram region in the following order:
76 | (Abc, aBc, ABc, abC, AbC, aBC, ABC)
77 | (i.e. last element corresponds to the size of intersection A&B&C).
78 | normalize_to: Normalize the values so that the total area sums to this value.
79 | _minimal_area: If the area of any circle is smaller than _minimal_area, makes it equal to _minimal_area.
80 | Returns:
81 | A list of areas (A_a, A_b, A_c, A_ab, A_bc, A_ac, A_abc),
82 | such that the total area of all circles is normalized to normalize_to (except corrections for _minimal_area)
83 |
84 | >>> _compute_areas((1, 1, 0, 1, 0, 0, 0))
85 | (0.33..., 0.33..., 0.33..., 0.0, 0.0, 0.0, 0.0)
86 | >>> _compute_areas((0, 0, 0, 0, 0, 0, 0))
87 | (1e-06, 1e-06, 1e-06, 0.0, 0.0, 0.0, 0.0)
88 | >>> _compute_areas((1, 1, 1, 1, 1, 1, 1), normalize_to=7)
89 | (4.0, 4.0, 4.0, 2.0, 2.0, 2.0, 1.0)
90 | >>> _compute_areas((1, 2, 3, 4, 5, 6, 7), normalize_to=56/2)
91 | (16.0, 18.0, 22.0, 10.0, 13.0, 12.0, 7.0)
92 | """
93 | # Normalize input values to sum to 1
94 | areas = np.array(np.abs(subset_sizes), float)
95 | total_area = np.sum(areas)
96 | if abs(total_area) < _minimal_area:
97 | warnings.warn("All circles have zero area.")
98 | return (1e-06, 1e-06, 1e-06, 0.0, 0.0, 0.0, 0.0)
99 | else:
100 | areas = areas / total_area * normalize_to
101 | A_a = areas[0] + areas[2] + areas[4] + areas[6]
102 | if A_a < _minimal_area:
103 | warnings.warn("Circle A has zero area.")
104 | A_a = _minimal_area
105 | A_b = areas[1] + areas[2] + areas[5] + areas[6]
106 | if A_b < _minimal_area:
107 | warnings.warn("Circle B has zero area.")
108 | A_b = _minimal_area
109 | A_c = areas[3] + areas[4] + areas[5] + areas[6]
110 | if A_c < _minimal_area:
111 | warnings.warn("Circle C has zero area.")
112 | A_c = _minimal_area
113 |
114 | # Areas of the three intersections (ab, ac, bc)
115 | A_ab, A_ac, A_bc = areas[2] + areas[6], areas[4] + areas[6], areas[5] + areas[6]
116 |
117 | return tuple(map(float, (A_a, A_b, A_c, A_ab, A_bc, A_ac, areas[6])))
118 |
119 |
120 | def _compute_layout(venn_areas: VennAreas) -> VennLayout:
121 | """
122 | Given the list of "venn areas" (as output from _compute_areas, i.e. (A, B, C, AB, BC, AC, ABC)),
123 | finds the positions and radii of the three circles.
124 | Assumes the input values to be nonnegative and not all zero.
125 | In particular, the first three values must all be positive.
126 |
127 | The return value is a VennLayout struct with just the coords and radii fields.
128 |
129 | The overall match is only approximate (to be precise, what is matched are the areas of the circles and the
130 | three pairwise intersections).
131 |
132 | >>> layout = _compute_layout((1, 1, 1, 0, 0, 0, 0))
133 | >>> np.round(layout.radii, 3).tolist()
134 | [0.564, 0.564, 0.564]
135 | >>> layout = _compute_layout(_compute_areas((1, 2, 40, 30, 4, 40, 4)))
136 | >>> np.round(layout.radii, 3).tolist()
137 | [0.359, 0.476, 0.453]
138 | """
139 | (A_a, A_b, A_c, A_ab, A_bc, A_ac, A_abc) = list(map(float, venn_areas))
140 | r_a, r_b, r_c = np.sqrt(A_a / np.pi), np.sqrt(A_b / np.pi), np.sqrt(A_c / np.pi)
141 | intersection_areas = [A_ab, A_bc, A_ac]
142 | radii = np.array([r_a, r_b, r_c])
143 |
144 | # Hypothetical distances between circle centers that assure
145 | # that their pairwise intersection areas match the requirements.
146 | dists = [
147 | find_distance_by_area(radii[i], radii[j], intersection_areas[i])
148 | for (i, j) in [(0, 1), (1, 2), (2, 0)]
149 | ]
150 |
151 | # How many intersections have nonzero area?
152 | num_nonzero = sum(np.array([A_ab, A_bc, A_ac]) > NUMERIC_TOLERANCE)
153 |
154 | # Handle four separate cases:
155 | # 1. All pairwise areas nonzero
156 | # 2. Two pairwise areas nonzero
157 | # 3. One pairwise area nonzero
158 | # 4. All pairwise areas zero.
159 |
160 | if num_nonzero == 3:
161 | # The "generic" case, simply use dists to position circles at the vertices of a triangle.
162 | # Before we need to ensure that resulting circles can be at all positioned on a triangle,
163 | # use an ad-hoc fix.
164 | for i in range(3):
165 | i, j, k = (i, (i + 1) % 3, (i + 2) % 3)
166 | if dists[i] > dists[j] + dists[k]:
167 | a, b = (j, k) if dists[j] < dists[k] else (k, j)
168 | dists[i] = dists[b] + dists[a] * 0.8
169 | warnings.warn("Bad circle positioning.")
170 | coords = _compute_triangle_layout_coords(radii, dists)
171 | elif num_nonzero == 2:
172 | # One pair of circles is not intersecting.
173 | # In this case we can position all three circles in a line
174 | # The two circles that have no intersection will be on either sides.
175 | for i in range(3):
176 | if intersection_areas[i] < NUMERIC_TOLERANCE:
177 | (left, right, middle) = (i, (i + 1) % 3, (i + 2) % 3)
178 | coords = np.zeros((3, 2))
179 | coords[middle][0] = dists[middle]
180 | coords[right][0] = dists[middle] + dists[right]
181 | # We want to avoid the situation where left & right still intersect
182 | if coords[left][0] + radii[left] > coords[right][0] - radii[right]:
183 | mid = (
184 | coords[left][0] + radii[left] + coords[right][0] - radii[right]
185 | ) / 2.0
186 | coords[left][0] = mid - radii[left] - 1e-5
187 | coords[right][0] = mid + radii[right] + 1e-5
188 | break
189 | elif num_nonzero == 1:
190 | # Only one pair of circles is intersecting, and one circle is independent.
191 | # Position all on a line first two intersecting, then the free one.
192 | for i in range(3):
193 | if intersection_areas[i] > NUMERIC_TOLERANCE:
194 | (left, right, side) = (i, (i + 1) % 3, (i + 2) % 3)
195 | coords = np.zeros((3, 2))
196 | coords[right][0] = dists[left]
197 | coords[side][0] = (
198 | dists[left] + radii[right] + radii[side] * 1.1
199 | ) # Pad by 10%
200 | break
201 | else:
202 | # All circles are non-touching. Put them all in a sequence
203 | coords = np.zeros((3, 2))
204 | coords[1][0] = radii[0] + radii[1] * 1.1
205 | coords[2][0] = radii[0] + radii[1] * 1.1 + radii[1] + radii[2] * 1.1
206 |
207 | coords = normalize_by_center_of_mass(coords, radii)
208 | result = VennLayout(
209 | centers=(
210 | Point2D(coords[0][0], coords[0][1]),
211 | Point2D(coords[1][0], coords[1][1]),
212 | Point2D(coords[2][0], coords[2][1]),
213 | ),
214 | radii=(radii[0], radii[1], radii[2]),
215 | )
216 | _compute_set_labels_positions(result)
217 | return result
218 |
219 |
220 | def _compute_triangle_layout_coords(
221 | radii: Tuple[float, float, float], dists: Tuple[float, float, float]
222 | ) -> np.ndarray:
223 | """
224 | Finds three centers for circles which form a proper triangle with given side lengths.
225 | The method puts the center of A and B on a horizontal line y==0, and C just below.
226 |
227 | Args:
228 | radii: The radii of the three circles (r_a, r_b, r_c).
229 | dists: The pairwise distances between the circle centers (d_ab, d_bc, d_ac),
230 |
231 | Returns:
232 | Coordinates of the circles to be laid out.
233 |
234 | >>> _compute_triangle_layout_coords((1, 1, 1), (0, 0, 0))
235 | array([[ 0., 0.],
236 | [ 0., 0.],
237 | [ 0., -0.]])
238 | >>> _compute_triangle_layout_coords((1, 1, 1), (2, 2, 2))
239 | array([[ 0. , 0. ],
240 | [ 2. , 0. ],
241 | [ 1. , -1.73205081]])
242 | """
243 | (d_ab, d_bc, d_ac) = dists
244 | (r_a, r_b, r_c) = radii
245 | coords = np.array([[0, 0], [d_ab, 0], [0, 0]], float)
246 | C_x = (
247 | (d_ac**2 - d_bc**2 + d_ab**2) / 2.0 / d_ab
248 | if np.abs(d_ab) > NUMERIC_TOLERANCE
249 | else 0.0
250 | )
251 | C_y = -np.sqrt(d_ac**2 - C_x**2)
252 | coords[2, :] = C_x, C_y
253 | return coords
254 |
255 |
256 | def _compute_set_labels_positions(layout: VennLayout):
257 | """Updates the set_labels_positions field of the given layout object."""
258 | if abs(layout.centers[2].y - layout.centers[0].y) > NUMERIC_TOLERANCE:
259 | # Three circles NOT on the same line
260 | layout.set_labels_layout = (
261 | LabelLayout(
262 | position=layout.centers[0]
263 | + Point2D(-layout.radii[0] / 2, layout.radii[0]),
264 | kwargs={"ha": "right"},
265 | ),
266 | LabelLayout(
267 | position=layout.centers[1]
268 | + Point2D(layout.radii[1] / 2, layout.radii[1]),
269 | kwargs={"ha": "left"},
270 | ),
271 | LabelLayout(
272 | position=layout.centers[2] + Point2D(0.0, -layout.radii[2] * 1.1),
273 | kwargs={"ha": "center", "va": "top"},
274 | ),
275 | )
276 | else:
277 | # Three circles on the same line
278 | padding = np.mean([r * 0.1 for r in layout.radii])
279 | layout.set_labels_layout = (
280 | LabelLayout(
281 | position=layout.centers[0] + Point2D(0.0, -layout.radii[0] - padding),
282 | kwargs={"ha": "center", "va": "top"},
283 | ),
284 | LabelLayout(
285 | position=layout.centers[1] + Point2D(0.0, -layout.radii[1] - padding),
286 | kwargs={"ha": "center", "va": "top"},
287 | ),
288 | LabelLayout(
289 | position=layout.centers[2] + Point2D(0.0, -layout.radii[2] - padding),
290 | kwargs={"ha": "center", "va": "top"},
291 | ),
292 | )
293 |
--------------------------------------------------------------------------------
/pyproject.toml:
--------------------------------------------------------------------------------
1 | [build-system]
2 | requires = ["setuptools"]
3 | build-backend = "setuptools.build_meta"
4 |
5 |
--------------------------------------------------------------------------------
/setup.cfg:
--------------------------------------------------------------------------------
1 | [egg_info]
2 | tag_build =
3 | tag_svn_revision = false
4 |
5 | [tool:pytest]
6 | addopts = --ignore=setup.py --ignore=build --ignore=dist --doctest-modules
7 | norecursedirs=*.egg
8 | doctest_optionflags=NORMALIZE_WHITESPACE ELLIPSIS
--------------------------------------------------------------------------------
/setup.py:
--------------------------------------------------------------------------------
1 | """
2 | Venn diagram plotting routines.
3 | Setup script.
4 |
5 | Note that "python setup.py test" invokes pytest on the package. This checks both xxx_test modules and docstrings.
6 |
7 | Copyright 2012-2024, Konstantin Tretyakov.
8 | http://kt.era.ee/
9 |
10 | Licensed under MIT license.
11 | """
12 |
13 | from setuptools import setup, find_namespace_packages
14 | from setuptools.command.test import test as TestCommand
15 |
16 |
17 | class PyTest(TestCommand):
18 | def run_tests(self):
19 | import sys
20 | import pytest # import here, cause outside the eggs aren't loaded
21 |
22 | sys.exit(pytest.main(self.test_args))
23 |
24 |
25 | version = [
26 | ln.split('"')[1]
27 | for ln in open("matplotlib_venn/__init__.py")
28 | if "__version__" in ln
29 | ][0]
30 |
31 | setup(
32 | name="matplotlib-venn",
33 | version=version,
34 | description="Functions for plotting area-proportional two- and three-way Venn diagrams in matplotlib.",
35 | long_description=open("README.rst").read(),
36 | classifiers=[ # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
37 | "Development Status :: 5 - Production/Stable",
38 | "Intended Audience :: Science/Research",
39 | "License :: OSI Approved :: MIT License",
40 | "Operating System :: OS Independent",
41 | "Programming Language :: Python :: 3",
42 | "Topic :: Scientific/Engineering :: Visualization",
43 | ],
44 | platforms=["Platform Independent"],
45 | keywords="matplotlib plotting charts venn-diagrams",
46 | author="Konstantin Tretyakov",
47 | author_email="kt@umn.ee",
48 | url="https://github.com/konstantint/matplotlib-venn",
49 | license="MIT",
50 | packages=find_namespace_packages(include=["matplotlib_venn*"]),
51 | include_package_data=True,
52 | zip_safe=True,
53 | install_requires=["matplotlib", "numpy", "scipy"],
54 | extras_require={
55 | "shapely": ["shapely"],
56 | },
57 | tests_require=["pytest"],
58 | cmdclass={"test": PyTest},
59 | entry_points="",
60 | )
61 |
--------------------------------------------------------------------------------
/tests/__init__.py:
--------------------------------------------------------------------------------
1 | '''
2 | Venn diagram plotting routines.
3 |
4 | Tests. Meant to be used via py.test.
5 |
6 | Copyright 2012-2024, Konstantin Tretyakov.
7 | http://kt.era.ee/
8 |
9 | Licensed under MIT license.
10 | '''
11 |
--------------------------------------------------------------------------------
/tests/functional_test.py:
--------------------------------------------------------------------------------
1 | """
2 | Venn diagram plotting routines.
3 | Test module (meant to be used via py.test).
4 |
5 | The images, corresponding to the tests here are shown in the ipython notebook Venn2 - Special Case Tests.
6 |
7 | Copyright 2014-2024, Konstantin Tretyakov.
8 | http://kt.era.ee/
9 |
10 | Licensed under MIT license.
11 | """
12 |
13 | import os.path
14 | from tests.utils import exec_ipynb
15 |
16 |
17 | def test_venn2():
18 | exec_ipynb(os.path.join(os.path.dirname(__file__), "venn2_functional.ipynb"))
19 |
20 |
21 | def test_venn3():
22 | exec_ipynb(os.path.join(os.path.dirname(__file__), "venn3_functional.ipynb"))
23 |
--------------------------------------------------------------------------------
/tests/issues_test.py:
--------------------------------------------------------------------------------
1 | '''
2 | Venn diagram plotting routines.
3 | Test module (meant to be used via py.test).
4 |
5 | Copyright 2015-2024, Konstantin Tretyakov.
6 | http://kt.era.ee/
7 |
8 | Licensed under MIT license.
9 | '''
10 |
11 | def test_issue_17():
12 | import matplotlib_venn as mv
13 | import numpy as np
14 | venn_areas = mv.layout.venn3.pairwise._compute_areas((135, 409, 17398, 122, 201, 135, 122), normalize_to=1.0, _minimal_area=1e-6)
15 | layout = mv.layout.venn3.pairwise._compute_layout(venn_areas)
16 | assert not np.any(np.isnan(layout.centers[0].asarray()))
17 |
18 |
19 | def test_pr_28():
20 | import matplotlib_venn as mv
21 | v = mv.venn3((1, 2, 3, 4, 5, 6, 7), subset_label_formatter = None)
22 | assert v.get_label_by_id('010').get_text() == '2'
23 | v = mv.venn3((1, 2, 3, 4, 5, 6, 7), subset_label_formatter = lambda x: 'Value: %+0.3f' % (x / 100.0))
24 | assert v.get_label_by_id('010').get_text() == 'Value: +0.020'
25 | v = mv.venn2((1, 2, 3), subset_label_formatter = None)
26 | assert v.get_label_by_id('01').get_text() == '2'
27 | v = mv.venn2((1, 2, 3), subset_label_formatter = lambda x: 'Value: %+0.3f' % (x / 100.0))
28 | assert v.get_label_by_id('01').get_text() == 'Value: +0.020'
29 |
30 | v = mv.venn3_unweighted((1, 2, 3, 4, 5, 6, 7), subset_label_formatter = lambda x: 'Value: %+0.3f' % (x / 100.0))
31 | assert v.get_label_by_id('010').get_text() == 'Value: +0.020'
32 | v = mv.venn2_unweighted((1, 2, 3), subset_label_formatter = lambda x: 'Value: %+0.3f' % (x / 100.0))
33 | assert v.get_label_by_id('01').get_text() == 'Value: +0.020'
34 |
--------------------------------------------------------------------------------
/tests/math_test.py:
--------------------------------------------------------------------------------
1 | """
2 | Venn diagram plotting routines.
3 | Test module (meant to be used via py.test).
4 |
5 | Copyright 2012-2024, Konstantin Tretyakov.
6 | http://kt.era.ee/
7 |
8 | Licensed under MIT license.
9 | """
10 |
11 | from numpy import pi, sqrt, arcsin
12 | from matplotlib_venn._math import (
13 | NUMERIC_TOLERANCE as tol,
14 | circle_intersection_area,
15 | find_distance_by_area,
16 | )
17 |
18 |
19 | def test_circle_intersection():
20 | f = lambda x: (sqrt(1 - x**2) * x + arcsin(x)) * 0.5 # Integral [sqrt(1 - x^2) dx]
21 | area_x = (
22 | lambda R: 4 * R**2 * (f(1) - f(0.5))
23 | ) # Area of intersection of two circles of radius R at distance R
24 |
25 | tests = [
26 | (0.0, 0.0, 0.0, 0.0),
27 | (0.0, 2.0, 1.0, 0.0),
28 | (2.0, 0.0, 1.0, 0.0),
29 | (1.0, 1.0, 0, pi),
30 | (2.0, 2.0, 0, pi * 4),
31 | (1, 1, 2, 0.0),
32 | (2.5, 3.5, 6.0, 0.0),
33 | (1, 1, 1, area_x(1)),
34 | (0.5, 0.5, 0.5, area_x(0.5)),
35 | (1.9, 1.9, 1.9, area_x(1.9)),
36 | ]
37 | for r, R, d, a in tests:
38 | assert abs(circle_intersection_area(r, R, d) - a) < tol
39 |
40 |
41 | def test_find_distances_by_area():
42 | tests = [
43 | (0.0, 0.0, 0.0, 0.0),
44 | (1.2, 1.3, 0.0, 2.5),
45 | (1.0, 1.0, pi, 0.0),
46 | (sqrt(1.0 / pi), sqrt(1.0 / pi), 1.0, 0.0),
47 | ]
48 | for r, R, a, d in tests:
49 | assert abs(find_distance_by_area(r, R, a, 0.0) - d) < tol
50 |
51 | tests = [
52 | (1, 2, 2),
53 | (1, 2, 1.1),
54 | (2, 3, 1.5),
55 | (2, 3, 1.0),
56 | (10, 20, 10),
57 | (20, 10, 10),
58 | (20, 10, 11),
59 | (0.9, 0.9, 0.0),
60 | ]
61 | for r, R, d in tests:
62 | a = circle_intersection_area(r, R, d)
63 | assert abs(find_distance_by_area(r, R, a, 0.0) - d) < tol
64 |
--------------------------------------------------------------------------------
/tests/region_test.py:
--------------------------------------------------------------------------------
1 | """
2 | Venn diagram plotting routines.
3 | Test module (meant to be used via py.test).
4 |
5 | Tests of the classes and methods in _regions.py
6 |
7 | Copyright 2014-2024, Konstantin Tretyakov.
8 | http://kt.era.ee/
9 |
10 | Licensed under MIT license.
11 | """
12 |
13 | import pytest
14 | import os
15 | import numpy as np
16 | from tests.utils import exec_ipynb
17 | from matplotlib_venn._region import (
18 | VennCircleRegion,
19 | VennRegionException,
20 | )
21 | from matplotlib_venn._math import NUMERIC_TOLERANCE as tol
22 |
23 |
24 | def test_circle_region():
25 | with pytest.raises(VennRegionException):
26 | vcr = VennCircleRegion((0, 0), -1)
27 |
28 | vcr = VennCircleRegion((0, 0), 10)
29 | assert abs(vcr.size() - np.pi * 100) <= tol
30 |
31 | # Interact with non-intersecting circle
32 | sr, ir = vcr.subtract_and_intersect_circle((11, 1), 1)
33 | assert sr == vcr
34 | assert ir.is_empty()
35 |
36 | # Interact with self
37 | sr, ir = vcr.subtract_and_intersect_circle((0, 0), 10)
38 | assert sr.is_empty()
39 | assert ir == vcr
40 |
41 | # Interact with a circle that makes a hole
42 | with pytest.raises(VennRegionException):
43 | sr, ir = vcr.subtract_and_intersect_circle((0, 8.9), 1)
44 |
45 | # Interact with a circle that touches the side from the inside
46 | for a, r in [
47 | (0, 1),
48 | (90, 2),
49 | (180, 3),
50 | (290, 0.01),
51 | (42, 9.99),
52 | (-0.1, 9.999),
53 | (180.1, 0.001),
54 | ]:
55 | cx = np.cos(a * np.pi / 180.0) * (10 - r)
56 | cy = np.sin(a * np.pi / 180.0) * (10 - r)
57 | # print "Next test case", a, r, cx, cy, r
58 | TEST_TOLERANCE = (
59 | tol if r > 0.001 and r < 9.999 else 1e-4
60 | ) # For tricky circles the numeric errors for arc lengths are just too big here
61 |
62 | sr, ir = vcr.subtract_and_intersect_circle((cx, cy), r)
63 | sr.verify()
64 | ir.verify()
65 | assert len(sr.arcs) == 2 and len(ir.arcs) == 2
66 | for a in sr.arcs:
67 | assert abs(a.length_degrees() - 360) < TEST_TOLERANCE
68 | assert abs(ir.arcs[0].length_degrees() - 0) < TEST_TOLERANCE
69 | assert abs(ir.arcs[1].length_degrees() - 360) < TEST_TOLERANCE
70 | assert abs(sr.size() + np.pi * r**2 - vcr.size()) < tol
71 | assert abs(ir.size() - np.pi * r**2) < tol
72 |
73 | # Interact with a circle that touches the side from the outside
74 | for a, r in [
75 | (0, 1),
76 | (90, 2),
77 | (180, 3),
78 | (290, 0.01),
79 | (42, 9.99),
80 | (-0.1, 9.999),
81 | (180.1, 0.001),
82 | ]:
83 | cx = np.cos(a * np.pi / 180.0) * (10 + r)
84 | cy = np.sin(a * np.pi / 180.0) * (10 + r)
85 |
86 | sr, ir = vcr.subtract_and_intersect_circle((cx, cy), r)
87 | # Depending on numeric roundoff we may get either an self and VennEmptyRegion or two arc regions. In any case the sizes should match
88 | assert abs(sr.size() + ir.size() - vcr.size()) < tol
89 | if sr == vcr:
90 | assert ir.is_empty()
91 | else:
92 | sr.verify()
93 | ir.verify()
94 | assert len(sr.arcs) == 2 and len(ir.arcs) == 2
95 | assert abs(sr.arcs[0].length_degrees() - 0) < tol
96 | assert abs(sr.arcs[1].length_degrees() - 360) < tol
97 | assert abs(ir.arcs[0].length_degrees() - 0) < tol
98 | assert abs(ir.arcs[1].length_degrees() - 0) < tol
99 |
100 | # Interact with some cases of intersecting circles
101 | for a, r in [
102 | (0, 1),
103 | (90, 2),
104 | (180, 3),
105 | (290, 0.01),
106 | (42, 9.99),
107 | (-0.1, 9.999),
108 | (180.1, 0.001),
109 | ]:
110 | cx = np.cos(a * np.pi / 180.0) * 10
111 | cy = np.sin(a * np.pi / 180.0) * 10
112 |
113 | sr, ir = vcr.subtract_and_intersect_circle((cx, cy), r)
114 | sr.verify()
115 | ir.verify()
116 | assert len(sr.arcs) == 2 and len(ir.arcs) == 2
117 | assert abs(sr.size() + ir.size() - vcr.size()) < tol
118 | assert sr.size() > 0
119 | assert ir.size() > 0
120 |
121 | # Do intersection the other way
122 | vcr2 = VennCircleRegion([cx, cy], r)
123 | sr2, ir2 = vcr2.subtract_and_intersect_circle(vcr.center, vcr.radius)
124 | sr2.verify()
125 | ir2.verify()
126 | assert len(sr2.arcs) == 2 and len(ir2.arcs) == 2
127 | assert abs(sr2.size() + ir2.size() - vcr2.size()) < tol
128 | assert sr2.size() > 0
129 | assert ir2.size() > 0
130 | for i in range(2):
131 | assert ir.arcs[i].approximately_equal(ir.arcs[i])
132 |
133 |
134 | def test_region_visual():
135 | exec_ipynb(os.path.join(os.path.dirname(__file__), "region_visual.ipynb"))
136 |
137 |
138 | def test_region_label_visual():
139 | exec_ipynb(os.path.join(os.path.dirname(__file__), "region_label_visual.ipynb"))
140 |
--------------------------------------------------------------------------------
/tests/utils.py:
--------------------------------------------------------------------------------
1 | """
2 | Venn diagram plotting routines.
3 | Utility functions used in tests.
4 |
5 | Copyright 2014-2024, Konstantin Tretyakov.
6 | http://kt.era.ee/
7 |
8 | Licensed under MIT license.
9 | """
10 |
11 | from typing import Dict, Sequence, Union
12 | import json
13 | import numpy as np
14 |
15 | import matplotlib.pyplot as plt
16 | from matplotlib.patches import PathPatch, Circle
17 | from matplotlib.pyplot import scatter
18 |
19 | from matplotlib_venn._common import VennDiagram
20 | from matplotlib_venn._math import Point2DInternal
21 |
22 |
23 | def point_in_patch(patch: Union[PathPatch, Circle], point: np.ndarray):
24 | """
25 | Given a patch, which is either a CirclePatch, a PathPatch or None,
26 | returns true if the patch is not None and the point is inside it.
27 | """
28 | if patch is None:
29 | return False
30 | elif isinstance(patch, Circle):
31 | c = patch.center
32 | return (c[0] - point[0]) ** 2 + (c[1] - point[1]) ** 2 <= patch.radius**2
33 | else:
34 | return patch.get_path().contains_point(point)
35 |
36 |
37 | def verify_diagram(
38 | diagram: VennDiagram, test_points: Dict[str, Sequence[Point2DInternal]]
39 | ) -> None:
40 | """
41 | Given an object returned from venn2/venn3 verifies that the regions of the diagram contain the given points.
42 | In addition, makes sure that the diagram labels are within the corresponding regions (for all regions that are claimed to exist).
43 | Parameters:
44 | diagram: a VennDiagram object
45 | test_points: a dict, mapping region ids to lists of points that must be located in that region.
46 | if some region is mapped to None rather than a list, the region must not be present in the diagram.
47 | Region '' lists points that must not be present in any other region.
48 | All keys of this dictionary not mapped to None (except key '') correspond to regions that must exist in the diagram.
49 | For those regions we check that the region's label is positioned inside the region.
50 | """
51 | for region in test_points.keys():
52 | points = test_points[region]
53 | if points is None:
54 | assert diagram.get_patch_by_id(region) is None, (
55 | "Region %s must be None" % region
56 | )
57 | else:
58 | if region != "":
59 | assert diagram.get_patch_by_id(region) is not None, (
60 | "Region %s must exist" % region
61 | )
62 | assert point_in_patch(
63 | diagram.get_patch_by_id(region),
64 | diagram.get_label_by_id(region).get_position(),
65 | ), (
66 | "Label for region %s must be within this region" % region
67 | )
68 | for pt in points:
69 | scatter(pt[0], pt[1])
70 | for (
71 | test_region
72 | ) in (
73 | test_points.keys()
74 | ): # Test that the point is in its own region and no one else's
75 | if test_region != "":
76 | assert point_in_patch(
77 | diagram.get_patch_by_id(test_region), pt
78 | ) == (
79 | region == test_region
80 | ), "Point %s should %s in region %s" % (
81 | pt,
82 | "be" if (region == test_region) else "not be",
83 | test_region,
84 | )
85 |
86 |
87 | def exec_ipynb(filename: str) -> None:
88 | """Executes all cells in a given ipython notebook consequentially."""
89 | s = json.load(open(filename))
90 | locals_dict = locals()
91 | for cell in s["cells"]:
92 | if cell["cell_type"] == "code":
93 | code = "".join(cell["source"])
94 | exec(code, locals_dict)
95 |
96 | # Explicitly close any figures created by this cell, which
97 | # would normally (in a notebook) be done by the
98 | # matplotlib-inline backend. This prevents a warning about "too
99 | # many figures opened" from Matplotlib.
100 | plt.close("all")
101 |
--------------------------------------------------------------------------------
/tests/venn3_pairwise_layout_test.py:
--------------------------------------------------------------------------------
1 | """
2 | Venn diagram plotting routines.
3 | Test module (meant to be used via py.test).
4 |
5 | Copyright 2012-2024, Konstantin Tretyakov.
6 | http://kt.era.ee/
7 |
8 | Licensed under MIT license.
9 | """
10 |
11 | import numpy as np
12 | from matplotlib_venn.layout.venn3.pairwise import (
13 | _compute_areas,
14 | _compute_layout,
15 | )
16 | from matplotlib_venn._math import (
17 | NUMERIC_TOLERANCE as tol,
18 | circle_intersection_area,
19 | circle_circle_intersection,
20 | )
21 |
22 |
23 | def test_compute_areas():
24 | tests = []
25 | for i in range(7):
26 | t = [0] * 7
27 | t[i] = 1
28 | tests.append(tuple(t))
29 | t = [1] * 7
30 | t[i] = 0
31 | tests.append(tuple(t))
32 | tests.append(tuple(range(7)))
33 |
34 | for t in tests:
35 | (A, B, C, AB, BC, AC, ABC) = _compute_areas(t, _minimal_area=0)
36 | t = np.array(t, float)
37 | t = t / np.sum(t)
38 | (Abc, aBc, ABc, abC, AbC, aBC, ABC) = t
39 | assert abs(A - (Abc + ABc + AbC + ABC)) < tol
40 | assert abs(B - (aBc + ABc + aBC + ABC)) < tol
41 | assert abs(C - (abC + AbC + aBC + ABC)) < tol
42 | assert abs(AB - (ABc + ABC)) < tol
43 | assert abs(AC - (AbC + ABC)) < tol
44 | assert abs(BC - (aBC + ABC)) < tol
45 |
46 |
47 | def test_compute_layout():
48 | from numpy.linalg import norm
49 |
50 | tol = 1e-5 # Test #2 does not pass at the desired tolerance.
51 |
52 | tests = [
53 | (2, 2, 2, 1, 1, 1, 0),
54 | (10, 40, 90, 0, 40, 10, 0),
55 | (1, 1, 1, 0, 0, 0, 0),
56 | (1.2, 2, 1, 1, 0.5, 0.6, 0),
57 | ]
58 | for t in tests:
59 | (A, B, C, AB, BC, AC, ABC) = t
60 | layout = _compute_layout(t)
61 | coords, radii = layout.centers, layout.radii
62 | assert (
63 | abs(
64 | circle_intersection_area(
65 | radii[0], radii[1], norm(coords[0].asarray() - coords[1].asarray())
66 | )
67 | - AB
68 | )
69 | < tol
70 | )
71 | assert (
72 | abs(
73 | circle_intersection_area(
74 | radii[0], radii[2], norm(coords[0].asarray() - coords[2].asarray())
75 | )
76 | - AC
77 | )
78 | < tol
79 | )
80 | assert (
81 | abs(
82 | circle_intersection_area(
83 | radii[1], radii[2], norm(coords[1].asarray() - coords[2].asarray())
84 | )
85 | - BC
86 | )
87 | < tol
88 | )
89 | assert (
90 | abs(
91 | norm(
92 | radii[0] ** 2 * coords[0].asarray()
93 | + radii[1] ** 2 * coords[1].asarray()
94 | + radii[2] ** 2 * coords[2].asarray()
95 | - np.array([0.0, 0.0])
96 | )
97 | )
98 | < tol
99 | )
100 |
101 |
102 | def test_circle_circle_intersection():
103 | from numpy.linalg import norm
104 |
105 | tests = [
106 | ([0, 0], 1, [1, 0], 1, 2),
107 | ([0, 0], 1, [2, 0], 1, 1),
108 | ([0, 0], 1, [0.5, 0], 0.5, 1),
109 | ([0, 0], 1, [0, 0], 1, 0),
110 | ([0, 0], 1, [0, 0.1], 0.8, 0),
111 | ([0, 0], 1, [2.1, 0], 1, 0),
112 | ([10, 20], 100, [200, 200], 50, 0),
113 | ([10, 20], 100, [40, 50], 20, 0),
114 | ([-2.0, -3.1], 10, [2.0, 3.1], 10, 2),
115 | ([-3.0, 1.0], 10.0, [0.0, 0.0], 9.0, 2),
116 | ]
117 | for C_a, r_a, C_b, r_b, num_intersections in tests:
118 | res = circle_circle_intersection(C_a, r_a, C_b, r_b)
119 | res2 = circle_circle_intersection(C_b, r_b, C_a, r_a)
120 | if num_intersections == 0:
121 | assert res is None
122 | assert res2 is None
123 | else:
124 | assert res is not None
125 | assert res2 is not None
126 | assert res.shape == (2, 2)
127 | assert res2.shape == (2, 2)
128 | C_a, C_b = np.array(C_a, float), np.array(C_b, float)
129 | for pt in res:
130 | assert abs(norm(pt - C_a) - r_a) < tol
131 | assert abs(norm(pt - C_b) - r_b) < tol
132 | if num_intersections == 1:
133 | assert abs(norm(res[0] - res[1])) < tol
134 | else:
135 | assert abs(norm(res[0] - res[1])) > tol
136 | # Verify the order of points
137 | v2, v1 = res[0] - C_a, res[1] - C_a
138 | outer_prod = v1[0] * v2[1] - v1[1] * v2[0]
139 | assert outer_prod < 0
140 | # Changing the order of circles must change the order of points
141 | assert abs(norm(res[0] - res2[1])) < tol
142 | assert abs(norm(res[1] - res2[0])) < tol
143 |
--------------------------------------------------------------------------------
/tests/venn_ax_kw_test.py:
--------------------------------------------------------------------------------
1 | """
2 | Venn diagram plotting routines.
3 | Test module (meant to be used via py.test).
4 |
5 | Copyright 2012-2024, Konstantin Tretyakov.
6 | http://kt.era.ee/
7 |
8 | Licensed under MIT license.
9 | """
10 |
11 | from matplotlib_venn import *
12 |
13 |
14 | def test_ax_kw():
15 | import matplotlib.pyplot as plt
16 |
17 | figure, axes = plt.subplots(2, 2)
18 |
19 | venn2(subsets={"10": 1, "01": 1, "11": 1}, set_labels=("A", "B"), ax=axes[0][0])
20 | venn2_circles((1, 2, 3), ax=axes[0][1])
21 | venn3(subsets=(1, 1, 1, 1, 1, 1, 1), set_labels=("A", "B", "C"), ax=axes[1][0])
22 | venn3_circles(
23 | {"001": 10, "100": 20, "010": 21, "110": 13, "011": 14}, ax=axes[1][1]
24 | )
25 |
--------------------------------------------------------------------------------