././@PaxHeader0000000000000000000000000000003400000000000010212 xustar0028 mtime=1722463586.3418362 matplotlib-venn-1.1.1/0000755000175000017500000000000014652532542016407 5ustar00konstantintkonstantint././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722463422.0 matplotlib-venn-1.1.1/CHANGELOG.txt0000644000175000017500000001057114652532276020447 0ustar00konstantintkonstantintVersion 1.1.1 -------------- - Removed cost_based.LayoutAlgorithmOptions (options are passed directly to the LayoutAlgorithm constructor instead). Version 1.1.0 -------------- - Implemented a cost-based layout algorithm (Issue #35). The implementation depends on shapely, which is added as an optional dependency (package needs to be installed as matplotlib-venn[shapely] for it to be included). Thanks to https://github.com/paulbrodersen - Added a set diagram to the README (Issue #51). - Fixed doctests that started failing due to different numpy versions representing values differently. Version 1.0.0 -------------- - Fixed package installation issues (Issue #78). Version 1.0.0-alpha -------------- - Dropped support for Python versions below 3.5 (by excluding those versions from CI builds). - Added typing annotations. Some arguments are now a bit more strictly typed. E.g. what previously would accept a List now requires (at least as far as type annotations are concerned) a tuple. - Refactored the code by abstracting away the layout algorithms to allow plug-in customization (Issue #35). This deprecated the `normalize_to` input argument to the venn2 and venn3 functions, and made `venn2_unweighted` and `venn3_unweighted` obsolete. These will be removed in some future version. Version 0.11.10 -------------- - Updated tests to work with Matplotlib 3.6+ (PR#70). Version 0.11.9 -------------- - Minor update to metadata (mark Development Status as Stable). Version 0.11.8 -------------- - Added pyproject.toml (Issue #71). Version 0.11.7 -------------- - Fixed Travis-CI-related error messages. Version 0.11.6 -------------- - Added matplotlib_venn.__version__ field (Issue #59). Version 0.11.5 -------------- - Added subset_label_formatter parameter (PR#28). Version 0.11.4 -------------- - Added support for Counter objects (PR#26). Version 0.11.3 -------------- - Tiny change in README required a version bump to upload it to PyPi. Version 0.11.2 -------------- - Fixes issue #24. - Addresses Debian bug #813782. Version 0.11 ------------ - Fixed issue #17. This would change the previous layout of circles in certain pathological cases. Version 0.10 ------------ - Completely rewritten the region generation logic, presumably fixing all of the problems behind issue #14 (and hopefully not introducing too many new bugs). The new algorithm positions the labels in a different way, which may look slightly worse than the previous one in some rare cases. - New kind of IPython-based tests. Version 0.9 ----------- - Better support for weird special cases in Venn3 (i.e. one circle being completely inside another, issue #10). Version 0.8 ----------- - Added support for Python 3. Version 0.7 ----------- - Added the possibility to provide sets (rather than subset sizes) to venn2 and venn3. Thanks to https://github.com/aebrahim - Functions won't bail out on sets of size 0 now (the diagrams won't look pretty, though). Thanks to https://github.com/olgabot - Venn2/Venn3 objects now provide information about the coordinates and radii of the circles. - Utility functions added for drawing unweighed diagrams (venn2_unweighted, venn3_unweighted) - Labels for zero-size sets can be switched off using a method of VennDiagram. - Some general code refactoring. Version 0.6 ----------- - Added "ax" keyword to the plotting routines to specify the axes object on which the diagram will be created. Thanks goes to https://github.com/sinhrks Version 0.5 ----------- - Fixed a bug (issue 1, "unreferenced variable 's'" in venn2 and venn2_circles) Version 0.4 ----------- - Fixed a bug ("ValueError: to_rgba: Invalid rgba arg" when specifying lighter set colors) Version 0.3 ----------- - Changed package name from `matplotlib.venn` to `matplotlib_venn`. - Fixed up some places to comply with pep8 lint checks. Version 0.2 ----------- - Changed parameterization of venn3 and venn3_circles (now expects 7-element vectors as arguments rather than 8-element). - 2-set venn diagrams (functions venn2 and venn2_circles) - Added support for non-intersecting sets ("Euler diagrams") - Minor fixes here and there. Version 0.1 ----------- - Initial version, three-circle area-weighted venn diagrams. ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722419543.0 matplotlib-venn-1.1.1/LICENSE0000644000175000017500000000205014652404527017412 0ustar00konstantintkonstantintCopyright (c) 2012, Konstantin Tretyakov 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: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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.././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722419543.0 matplotlib-venn-1.1.1/MANIFEST.in0000644000175000017500000000004014652404527020140 0ustar00konstantintkonstantintinclude README.rst CHANGELOG.txt././@PaxHeader0000000000000000000000000000003400000000000010212 xustar0028 mtime=1722463586.3418362 matplotlib-venn-1.1.1/PKG-INFO0000644000175000017500000002137214652532542017511 0ustar00konstantintkonstantintMetadata-Version: 2.1 Name: matplotlib-venn Version: 1.1.1 Summary: Functions for plotting area-proportional two- and three-way Venn diagrams in matplotlib. Home-page: https://github.com/konstantint/matplotlib-venn Author: Konstantin Tretyakov Author-email: kt@umn.ee License: MIT Keywords: matplotlib plotting charts venn-diagrams Platform: Platform Independent Classifier: Development Status :: 5 - Production/Stable Classifier: Intended Audience :: Science/Research Classifier: License :: OSI Approved :: MIT License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python :: 3 Classifier: Topic :: Scientific/Engineering :: Visualization Provides-Extra: shapely License-File: LICENSE ==================================================== Venn diagram plotting routines for Python/Matplotlib ==================================================== .. image:: https://travis-ci.org/konstantint/matplotlib-venn.png?branch=master :target: https://travis-ci.org/konstantint/matplotlib-venn Routines for plotting area-weighted two- and three-circle venn diagrams. Installation ------------ Install the package as usual via ``pip``:: $ python -m pip install matplotlib-venn Since version 1.1.0 the package includes an extra "cost based" layout algorithm for `venn3` diagrams, that relies on the `shapely` package, which is not installed as a default dependency. If you need the new algorithm (or just have nothing against installing `shapely` along the way), instead do:: $ python -m pip install "matplotlib-venn[shapely]" It is quite probable that `shapely` will become a required dependency eventually in one of the future versions. Dependencies ------------ - ``numpy``, - ``scipy``, - ``matplotlib``, - ``shapely`` (optional). Usage ----- The package provides four main functions: ``venn2``, ``venn2_circles``, ``venn3`` and ``venn3_circles``. The functions ``venn2`` and ``venn2_circles`` accept as their only required argument a 3-element tuple ``(Ab, aB, AB)`` of subset sizes, and draw a two-circle venn diagram with respective region areas, e.g.:: venn2(subsets = (3, 2, 1)) In this example, the region, corresponding to subset ``A and not B`` will be three times larger in area than the region, corresponding to subset ``A and B``. You can also provide a tuple of two ``set`` or ``Counter`` (i.e. multi-set) objects instead (new in version 0.7), e.g.:: venn2((set(['A', 'B', 'C', 'D']), set(['D', 'E', 'F']))) Similarly, the functions ``venn3`` and ``venn3_circles`` take a 7-element tuple of subset sizes ``(Abc, aBc, ABc, abC, AbC, aBC, ABC)``, and draw a three-circle area-weighted Venn diagram: .. image:: https://user-images.githubusercontent.com/13646666/87874366-96924800-c9c9-11ea-8b06-ac1336506b59.png Alternatively, a tuple of three ``set`` or ``Counter`` objects may be provided. The functions ``venn2`` and ``venn3`` draw the diagrams as a collection of colored patches, annotated with text labels. The functions ``venn2_circles`` and ``venn3_circles`` draw just the circles. The functions ``venn2_circles`` and ``venn3_circles`` return the list of ``matplotlib.patch.Circle`` objects that may be tuned further to your liking. The functions ``venn2`` and ``venn3`` return an object of class ``VennDiagram``, which gives access to constituent patches, text elements, and (since version 0.7) the information about the centers and radii of the circles. Basic Example:: from matplotlib_venn import venn2 venn2(subsets = (3, 2, 1)) For the three-circle case:: from matplotlib_venn import venn3 venn3(subsets = (1, 1, 1, 2, 1, 2, 2), set_labels = ('Set1', 'Set2', 'Set3')) A more elaborate example:: from matplotlib import pyplot as plt import numpy as np from matplotlib_venn import venn3, venn3_circles plt.figure(figsize=(4,4)) v = venn3(subsets=(1, 1, 1, 1, 1, 1, 1), set_labels = ('A', 'B', 'C')) v.get_patch_by_id('100').set_alpha(1.0) v.get_patch_by_id('100').set_color('white') v.get_label_by_id('100').set_text('Unknown') v.get_label_by_id('A').set_text('Set "A"') c = venn3_circles(subsets=(1, 1, 1, 1, 1, 1, 1), linestyle='dashed') c[0].set_lw(1.0) c[0].set_ls('dotted') plt.title("Sample Venn diagram") plt.annotate('Unknown set', xy=v.get_label_by_id('100').get_position() - np.array([0, 0.05]), xytext=(-70,-70), ha='center', textcoords='offset points', bbox=dict(boxstyle='round,pad=0.5', fc='gray', alpha=0.1), arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0.5',color='gray')) plt.show() An example with multiple subplots:: from matplotlib_venn import venn2, venn2_circles figure, axes = plt.subplots(2, 2) venn2(subsets={'10': 1, '01': 1, '11': 1}, set_labels = ('A', 'B'), ax=axes[0][0]) venn2_circles((1, 2, 3), ax=axes[0][1]) venn3(subsets=(1, 1, 1, 1, 1, 1, 1), set_labels = ('A', 'B', 'C'), ax=axes[1][0]) venn3_circles({'001': 10, '100': 20, '010': 21, '110': 13, '011': 14}, ax=axes[1][1]) plt.show() Perhaps the most common use case is generating a Venn diagram given three sets of objects:: set1 = set(['A', 'B', 'C', 'D']) set2 = set(['B', 'C', 'D', 'E']) set3 = set(['C', 'D',' E', 'F', 'G']) venn3([set1, set2, set3], ('Set1', 'Set2', 'Set3')) plt.show() Tuning the diagram layout ------------------------- Note that for a three-circle venn diagram it is not in general possible to achieve exact correspondence between the required set sizes and region areas. The default layout algorithm aims to correctly represent: * Relative areas of the full individual sets (A, B, C). * Relative areas of pairwise intersections of sets (A&B, A&C, B&C, not to be confused with the regions A&B&~C, A&~B&C, ~A&B&C, on the diagram). Sometimes the result is unsatisfactory and either the area weighting or the layout logic needs to be tuned. The area weighing can be adjusted by providing a `fixed_subset_sizes` argument to the `DefaultLayoutAlgorithm`:: from matplotlib_venn.layout.venn2 import DefaultLayoutAlgorithm venn2((1,2,3), layout_algorithm=DefaultLayoutAlgorithm(fixed_subset_sizes=(1,1,1))) from matplotlib_venn.layout.venn3 import DefaultLayoutAlgorithm venn3((7,6,5,4,3,2,1), layout_algorithm=DefaultLayoutAlgorithm(fixed_subset_sizes=(1,1,1,1,1,1,1))) 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 invoked, yet the actual numbers will be `(1,2,3)` and `(7,6,5,4,3,2,1)` respectively. The diagram can be tuned further by switching the layout algorithm to a different implementation. At the moment the package offers an alternative layout algorithm for `venn3` diagrams that lays the circles out by optimizing a user-provided *cost function*. The following examples illustrate its usage:: from matplotlib_venn.layout.venn3 import cost_based subset_sizes = (100,200,10000,10,20,3,1) venn3(subset_sizes, layout_algorithm=cost_based.LayoutAlgorithm()) alg = cost_based.LayoutAlgorithm(cost_fn=cost_based.WeightedAggregateCost(transform_fn=lambda x: x)) venn3(subset_sizes, layout_algorithm=alg) alg = cost_based.LayoutAlgorithm(cost_fn=cost_based.WeightedAggregateCost(weights=(0,0,0,1,1,1,1))) venn3(subset_sizes, layout_algorithm=alg) The default "pairwise" algorithm is, theoretically, a special case of the cost-based method with the respective cost function:: alg = cost_based.LayoutAlgorithm(cost_fn=cost_based.pairwise_cost) venn3(subset_sizes, layout_algorithm=alg) (The latter plot will be close, but not perfectly equal to the outcome of `DefaultLayoutAlgorithm()`). Note that the import:: from matplotlib_venn.layout.venn3 import cost_based will fail unless you have the optional `shapely` package installed (see "Installation" above). Questions --------- * If you ask your questions at `StackOverflow `_ and tag them `matplotlib-venn `_, chances are high you could get an answer from the maintainer of this package. See also -------- * Report issues and submit fixes at Github: https://github.com/konstantint/matplotlib-venn Check out the ``DEVELOPER-README.rst`` for development-related notes. * Some alternative means of plotting a Venn diagram (as of October 2012) are reviewed in the blog post: http://fouryears.eu/2012/10/13/venn-diagrams-in-python/ * The `matplotlib-subsets `_ package visualizes a hierarchy of sets as a tree of rectangles. * The `matplotlib_set_diagrams `_ package is a GPL-licensed alternative that offers a different layout algorithm, which supports more than three sets and provides a cool ability to incorporate wordclouds into your Venn (Euler) diagrams. ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722463346.0 matplotlib-venn-1.1.1/README.rst0000644000175000017500000002004714652532162020077 0ustar00konstantintkonstantint==================================================== Venn diagram plotting routines for Python/Matplotlib ==================================================== .. image:: https://travis-ci.org/konstantint/matplotlib-venn.png?branch=master :target: https://travis-ci.org/konstantint/matplotlib-venn Routines for plotting area-weighted two- and three-circle venn diagrams. Installation ------------ Install the package as usual via ``pip``:: $ python -m pip install matplotlib-venn Since version 1.1.0 the package includes an extra "cost based" layout algorithm for `venn3` diagrams, that relies on the `shapely` package, which is not installed as a default dependency. If you need the new algorithm (or just have nothing against installing `shapely` along the way), instead do:: $ python -m pip install "matplotlib-venn[shapely]" It is quite probable that `shapely` will become a required dependency eventually in one of the future versions. Dependencies ------------ - ``numpy``, - ``scipy``, - ``matplotlib``, - ``shapely`` (optional). Usage ----- The package provides four main functions: ``venn2``, ``venn2_circles``, ``venn3`` and ``venn3_circles``. The functions ``venn2`` and ``venn2_circles`` accept as their only required argument a 3-element tuple ``(Ab, aB, AB)`` of subset sizes, and draw a two-circle venn diagram with respective region areas, e.g.:: venn2(subsets = (3, 2, 1)) In this example, the region, corresponding to subset ``A and not B`` will be three times larger in area than the region, corresponding to subset ``A and B``. You can also provide a tuple of two ``set`` or ``Counter`` (i.e. multi-set) objects instead (new in version 0.7), e.g.:: venn2((set(['A', 'B', 'C', 'D']), set(['D', 'E', 'F']))) Similarly, the functions ``venn3`` and ``venn3_circles`` take a 7-element tuple of subset sizes ``(Abc, aBc, ABc, abC, AbC, aBC, ABC)``, and draw a three-circle area-weighted Venn diagram: .. image:: https://user-images.githubusercontent.com/13646666/87874366-96924800-c9c9-11ea-8b06-ac1336506b59.png Alternatively, a tuple of three ``set`` or ``Counter`` objects may be provided. The functions ``venn2`` and ``venn3`` draw the diagrams as a collection of colored patches, annotated with text labels. The functions ``venn2_circles`` and ``venn3_circles`` draw just the circles. The functions ``venn2_circles`` and ``venn3_circles`` return the list of ``matplotlib.patch.Circle`` objects that may be tuned further to your liking. The functions ``venn2`` and ``venn3`` return an object of class ``VennDiagram``, which gives access to constituent patches, text elements, and (since version 0.7) the information about the centers and radii of the circles. Basic Example:: from matplotlib_venn import venn2 venn2(subsets = (3, 2, 1)) For the three-circle case:: from matplotlib_venn import venn3 venn3(subsets = (1, 1, 1, 2, 1, 2, 2), set_labels = ('Set1', 'Set2', 'Set3')) A more elaborate example:: from matplotlib import pyplot as plt import numpy as np from matplotlib_venn import venn3, venn3_circles plt.figure(figsize=(4,4)) v = venn3(subsets=(1, 1, 1, 1, 1, 1, 1), set_labels = ('A', 'B', 'C')) v.get_patch_by_id('100').set_alpha(1.0) v.get_patch_by_id('100').set_color('white') v.get_label_by_id('100').set_text('Unknown') v.get_label_by_id('A').set_text('Set "A"') c = venn3_circles(subsets=(1, 1, 1, 1, 1, 1, 1), linestyle='dashed') c[0].set_lw(1.0) c[0].set_ls('dotted') plt.title("Sample Venn diagram") plt.annotate('Unknown set', xy=v.get_label_by_id('100').get_position() - np.array([0, 0.05]), xytext=(-70,-70), ha='center', textcoords='offset points', bbox=dict(boxstyle='round,pad=0.5', fc='gray', alpha=0.1), arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0.5',color='gray')) plt.show() An example with multiple subplots:: from matplotlib_venn import venn2, venn2_circles figure, axes = plt.subplots(2, 2) venn2(subsets={'10': 1, '01': 1, '11': 1}, set_labels = ('A', 'B'), ax=axes[0][0]) venn2_circles((1, 2, 3), ax=axes[0][1]) venn3(subsets=(1, 1, 1, 1, 1, 1, 1), set_labels = ('A', 'B', 'C'), ax=axes[1][0]) venn3_circles({'001': 10, '100': 20, '010': 21, '110': 13, '011': 14}, ax=axes[1][1]) plt.show() Perhaps the most common use case is generating a Venn diagram given three sets of objects:: set1 = set(['A', 'B', 'C', 'D']) set2 = set(['B', 'C', 'D', 'E']) set3 = set(['C', 'D',' E', 'F', 'G']) venn3([set1, set2, set3], ('Set1', 'Set2', 'Set3')) plt.show() Tuning the diagram layout ------------------------- Note that for a three-circle venn diagram it is not in general possible to achieve exact correspondence between the required set sizes and region areas. The default layout algorithm aims to correctly represent: * Relative areas of the full individual sets (A, B, C). * Relative areas of pairwise intersections of sets (A&B, A&C, B&C, not to be confused with the regions A&B&~C, A&~B&C, ~A&B&C, on the diagram). Sometimes the result is unsatisfactory and either the area weighting or the layout logic needs to be tuned. The area weighing can be adjusted by providing a `fixed_subset_sizes` argument to the `DefaultLayoutAlgorithm`:: from matplotlib_venn.layout.venn2 import DefaultLayoutAlgorithm venn2((1,2,3), layout_algorithm=DefaultLayoutAlgorithm(fixed_subset_sizes=(1,1,1))) from matplotlib_venn.layout.venn3 import DefaultLayoutAlgorithm venn3((7,6,5,4,3,2,1), layout_algorithm=DefaultLayoutAlgorithm(fixed_subset_sizes=(1,1,1,1,1,1,1))) 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 invoked, yet the actual numbers will be `(1,2,3)` and `(7,6,5,4,3,2,1)` respectively. The diagram can be tuned further by switching the layout algorithm to a different implementation. At the moment the package offers an alternative layout algorithm for `venn3` diagrams that lays the circles out by optimizing a user-provided *cost function*. The following examples illustrate its usage:: from matplotlib_venn.layout.venn3 import cost_based subset_sizes = (100,200,10000,10,20,3,1) venn3(subset_sizes, layout_algorithm=cost_based.LayoutAlgorithm()) alg = cost_based.LayoutAlgorithm(cost_fn=cost_based.WeightedAggregateCost(transform_fn=lambda x: x)) venn3(subset_sizes, layout_algorithm=alg) alg = cost_based.LayoutAlgorithm(cost_fn=cost_based.WeightedAggregateCost(weights=(0,0,0,1,1,1,1))) venn3(subset_sizes, layout_algorithm=alg) The default "pairwise" algorithm is, theoretically, a special case of the cost-based method with the respective cost function:: alg = cost_based.LayoutAlgorithm(cost_fn=cost_based.pairwise_cost) venn3(subset_sizes, layout_algorithm=alg) (The latter plot will be close, but not perfectly equal to the outcome of `DefaultLayoutAlgorithm()`). Note that the import:: from matplotlib_venn.layout.venn3 import cost_based will fail unless you have the optional `shapely` package installed (see "Installation" above). Questions --------- * If you ask your questions at `StackOverflow `_ and tag them `matplotlib-venn `_, chances are high you could get an answer from the maintainer of this package. See also -------- * Report issues and submit fixes at Github: https://github.com/konstantint/matplotlib-venn Check out the ``DEVELOPER-README.rst`` for development-related notes. * Some alternative means of plotting a Venn diagram (as of October 2012) are reviewed in the blog post: http://fouryears.eu/2012/10/13/venn-diagrams-in-python/ * The `matplotlib-subsets `_ package visualizes a hierarchy of sets as a tree of rectangles. * The `matplotlib_set_diagrams `_ package is a GPL-licensed alternative that offers a different layout algorithm, which supports more than three sets and provides a cool ability to incorporate wordclouds into your Venn (Euler) diagrams. ././@PaxHeader0000000000000000000000000000003400000000000010212 xustar0028 mtime=1722463586.3418362 matplotlib-venn-1.1.1/matplotlib_venn/0000755000175000017500000000000014652532542021604 5ustar00konstantintkonstantint././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722463366.0 matplotlib-venn-1.1.1/matplotlib_venn/__init__.py0000644000175000017500000000535614652532206023723 0ustar00konstantintkonstantint""" Venn diagram plotting routines. Copyright 2012-2024, Konstantin Tretyakov. http://kt.era.ee/ Licensed under MIT license. This package contains routines for plotting area-weighted two- and three-circle venn diagrams. There are four main functions here: :code:`venn2`, :code:`venn2_circles`, :code:`venn3`, :code:`venn3_circles`. The :code:`venn2` and :code:`venn2_circles` accept as their only required argument a 3-element list of subset sizes: subsets = (Ab, aB, AB) That is, for example, subsets[0] contains the size of the subset (A and not B), and subsets[2] contains the size of the set (A and B), etc. Similarly, the functions :code:`venn3` and :code:`venn3_circles` require a 7-element list: subsets = (Abc, aBc, ABc, abC, AbC, aBC, ABC) The functions :code:`venn2_circles` and :code:`venn3_circles` simply draw two or three circles respectively, such that their intersection areas correspond to the desired set intersection sizes. Note that for a three-circle Venn diagram it is not possible to achieve exact correspondence, although in most cases the picture will still provide a decent representation. The functions :code:`venn2` and :code:`venn3` draw diagram as a collection of separate colored patches with text labels. The functions :code:`venn2_circles` and :code:`venn3_circles` return the list of Circle patches that may be tuned further to your liking. The functions :code:`venn2` and :code:`venn3` return an object of class :code:`VennDiagram` which provides access to constituent patches and text elements. Example:: from matplotlib import pyplot as plt import numpy as np from matplotlib_venn import venn3, venn3_circles plt.figure(figsize=(4,4)) v = venn3(subsets=(1, 1, 1, 1, 1, 1, 1), set_labels = ('A', 'B', 'C')) v.get_patch_by_id('100').set_alpha(1.0) v.get_patch_by_id('100').set_color('white') v.get_label_by_id('100').set_text('Unknown') v.get_label_by_id('A').set_text('Set "A"') c = venn3_circles(subsets=(1, 1, 1, 1, 1, 1, 1), linestyle='dashed') c[0].set_lw(1.0) c[0].set_ls('dotted') plt.title("Sample Venn diagram") plt.annotate('Unknown set', xy=v.get_label_by_id('100').get_position() - np.array([0, 0.05]), xytext=(-70,-70), ha='center', textcoords='offset points', bbox=dict(boxstyle='round,pad=0.5', fc='gray', alpha=0.1), arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0.5',color='gray')) """ from matplotlib_venn._venn2 import venn2, venn2_circles from matplotlib_venn._venn3 import venn3, venn3_circles from matplotlib_venn._util import venn2_unweighted, venn3_unweighted ___all___ = [ "venn2", "venn2_circles", "venn3", "venn3_circles", "venn2_unweighted", "venn3_unweighted", ] __version__ = "1.1.1" ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722437028.0 matplotlib-venn-1.1.1/matplotlib_venn/_arc.py0000644000175000017500000005502614652446644023101 0ustar00konstantintkonstantint""" Venn diagram plotting routines. General-purpose math routines for computing with circular arcs. Everything is encapsulated in the "Arc" class. Copyright 2014-2024, Konstantin Tretyakov. http://kt.era.ee/ Licensed under MIT license. """ from typing import Optional, Sequence import math import numpy as np from matplotlib_venn._math import ( NUMERIC_TOLERANCE, Point2DInternal, circle_circle_intersection, vector_angle_in_degrees, ) class Arc: """ A representation of a directed circle arc. Essentially it is a namedtuple(center, radius, from_angle, to_angle, direction) with a bunch of helper methods for measuring arc lengths and intersections. The from_angle and to_angle of an arc must be represented in degrees. The direction is a boolean, with True corresponding to counterclockwise (positive) direction, and False - clockwise (negative). For convenience, the class defines a "sign" property, which is +1 if direction = True and -1 otherwise. """ def __init__( self, center: Point2DInternal, radius: float, from_angle: float, to_angle: float, direction: bool, ): """Raises a ValueError if radius is negative. >>> a = Arc((0, 0), -1, 0, 0, True) Traceback (most recent call last): ... ValueError: Arc's radius may not be negative >>> a = Arc((0, 0), 0, 0, 0, True) >>> a = Arc((0, 0), 1, 0, 0, True) """ self.center = np.asarray(center) self.radius = float(radius) if radius < 0.0: raise ValueError("Arc's radius may not be negative") self.from_angle = float(from_angle) self.to_angle = float(to_angle) self.direction = direction self.sign = 1 if direction else -1 def length_degrees(self) -> float: """Computes the length of the arc in degrees. The length computation corresponds to what you would expect if you would draw the arc using matplotlib taking direction into account. >>> Arc((0,0), 1, 0, 0, True).length_degrees() 0.0 >>> Arc((0,0), 2, 0, 0, False).length_degrees() 0.0 >>> Arc((0,0), 3, 0, 1, True).length_degrees() 1.0 >>> Arc((0,0), 4, 0, 1, False).length_degrees() 359.0 >>> Arc((0,0), 5, 0, 360, True).length_degrees() 360.0 >>> Arc((0,0), 6, 0, 360, False).length_degrees() 0.0 >>> Arc((0,0), 7, 0, 361, True).length_degrees() 360.0 >>> Arc((0,0), 8, 0, 361, False).length_degrees() 359.0 >>> Arc((0,0), 9, 10, -10, True).length_degrees() 340.0 >>> Arc((0,0), 10, 10, -10, False).length_degrees() 20.0 >>> Arc((0,0), 1, 10, 5, True).length_degrees() 355.0 >>> Arc((0,0), 1, -10, -5, False).length_degrees() 355.0 >>> Arc((0,0), 1, 180, -180, True).length_degrees() 0.0 >>> Arc((0,0), 1, 180, -180, False).length_degrees() 360.0 >>> Arc((0,0), 1, -180, 180, True).length_degrees() 360.0 >>> Arc((0,0), 1, -180, 180, False).length_degrees() 0.0 >>> Arc((0,0), 1, 175, -175, True).length_degrees() 10.0 >>> Arc((0,0), 1, 175, -175, False).length_degrees() 350.0 """ d_angle = self.sign * (self.to_angle - self.from_angle) if d_angle > 360: return 360.0 elif d_angle < 0: return d_angle % 360.0 else: return abs( d_angle ) # Yes, abs() is needed, otherwise we get the weird "-0.0" output in the doctests def length_radians(self) -> float: """Returns the length of the arc in radians. >>> Arc((0,0), 1, 0, 0, True).length_radians() 0.0 >>> Arc((0,0), 2, 0, 360, True).length_radians() 6.283... >>> Arc((0,0), 6, -18, 18, True).length_radians() 0.6283... """ return self.length_degrees() * np.pi / 180.0 def length(self) -> float: """Returns the actual length of the arc. >>> Arc((0,0), 2, 0, 360, True).length() 12.566... >>> Arc((0,0), 2, 90, 360, False).length() 3.1415... >>> Arc((0,0), 0, 90, 360, True).length() 0.0 """ return self.radius * self.length_radians() def sector_area(self) -> float: """Returns the area of the corresponding arc sector. >>> Arc((0,0), 2, 0, 360, True).sector_area() 12.566... >>> Arc((0,0), 2, 0, 36, True).sector_area() 1.2566... >>> Arc((0,0), 2, 0, 36, False).sector_area() 11.3097... """ return self.radius**2 / 2 * self.length_radians() def segment_area(self) -> float: """Returns the area of the corresponding arc segment. >>> Arc((0,0), 2, 0, 360, True).segment_area() 12.566... >>> Arc((0,0), 2, 0, 180, True).segment_area() 6.283... >>> Arc((0,0), 2, 0, 90, True).segment_area() 1.14159... >>> Arc((0,0), 2, 0, 90, False).segment_area() 11.42477796... >>> Arc((0,0), 2, 0, 0, False).segment_area() 0.0 >>> Arc((0, 9), 1, 89.99, 90, False).segment_area() 3.1415... """ theta = self.length_radians() return self.radius**2 / 2 * (theta - math.sin(theta)) def angle_as_point(self, angle: float) -> np.ndarray: """ Converts a given angle in degrees to the point coordinates on the arc's circle. Inverse of point_to_angle. >>> Arc((1, 1), 1, 0, 0, True).angle_as_point(0).tolist() [2.0, 1.0] >>> Arc((1, 1), 1, 0, 0, True).angle_as_point(90).tolist() [1.0, 2.0] >>> bool(np.all(np.isclose(Arc((1, 1), 1, 0, 0, True).angle_as_point(-270), [1.0, 2.0]))) True """ angle_rad = angle * np.pi / 180.0 return self.center + self.radius * np.array( [math.cos(angle_rad), math.sin(angle_rad)] ) def start_point(self) -> np.ndarray: """ Returns a 2x1 numpy array with the coordinates of the arc's start point. >>> Arc((0, 0), 1, 0, 0, True).start_point().tolist() [1.0, 0.0] >>> Arc((0, 0), 1, 45, 0, True).start_point().tolist() [0.707..., 0.707...] """ return self.angle_as_point(self.from_angle) def end_point(self) -> np.ndarray: """ Returns a 2x1 numpy array with the coordinates of the arc's end point. >>> bool(np.all(Arc((0, 0), 1, 0, 90, True).end_point() - np.array([0, 1]) < NUMERIC_TOLERANCE)) True """ return self.angle_as_point(self.to_angle) def mid_point(self) -> np.ndarray: """ Returns the midpoint of the arc as a 1x2 numpy array. """ midpoint_angle = self.from_angle + self.sign * self.length_degrees() / 2 return self.angle_as_point(midpoint_angle) def approximately_equal(self, arc: "Arc", tolerance=NUMERIC_TOLERANCE) -> bool: """ Returns true if the parameters of this arc are within of the parameters of the other arc, and the direction is the same. Note that no angle simplification is performed (i.e. some arcs that might be equal in principle are not declared as such by this method) >>> tol = NUMERIC_TOLERANCE >>> Arc((0, 0), 10, 20, 30, True).approximately_equal(Arc((tol/2, tol/2), 10+tol/2, 20-tol/2, 30-tol/2, True)) True >>> Arc((0, 0), 10, 20, 30, True).approximately_equal(Arc((0, 0), 10, 20, 30, False)) False >>> Arc((0, 0), 10, 20, 30, True).approximately_equal(Arc((0, 0+tol), 10, 20, 30, True)) False """ return bool( self.direction == arc.direction and np.all(abs(self.center - arc.center) < tolerance) and abs(self.radius - arc.radius) < tolerance and abs(self.from_angle - arc.from_angle) < tolerance and abs(self.to_angle - arc.to_angle) < tolerance ) def point_as_angle(self, pt: Point2DInternal) -> float: """ Given a point located on the arc's circle, return the corresponding angle in degrees. No check is done that the point lies on the circle (this is essentially a convenience wrapper around _math.vector_angle_in_degrees) >>> a = Arc((0, 0), 1, 0, 0, True) >>> a.point_as_angle((1, 0)) 0.0 >>> a.point_as_angle((1, 1)) 45.0 >>> a.point_as_angle((0, 1)) 90.0 >>> a.point_as_angle((-1, 1)) 135.0 >>> a.point_as_angle((-1, 0)) 180.0 >>> a.point_as_angle((-1, -1)) -135.0 >>> a.point_as_angle((0, -1)) -90.0 >>> a.point_as_angle((1, -1)) -45.0 """ return vector_angle_in_degrees(np.asarray(pt) - self.center) def contains_angle_degrees(self, angle: float) -> float: """ Returns true, if a point with the corresponding angle (given in degrees) is within the arc. 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) >>> a = Arc((0, 0), 1, 0, 0, True) >>> assert a.contains_angle_degrees(0) >>> assert a.contains_angle_degrees(360) >>> assert not a.contains_angle_degrees(1) >>> a = Arc((0, 0), 1, 170, -170, True) >>> assert not a.contains_angle_degrees(165) >>> assert a.contains_angle_degrees(170) >>> assert a.contains_angle_degrees(175) >>> assert a.contains_angle_degrees(180) >>> assert a.contains_angle_degrees(185) >>> assert a.contains_angle_degrees(190) >>> assert not a.contains_angle_degrees(195) >>> assert not a.contains_angle_degrees(-195) >>> assert a.contains_angle_degrees(-190) >>> assert a.contains_angle_degrees(-185) >>> assert a.contains_angle_degrees(-180) >>> assert a.contains_angle_degrees(-175) >>> assert a.contains_angle_degrees(-170) >>> assert not a.contains_angle_degrees(-165) >>> assert a.contains_angle_degrees(-170 - 360) >>> assert a.contains_angle_degrees(-190 - 360) >>> assert a.contains_angle_degrees(170 + 360) >>> assert not a.contains_angle_degrees(0) >>> assert not a.contains_angle_degrees(100) >>> assert not a.contains_angle_degrees(-100) """ _d = self.sign * (angle - self.from_angle) % 360.0 return _d <= self.length_degrees() def intersect_circle( self, center: Point2DInternal, radius: float ) -> Sequence[np.ndarray]: """ Given a circle, finds the intersection point(s) of the arc with the circle. Returns a list of 2x1 numpy arrays. The list has length 0, 1 or 2, depending on how many intesection points there are. If the circle touches the arc, it is reported as two intersection points (which are equal). Points are ordered along the arc. Intersection with the same circle as the arc's own (which means infinitely many points usually) is reported as no intersection at all. >>> a = Arc((0, 0), 1, -60, 60, True) >>> str(a.intersect_circle((1, 0), 1)).replace(' ', '') '[array([0.5...,-0.866...]),array([0.5...,0.866...])]' >>> a.intersect_circle((0.9, 0), 1) [] >>> str(a.intersect_circle((1,-0.1), 1)).replace(' ', '') '[array([0.586...,0.810...])]' >>> str(a.intersect_circle((1, 0.1), 1)).replace(' ', '') '[array([0.586...,-0.810...])]' >>> a.intersect_circle((0, 0), 1) # Infinitely many intersection points [] >>> str(a.intersect_circle((2, 0), 1)).replace(' ', '') # Touching point, hence repeated twice '[array([1.,0.]),array([1.,0.])]' >>> a = Arc((0, 0), 1, 60, -60, False) # Same arc, different direction >>> str(a.intersect_circle((1, 0), 1)).replace(' ', '') '[array([0.5...,0.866...]),array([0.5...,-0.866...])]' >>> a = Arc((0, 0), 1, 120, -120, True) >>> a.intersect_circle((-1, 0), 1) [array([-0.5..., 0.866...]), array([-0.5..., -0.866...])] >>> a.intersect_circle((-0.9, 0), 1) [] >>> a.intersect_circle((-1,-0.1), 1) [array([-0.586..., 0.810...])] >>> a.intersect_circle((-1, 0.1), 1) [array([-0.586..., -0.810...])] >>> a.intersect_circle((-2, 0), 1) [array([-1., 0.]), array([-1., 0.])] >>> a = Arc((0, 0), 1, -120, 120, False) >>> a.intersect_circle((-1, 0), 1) [array([-0.5..., -0.866...]), array([-0.5..., 0.866...])] """ intersections = circle_circle_intersection( self.center, self.radius, center, radius ) if intersections is None: return [] # Check whether the points lie on the arc and order them accordingly _len = self.length_degrees() isections = [ [self.sign * (self.point_as_angle(pt) - self.from_angle) % 360.0, pt] for pt in intersections ] # Try to find as many candidate intersections as possible (i.e. +- tol within arc limits) # Unless arc's length is 360, interpret intersections just before the arc's starting point as belonging to the starting point. if _len < 360.0 - NUMERIC_TOLERANCE: for isec in isections: if isec[0] > 360.0 - NUMERIC_TOLERANCE: isec[0] = 0.0 isections = [ (a, pt[0], pt[1]) for (a, pt) in isections if a < _len + NUMERIC_TOLERANCE or a > 360 - NUMERIC_TOLERANCE ] isections.sort() return [np.array([b, c]) for (a, b, c) in isections] def intersect_arc(self, arc: "Arc") -> Sequence[np.ndarray]: """ Given an arc, finds the intersection point(s) of this arc with that. Returns a list of 2x1 numpy arrays. The list has length 0, 1 or 2, depending on how many intesection points there are. Points are ordered along the arc. Intersection with the arc along the same circle (which means infinitely many points usually) is reported as no intersection at all. >>> a = Arc((0, 0), 1, -90, 90, True) >>> str(a.intersect_arc(Arc((1, 0), 1, 90, 270, True))).replace(' ', '') '[array([0.5,-0.866...]),array([0.5,0.866...])]' >>> str(a.intersect_arc(Arc((1, 0), 1, 90, 180, True))).replace(' ', '') '[array([0.5,0.866...])]' >>> a.intersect_arc(Arc((1, 0), 1, 121, 239, True)) [] >>> tol = NUMERIC_TOLERANCE >>> 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). '[array([0.5,-0.866...]),array([0.5,0.866...])]' """ intersections = self.intersect_circle(arc.center, arc.radius) isections = [ pt for pt in intersections if arc.contains_angle_degrees(arc.point_as_angle(pt)) ] return isections def subarc( self, from_angle: Optional[float] = None, to_angle: Optional[float] = None ) -> "Arc": """ Creates a sub-arc from a given angle (or beginning of this arc) to a given angle (or end of this arc). Verifies that from_angle and to_angle are within the arc and properly ordered. If from_angle is None, start of this arc is used instead. If to_angle is None, end of this arc is used instead. Angles are given in degrees. >>> a = Arc((0, 0), 1, 0, 360, True) >>> a.subarc(None, None) Arc([0.000, 0.000], 1.000, 0.000, 360.000, True, degrees=360.000) >>> a.subarc(360, None) Arc([0.000, 0.000], 1.000, 360.000, 360.000, True, degrees=0.000) >>> a.subarc(0, None) Arc([0.000, 0.000], 1.000, 0.000, 360.000, True, degrees=360.000) >>> a.subarc(-10, None) Arc([0.000, 0.000], 1.000, 350.000, 360.000, True, degrees=10.000) >>> a.subarc(None, -10) Arc([0.000, 0.000], 1.000, 0.000, 350.000, True, degrees=350.000) >>> a.subarc(1, 359).subarc(2, 358).subarc() Arc([0.000, 0.000], 1.000, 2.000, 358.000, True, degrees=356.000) """ if from_angle is None: from_angle = self.from_angle if to_angle is None: to_angle = self.to_angle cur_length = self.length_degrees() d_new_from = self.sign * (from_angle - self.from_angle) if d_new_from != 360.0: d_new_from = d_new_from % 360.0 d_new_to = self.sign * (to_angle - self.from_angle) if d_new_to != 360.0: d_new_to = d_new_to % 360.0 # Gracefully handle numeric precision issues for zero-length arcs if abs(d_new_from - d_new_to) < NUMERIC_TOLERANCE: d_new_from = d_new_to if d_new_to < d_new_from: raise ValueError("Subarc to-angle must be smaller than from-angle.") if d_new_to > cur_length + NUMERIC_TOLERANCE: raise ValueError("Subarc to-angle must lie within the current arc.") return Arc( self.center, self.radius, self.from_angle + self.sign * d_new_from, self.from_angle + self.sign * d_new_to, self.direction, ) def subarc_between_points( self, p_from: Optional[Point2DInternal] = None, p_to: Optional[Point2DInternal] = None, ) -> "Arc": """ Given two points on the arc, extract a sub-arc between those points. No check is made to verify the points are actually on the arc. It is basically a wrapper around subarc(point_as_angle(p_from), point_as_angle(p_to)). Either p_from or p_to may be None to denote first or last arc endpoints. >>> a = Arc((0, 0), 1, 0, 90, True) >>> a.subarc_between_points((1, 0), (math.cos(np.pi/4), math.sin(np.pi/4))) Arc([0.000, 0.000], 1.000, 0.000, 45.000, True, degrees=45.000) >>> a.subarc_between_points(None, None) Arc([0.000, 0.000], 1.000, 0.000, 90.000, True, degrees=90.000) >>> a.subarc_between_points((math.cos(np.pi/4), math.sin(np.pi/4))) Arc([0.000, 0.000], 1.000, 45.000, 90.000, True, degrees=45.000) """ a_from = self.point_as_angle(p_from) if p_from is not None else None a_to = self.point_as_angle(p_to) if p_to is not None else None return self.subarc(a_from, a_to) def reversed(self) -> "Arc": """ Returns a copy of this arc, with the direction flipped. >>> Arc((0, 0), 1, 0, 360, True).reversed() Arc([0.000, 0.000], 1.000, 360.000, 0.000, False, degrees=360.000) >>> Arc((0, 0), 1, 175, -175, True).reversed() Arc([0.000, 0.000], 1.000, -175.000, 175.000, False, degrees=10.000) >>> Arc((0, 0), 1, 0, 370, True).reversed() Arc([0.000, 0.000], 1.000, 370.000, 0.000, False, degrees=360.000) """ return Arc( self.center, self.radius, self.to_angle, self.from_angle, not self.direction ) def direction_vector(self, angle: float) -> np.ndarray: """ Returns a unit vector, pointing in the arc's movement direction at a given (absolute) angle (in degrees). No check is made whether angle lies within the arc's span (the results for angles outside of the arc's span ) Returns a 2x1 numpy array. >>> a = Arc((0, 0), 1, 0, 90, True) >>> tol = NUMERIC_TOLERANCE >>> assert all(abs(a.direction_vector(0) - np.array([0.0, 1.0])) < tol) >>> assert all(abs(a.direction_vector(45) - np.array([ -0.70710678, 0.70710678])) < 1e-6) >>> assert all(abs(a.direction_vector(90) - np.array([-1.0, 0.0])) < tol) >>> assert all(abs(a.direction_vector(135) - np.array([-0.70710678, -0.70710678])) < 1e-6) >>> assert all(abs(a.direction_vector(-180) - np.array([0.0, -1.0])) < tol) >>> assert all(abs(a.direction_vector(-90) - np.array([1.0, 0.0])) < tol) >>> a = a.reversed() >>> assert all(abs(a.direction_vector(0) - np.array([0.0, -1.0])) < tol) >>> assert all(abs(a.direction_vector(45) - np.array([ 0.70710678, -0.70710678])) < 1e-6) >>> assert all(abs(a.direction_vector(90) - np.array([1.0, 0.0])) < tol) >>> assert all(abs(a.direction_vector(135) - np.array([0.70710678, 0.70710678])) < 1e-6) >>> assert all(abs(a.direction_vector(-180) - np.array([0.0, 1.0])) < tol) >>> assert all(abs(a.direction_vector(-90) - np.array([-1.0, 0.0])) < tol) """ a = angle + self.sign * 90 a = a * np.pi / 180.0 return np.array([math.cos(a), math.sin(a)]) def fix_360_to_0(self) -> None: """ Sometimes we have to create an arc using from_angle and to_angle computed numerically. If from_angle == to_angle, it may sometimes happen that a tiny discrepancy will make from_angle > to_angle, and instead of getting a 0-length arc we end up with a 360-degree arc. Sometimes we know for sure that a 360-degree arc is not what we want, and in those cases the problem is easy to fix. This helper method does that. It checks whether from_angle and to_angle are numerically similar, and if so makes them equal. >>> a = Arc((0, 0), 1, 0, -NUMERIC_TOLERANCE/2, True) >>> a Arc([0.000, 0.000], 1.000, 0.000, -0.000, True, degrees=360.000) >>> a.fix_360_to_0() >>> a Arc([0.000, 0.000], 1.000, -0.000, -0.000, True, degrees=0.000) """ if abs(self.from_angle - self.to_angle) < NUMERIC_TOLERANCE: self.from_angle = self.to_angle def lies_on_circle(self, center: Point2DInternal, radius: float) -> bool: """Tests whether the arc circle's center and radius match the given ones within tolerance. >>> a = Arc((0, 0), 1, 0, 0, False) >>> tol = NUMERIC_TOLERANCE >>> a.lies_on_circle((tol/2, tol/2), 1+tol/2) True >>> a.lies_on_circle((tol/2, tol/2), 1-tol) False """ return ( np.all(abs(np.asarray(center) - self.center) < NUMERIC_TOLERANCE) and abs(radius - self.radius) < NUMERIC_TOLERANCE ) def __repr__(self) -> str: return "Arc([%0.3f, %0.3f], %0.3f, %0.3f, %0.3f, %s, degrees=%0.3f)" % ( self.center[0], self.center[1], self.radius, self.from_angle, self.to_angle, self.direction, self.length_degrees(), ) ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722419543.0 matplotlib-venn-1.1.1/matplotlib_venn/_common.py0000644000175000017500000001111714652404527023607 0ustar00konstantintkonstantint""" Venn diagram plotting routines. Functionality, common to venn2 and venn3. Copyright 2012-2024, Konstantin Tretyakov. http://kt.era.ee/ Licensed under MIT license. """ from typing import Optional, Sequence import numpy as np from matplotlib.axes import Axes from matplotlib.patches import Patch from matplotlib.text import Text from matplotlib_venn._math import Point2D class VennDiagram: """ A container for a set of patches and patch labels and set labels, which make up the rendered venn diagram. This object is returned by a venn2 or venn3 function call. """ id2idx = { "10": 0, "01": 1, "11": 2, "100": 0, "010": 1, "110": 2, "001": 3, "101": 4, "011": 5, "111": 6, "A": 0, "B": 1, "C": 2, } def __init__( self, patches: Sequence[Patch], subset_labels: Sequence[Text], set_labels: Sequence[Text], centers: Sequence[Point2D], radii: Sequence[float], ): self.patches = patches self.subset_labels = subset_labels self.set_labels = set_labels self.centers = centers self.radii = radii def get_patch_by_id(self, id: str) -> Patch: """Returns a patch by a "region id". A region id is a string '10', '01' or '11' for 2-circle diagram or a string like '001', '010', etc, for 3-circle diagram.""" return self.patches[self.id2idx[id]] def get_label_by_id(self, id: str) -> Text: """ Returns a subset label by a "region id". A region id is a string '10', '01' or '11' for 2-circle diagram or a string like '001', '010', etc, for 3-circle diagram. Alternatively, if the string 'A', 'B' (or 'C' for 3-circle diagram) is given, the label of the corresponding set is returned (or None).""" if len(id) == 1: return ( self.set_labels[self.id2idx[id]] if self.set_labels is not None else None ) else: return self.subset_labels[self.id2idx[id]] def get_circle_center(self, id: int) -> Point2D: """ Returns the coordinates of the center of a circle as a numpy array (x,y) id must be 0, 1 or 2 (corresponding to the first, second, or third circle). This is a getter-only (i.e. changing this value does not affect the diagram) """ return self.centers[id] def get_circle_radius(self, id: int) -> float: """ Returns the radius of circle id (where id is 0, 1 or 2). This is a getter-only (i.e. changing this value does not affect the diagram) """ return self.radii[id] def hide_zeroes(self) -> None: """ Sometimes it makes sense to hide the labels for subsets whose size is zero. This utility method does this. """ for v in self.subset_labels: if v is not None and v.get_text() == "0": v.set_visible(False) def mix_colors( col1: np.ndarray, col2: np.ndarray, col3: Optional[np.ndarray] = None ) -> np.ndarray: """ Mixes two or three colors to compute a "mixed" color (for purposes of computing colors of the intersection regions based on the colors of the sets. Note that we do not simply compute averages of given colors as those seem too dark for some default configurations. Thus, we lighten the combination up a bit. Inputs are (up to) three RGB triples of floats 0.0-1.0 given as numpy arrays. >>> mix_colors(np.array([1.0, 0., 0.]), np.array([1.0, 0., 0.])).tolist() [1.0, 0.0, 0.0] >>> 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() [1.0, 1.0, 0.04] """ if col3 is None: mix_color = 0.7 * (col1 + col2) else: mix_color = 0.4 * (col1 + col2 + col3) mix_color = np.min([mix_color, [1.0, 1.0, 1.0]], 0) return mix_color def prepare_venn_axes( ax: Axes, centers: Sequence[Point2D], radii: Sequence[float] ) -> None: """ Sets properties of the axis object to suit venn plotting. I.e. hides ticks, makes proper xlim/ylim. """ ax.set_aspect("equal") ax.set_xticks([]) ax.set_yticks([]) min_x = min(c.x - r for (c, r) in zip(centers, radii)) max_x = max(c.x + r for (c, r) in zip(centers, radii)) min_y = min(c.y - r for (c, r) in zip(centers, radii)) max_y = max(c.y + r for (c, r) in zip(centers, radii)) ax.set_xlim([min_x - 0.1, max_x + 0.1]) ax.set_ylim([min_y - 0.1, max_y + 0.1]) ax.set_axis_off() ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722429679.0 matplotlib-venn-1.1.1/matplotlib_venn/_math.py0000644000175000017500000002214214652430357023250 0ustar00konstantintkonstantint""" Venn diagram plotting routines. Math helper functions. Copyright 2012, Konstantin Tretyakov. http://kt.era.ee/ Licensed under MIT license. """ from typing import Optional, Sequence, Union from scipy.optimize import brentq import math import numpy as np class Point2D: """A simple representation of a 2D point. Note that the methods below use raw 2D np.arrays to represent points. This is due to the original desire to keep things simple and assumption that those are all internal. Since version 1.0.0 we expose layout logic for external interfacing and would rather use semantically meaningful structures. Maybe this should move over to a module without an underscore in the name. """ __slots__ = ("x", "y") def __init__(self, x: float, y: float): self.x = x self.y = y def asarray(self): return np.array([self.x, self.y], float) def __add__(self, other: "Point2D") -> "Point2D": return Point2D(self.x + other.x, self.y + other.y) def __repr__(self): return "Point2D({}, {})".format(self.x, self.y) Point2DInternal = Union[Sequence[float], np.ndarray] NUMERIC_TOLERANCE = 1e-10 def point_in_circle( pt: Point2DInternal, center: Point2DInternal, radius: float ) -> bool: """ Returns true if a given point is located inside (or on the border) of a circle. >>> point_in_circle((0, 0), (0, 0), 1) True >>> point_in_circle((1, 0), (0, 0), 1) True >>> point_in_circle((1, 1), (0, 0), 1) False """ d = np.linalg.norm(np.asarray(pt) - np.asarray(center)) return bool(d <= radius) def box_product(v1: Point2DInternal, v2: Point2DInternal) -> float: """Returns a determinant |v1 v2|. The value is equal to the signed area of a parallelogram built on v1 and v2. The value is positive is v2 is to the left of v1. >>> box_product((0.0, 1.0), (0.0, 1.0)) 0.0 >>> box_product((1.0, 0.0), (0.0, 1.0)) 1.0 >>> box_product((0.0, 1.0), (1.0, 0.0)) -1.0 """ return v1[0] * v2[1] - v1[1] * v2[0] def circle_intersection_area(r: float, R: float, d: float) -> float: """ Formula from: http://mathworld.wolfram.com/Circle-CircleIntersection.html Does not make sense for negative r, R or d >>> circle_intersection_area(0.0, 0.0, 0.0) 0.0 >>> circle_intersection_area(1.0, 1.0, 0.0) 3.1415... >>> circle_intersection_area(1.0, 1.0, 1.0) 1.2283... """ if abs(d) < NUMERIC_TOLERANCE: minR = min(r, R) return np.pi * minR**2 if abs(r - 0) < NUMERIC_TOLERANCE or abs(R - 0) < NUMERIC_TOLERANCE: return 0.0 d2, r2, R2 = float(d**2), float(r**2), float(R**2) arg = (d2 + r2 - R2) / 2 / d / r # Even with valid arguments, the above computation may result in things like -1.001 arg = max(min(arg, 1.0), -1.0) A = r2 * math.acos(arg) arg = (d2 + R2 - r2) / 2 / d / R arg = max(min(arg, 1.0), -1.0) B = R2 * math.acos(arg) arg = (-d + r + R) * (d + r - R) * (d - r + R) * (d + r + R) arg = max(arg, 0) C = -0.5 * math.sqrt(arg) return A + B + C def circle_line_intersection( center: Point2DInternal, r: float, a: Point2DInternal, b: Point2DInternal ) -> Optional[np.ndarray]: """ Computes two intersection points between the circle centered at
and radius and a line given by two points a and b. If no intersection exists, or if a==b, None is returned. If one intersection exists, it is repeated in the answer. >>> circle_line_intersection(np.array([0.0, 0.0]), 1, np.array([-1.0, 0.0]), np.array([1.0, 0.0])) array([[ 1., 0.], [-1., 0.]]) >>> 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() [[0.0, 0.0], [0.0, 0.0]] """ s = b - a # Quadratic eqn coefs A = np.linalg.norm(s) ** 2 if abs(A) < NUMERIC_TOLERANCE: return None B = 2 * np.dot(a - center, s) C = np.linalg.norm(a - center) ** 2 - r**2 disc = B**2 - 4 * A * C if disc < 0.0: return None t1 = (-B + math.sqrt(disc)) / 2.0 / A t2 = (-B - math.sqrt(disc)) / 2.0 / A return np.array([a + t1 * s, a + t2 * s]) def find_distance_by_area( r: float, R: float, a: float, numeric_correction: float = 0.0001 ) -> float: """ Solves circle_intersection_area(r, R, d) == a for d numerically (analytical solution seems to be too ugly to pursue). Assumes that a < pi * min(r, R)**2, will fail otherwise. The numeric correction parameter is used whenever the computed distance is exactly (R - r) (i.e. one circle must be inside another). In this case the result returned is (R-r+correction). This helps later when we position the circles and need to ensure they intersect. >>> find_distance_by_area(1, 1, 0, 0.0) 2.0 >>> round(find_distance_by_area(1, 1, 3.1415, 0.0), 4) 0.0 >>> d = find_distance_by_area(2, 3, 4, 0.0) >>> d 3.37... >>> round(circle_intersection_area(2, 3, d), 10) 4.0 >>> find_distance_by_area(1, 2, np.pi) 1.0001 """ if r > R: r, R = R, r if abs(a) < NUMERIC_TOLERANCE: return float(r + R) if abs(min(r, R) ** 2 * np.pi - a) < NUMERIC_TOLERANCE: return abs(R - r + numeric_correction) return brentq(lambda x: circle_intersection_area(r, R, x) - a, R - r, R + r) def circle_circle_intersection( C_a: Point2DInternal, r_a: float, C_b: Point2DInternal, r_b: float ) -> Optional[np.ndarray]: """ Finds the coordinates of the intersection points of two circles A and B. Circle center coordinates C_a and C_b, should be given as tuples (or 1x2 arrays). Returns a 2x2 array result with result[0] being the first intersection point (to the right of the vector C_a -> C_b) and result[1] being the second intersection point. If there is a single intersection point, it is repeated in output. If there are no intersection points or an infinite number of those, None is returned. >>> circle_circle_intersection([0, 0], 1, [1, 0], 1) # Two intersection points array([[ 0.5 , -0.866...], [ 0.5 , 0.866...]]) >>> circle_circle_intersection([0, 0], 1, [2, 0], 1).tolist() # Single intersection point (circles touch from outside) [[1.0, 0.0], [1.0, 0.0]] >>> circle_circle_intersection([0, 0], 1, [0.5, 0], 0.5).tolist() # Single intersection point (circles touch from inside) [[1.0, 0.0], [1.0, 0.0]] >>> circle_circle_intersection([0, 0], 1, [0, 0], 1) is None # Infinite number of intersections (circles coincide) True >>> circle_circle_intersection([0, 0], 1, [0, 0.1], 0.8) is None # No intersections (one circle inside another) True >>> circle_circle_intersection([0, 0], 1, [2.1, 0], 1) is None # No intersections (one circle outside another) True """ C_a, C_b = np.asarray(C_a, float), np.asarray(C_b, float) v_ab = C_b - C_a d_ab = np.linalg.norm(v_ab) if ( np.abs(d_ab) < NUMERIC_TOLERANCE ): # No intersection points or infinitely many of them (circle centers coincide) return None cos_gamma = (d_ab**2 + r_a**2 - r_b**2) / 2.0 / d_ab / r_a if ( abs(cos_gamma) > 1.0 + NUMERIC_TOLERANCE / 10 ): # Allow for a tiny numeric tolerance here too (always better to be return something instead of None, if possible) return None # No intersection point (circles do not touch) if cos_gamma > 1.0: cos_gamma = 1.0 if cos_gamma < -1.0: cos_gamma = -1.0 sin_gamma = math.sqrt(1 - cos_gamma**2) u = v_ab / d_ab v = np.array([-u[1], u[0]]) pt1 = C_a + r_a * cos_gamma * u - r_a * sin_gamma * v pt2 = C_a + r_a * cos_gamma * u + r_a * sin_gamma * v return np.array([pt1, pt2]) def vector_angle_in_degrees(v: Point2DInternal) -> float: """ Given a vector, returns its elevation angle in degrees (-180..180). >>> vector_angle_in_degrees([1, 0]) 0.0 >>> vector_angle_in_degrees([1, 1]) 45.0 >>> vector_angle_in_degrees([0, 1]) 90.0 >>> vector_angle_in_degrees([-1, 1]) 135.0 >>> vector_angle_in_degrees([-1, 0]) 180.0 >>> vector_angle_in_degrees([-1, -1]) -135.0 >>> vector_angle_in_degrees([0, -1]) -90.0 >>> vector_angle_in_degrees([1, -1]) -45.0 """ return math.atan2(v[1], v[0]) * 180 / np.pi def normalize_by_center_of_mass(coords: np.ndarray, radii: np.ndarray) -> np.ndarray: """ Given coordinates of circle centers and radii, as two arrays, returns new coordinates array, computed such that the center of mass of the three circles is (0, 0). >>> 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])) array([[-1., -1.], [ 1., -1.], [ 0., 2.]]) >>> 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)])) array([[-1., -1.], [ 1., -1.], [ 0., 1.]]) """ # Now find the center of mass. radii = radii**2 sum_r = np.sum(radii) if sum_r < NUMERIC_TOLERANCE: return coords else: return coords - np.dot(radii, coords) / sum_r ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722419543.0 matplotlib-venn-1.1.1/matplotlib_venn/_region.py0000644000175000017500000007027214652404527023611 0ustar00konstantintkonstantint""" Venn diagram plotting routines. Math for computing with venn diagram regions. Copyright 2014-2024, Konstantin Tretyakov. http://kt.era.ee/ Licensed under MIT license. The current logic of drawing the venn diagram is the following: - Position the circles. - Compute the regions of the diagram based on circles - Compute the position of the label within each region. - Create matplotlib PathPatch or Circle objects for each of the regions. This module contains functionality necessary for the second, third and fourth steps of this process. Note that the regions of an up to 3-circle Venn diagram may be of the following kinds: - No region - A circle - A 2, 3 or 4-arc "poly-arc-gon". (I.e. a polygon with up to 4 vertices, that are connected by circle arcs) - A set of two 3-arc-gons. We create each of the regions by starting with a circle, and then either intersecting or subtracting the second and the third circles. The classes below implement the region representation, the intersection/subtraction procedures and the conversion to matplotlib patches. In addition, each region type has a "label positioning" procedure assigned. """ from typing import Optional, Sequence, Tuple import warnings import numpy as np from matplotlib.patches import Patch, Circle, PathPatch, Path from matplotlib.path import Path from matplotlib_venn._math import ( Point2DInternal, NUMERIC_TOLERANCE, circle_circle_intersection, vector_angle_in_degrees, ) from matplotlib_venn._math import point_in_circle, box_product from matplotlib_venn._arc import Arc class VennRegionException(Exception): pass class VennRegion: """ This is a superclass of a Venn diagram region, defining the interface that has to be supported by the different region types. """ def subtract_and_intersect_circle( self, center: Point2DInternal, radius: float ) -> Tuple["VennRegion", "VennRegion"]: """ Given a circular region, compute two new regions: one obtained by subtracting the circle from this region, and another obtained by intersecting the circle with the region. In all implementations it is assumed that the circle to be subtracted is not completely within the current region without touching its borders, i.e. it will not form a "hole" when subtracted. Arguments: center (tuple): A two-element tuple-like, representing the coordinates of the center of the circle. radius (float): A nonnegative number, the radius of the circle. Returns: a tuple with two elements - the result of subtracting the circle, and the result of intersecting with the circle. """ raise NotImplementedError("Method not implemented") def label_position(self) -> Optional[np.ndarray]: """Compute the position of a label for this region and return it as a 1x2 numpy array (x, y). May return None if label is not applicable.""" raise NotImplementedError("Method not implemented") def size(self) -> float: """Return a number, representing the size of the region. It is not important that the number would be a precise measurement, as long as sizes of various regions can be compared to choose the largest one. """ raise NotImplementedError("Method not implemented") def make_patch(self) -> Optional[Patch]: """Create a matplotlib patch object, corresponding to this region. May return None if no patch has to be created.""" raise NotImplementedError("Method not implemented") def verify(self) -> None: """Self-verification routine for purposes of testing. Raises a VennRegionException if some inconsistencies of internal representation are discovered.""" raise NotImplementedError("Method not implemented") class VennEmptyRegion(VennRegion): """ An empty region. To save some memory, returns [self, self] on the subtract_and_intersect_circle operation. It is possible to create an empty region with a non-None label position, by providing it in the constructor. >>> v = VennEmptyRegion() >>> [a, b] = v.subtract_and_intersect_circle((1,2), 3) >>> assert a == v and b == v >>> assert v.label_position() is None >>> assert v.size() == 0 >>> assert v.make_patch() is None >>> assert v.is_empty() >>> v = VennEmptyRegion((0, 0)) >>> v.label_position().tolist() [0.0, 0.0] """ def __init__(self, label_pos: Optional[Point2DInternal] = None): self.label_pos = None if label_pos is None else np.asarray(label_pos, float) def subtract_and_intersect_circle( self, center: Point2DInternal, radius: float ) -> Tuple[VennRegion, VennRegion]: return (self, self) def size(self) -> float: return 0 def label_position(self) -> np.ndarray: return self.label_pos def make_patch(self) -> Optional[Patch]: return None def is_empty( self, ) -> bool: # We use this in tests as an equivalent of isinstance(VennEmptyRegion) return True def verify(self) -> None: pass class VennCircleRegion(VennRegion): """ A circle-shaped region. >>> vcr = VennCircleRegion((0, 0), 1) >>> vcr.size() 3.1415... >>> vcr.label_position().tolist() [0.0, 0.0] >>> vcr.make_patch() >>> sr, ir = vcr.subtract_and_intersect_circle((0.5, 0), 1) >>> assert abs(sr.size() + ir.size() - vcr.size()) < NUMERIC_TOLERANCE """ def __init__(self, center: Point2DInternal, radius: float): self.center = np.asarray(center, float) self.radius = abs(radius) if radius < -NUMERIC_TOLERANCE: raise VennRegionException("Circle with a negative radius is invalid") def subtract_and_intersect_circle( self, center: Point2DInternal, radius: float ) -> Tuple[VennRegion, VennRegion]: """Will throw a VennRegionException if the circle to be subtracted is completely inside and not touching the given region.""" # Check whether the target circle intersects us center = np.asarray(center, float) d = np.linalg.norm(center - self.center) if d > (radius + self.radius - NUMERIC_TOLERANCE): return [self, VennEmptyRegion()] # The circle does not intersect us elif d < NUMERIC_TOLERANCE: if radius > self.radius - NUMERIC_TOLERANCE: # We are completely covered by that circle or we are the same circle return (VennEmptyRegion(), self) else: # That other circle is inside us and smaller than us - we can't deal with it raise VennRegionException( "Invalid configuration of circular regions (holes are not supported)." ) else: # We *must* intersect the other circle. If it is not the case, then it is inside us completely, # and we'll complain. intersections = circle_circle_intersection( self.center, self.radius, center, radius ) if intersections is None: raise VennRegionException( "Invalid configuration of circular regions (holes are not supported)." ) elif ( np.all(abs(intersections[0] - intersections[1]) < NUMERIC_TOLERANCE) and self.radius < radius ): # There is a single intersection point (i.e. we are touching the circle), # the circle to be subtracted is not outside of us (this was checked before), and is larger than us. # This is a particular corner case that is not dealt with correctly by the general-purpose code below and must # be handled separately return (VennEmptyRegion(), self) else: # Otherwise the subtracted region is a 2-arc-gon # Before we need to convert the intersection points as angles wrt each circle. a_1 = vector_angle_in_degrees(intersections[0] - self.center) a_2 = vector_angle_in_degrees(intersections[1] - self.center) b_1 = vector_angle_in_degrees(intersections[0] - center) b_2 = vector_angle_in_degrees(intersections[1] - center) # We must take care of the situation where the intersection points happen to be the same if abs(b_1 - b_2) < NUMERIC_TOLERANCE: b_1 = b_2 - NUMERIC_TOLERANCE / 2 if abs(a_1 - a_2) < NUMERIC_TOLERANCE: a_2 = a_1 + NUMERIC_TOLERANCE / 2 # The subtraction is a 2-arc-gon [(AB, B-), (BA, A+)] s_arc1 = Arc(center, radius, b_1, b_2, False) s_arc2 = Arc(self.center, self.radius, a_2, a_1, True) subtraction = VennArcgonRegion([s_arc1, s_arc2]) # .. and the intersection is a 2-arc-gon [(AB, A+), (BA, B+)] i_arc1 = Arc(self.center, self.radius, a_1, a_2, True) i_arc2 = Arc(center, radius, b_2, b_1, True) intersection = VennArcgonRegion([i_arc1, i_arc2]) return (subtraction, intersection) def size(self) -> float: """ Return the area of the circle >>> VennCircleRegion((0, 0), 1).size() 3.1415... >>> VennCircleRegion((0, 0), 2).size() 12.56637... """ return np.pi * self.radius**2 def label_position(self) -> np.ndarray: """ The label should be positioned in the center of the circle >>> VennCircleRegion((0, 0), 1).label_position().tolist() [0.0, 0.0] >>> VennCircleRegion((-1.2, 3.4), 1).label_position().tolist() [-1.2, 3.4] """ return self.center def make_patch(self) -> Optional[Patch]: """ Returns the corresponding circular patch. >>> patch = VennCircleRegion((1, 2), 3).make_patch() >>> patch >>> patch.center.tolist(), patch.radius ([1.0, 2.0], 3.0) """ return Circle(self.center, self.radius) def verify(self) -> None: pass class VennArcgonRegion(VennRegion): """ A poly-arc region. Note that we essentially only support 2, 3 and 4 arced regions, whereas intersections and subtractions only work for 2-arc regions. """ def __init__(self, arcs: Sequence[Arc]): """ Create a poly-arc region given a list of Arc objects. The arcs list must be of length 2, 3 or 4. The arcs must form a closed polygon, i.e. the last point of each arc must be the first point of the next arc. The vertices of a 3 or 4-arcgon must be listed in a CCW order. Arcs must not intersect. This is not verified in the constructor, but a special verify() method can be used to check for validity. """ self.arcs = arcs def verify(self) -> None: """ Verify the correctness of the region arcs. Throws an VennRegionException if verification fails (or any other exception if it happens during verification). """ # Verify size of arcs list if len(self.arcs) < 2: raise VennRegionException("At least two arcs needed in a poly-arc region") if len(self.arcs) > 4: raise VennRegionException( "At most 4 arcs are supported currently for poly-arc regions" ) TRIG_TOL = ( 100 * NUMERIC_TOLERANCE ) # We need to use looser tolerance level here because conversion to angles and back is prone to large errors. # Verify connectedness of arcs for i in range(len(self.arcs)): if not np.all( self.arcs[i - 1].end_point() - self.arcs[i].start_point() < TRIG_TOL ): raise VennRegionException( "Arcs of an poly-arc-gon must be connected via endpoints" ) # Verify that arcs do not cross-intersect except at endpoints for i in range(len(self.arcs) - 1): for j in range(i + 1, len(self.arcs)): ips = self.arcs[i].intersect_arc(self.arcs[j]) for ip in ips: if not ( np.all(abs(ip - self.arcs[i].start_point()) < TRIG_TOL) or np.all(abs(ip - self.arcs[i].end_point()) < TRIG_TOL) ): raise VennRegionException( "Arcs of a poly-arc-gon may only intersect at endpoints" ) if ( len(ips) != 0 and (i - j) % len(self.arcs) > 1 and (j - i) % len(self.arcs) > 1 ): # Two non-consecutive arcs intersect. This is in general not good, but # may occasionally happen when all arcs inbetween have length 0. pass # raise VennRegionException("Non-consecutive arcs of a poly-arc-gon may not intersect") # Verify that vertices are ordered so that at each point the direction along the polyarc changes towards the left. # Note that this test only makes sense for polyarcs obtained using circle intersections & subtractions. # A "flower-like" polyarc may have its vertices ordered counter-clockwise yet the direction would turn to the right at each of them. for i in range(len(self.arcs)): prev_arc = self.arcs[i - 1] cur_arc = self.arcs[i] if ( box_product( prev_arc.direction_vector(prev_arc.to_angle), cur_arc.direction_vector(cur_arc.from_angle), ) < -NUMERIC_TOLERANCE ): raise VennRegionException( "Arcs must be ordered so that the direction at each vertex changes counter-clockwise" ) def subtract_and_intersect_circle( self, center: Point2DInternal, radius: float ) -> Tuple[Patch, Patch]: """ Circle subtraction / intersection only supported by 2-gon regions, otherwise a VennRegionException is thrown. In addition, such an exception will be thrown if the circle to be subtracted is completely within the region and forms a "hole". The result may be either a VennArcgonRegion or a VennMultipieceRegion (the latter happens when the circle "splits" a crescent in two). """ if len(self.arcs) != 2: raise VennRegionException( "Circle subtraction and intersection with poly-arc regions is currently only supported for 2-arc-gons." ) # In the following we consider the 2-arc-gon case. # Before we do anything, we check for a special case, where the circle of interest is one of the two circles forming the arcs. # In this case we can determine the answer quite easily. matching_arcs = [a for a in self.arcs if a.lies_on_circle(center, radius)] if len(matching_arcs) != 0: # If the circle matches a positive arc, the result is [empty, self], otherwise [self, empty] return ( [VennEmptyRegion(), self] if matching_arcs[0].direction else [self, VennEmptyRegion()] ) # Consider the intersection points of the circle with the arcs. # If any of the intersection points corresponds exactly to any of the arc's endpoints, we will end up with # a lot of messy special cases (as if the usual situation is not messy enough, eh). # To avoid that, we cheat by slightly increasing the circle's radius until this is not the case any more. center = np.asarray(center) illegal_intersections = [a.start_point() for a in self.arcs] while True: valid = True intersections = [a.intersect_circle(center, radius) for a in self.arcs] for ints in intersections: for pt in ints: for illegal_pt in illegal_intersections: if np.all(abs(pt - illegal_pt) < NUMERIC_TOLERANCE): valid = False if valid: break else: radius += NUMERIC_TOLERANCE # There must be an even number of those points in total. # (If this is not the case, then we have an unfortunate case with weird numeric errors [TODO: find examples and deal with it?]). # There are three possibilities with the following subcases: # I. No intersection points # a) The polyarc is completely within the circle. # result = [ empty, self ] # b) The polyarc is completely outside the circle. # result = [ self, empty ] # II. Four intersection points, two for each arc. Points x1, x2 for arc X and y1, y2 for arc Y, ordered along the arc. # a) The polyarc endpoints are both outside the circle. # result_subtraction = a combination of two 3-arc polyarcs: # 1: {X - start to x1, # x1 to y2 along circle (negative direction)), # Y - y2 to end} # 2: {Y start to y1, # y1 to x2 along circle (negative direction)), # X - x2 to end} # b) The polyarc endpoints are both inside the circle # same as above, but the "along circle" arc directions are flipped and subtraction/intersection parts are exchanged # III. Two intersection points # a) One arc, X, has two intersection points i & j, another arc, Y, has no intersection points # a.1) Polyarc endpoints are outside the circle # result_subtraction = {X from start to i, circle i to j (direction = negative), X j to end, Y} # result_intersection = {X i to j, circle j to i (direction = positive} # a.2) Polyarc endpoints are inside the circle # result_subtraction = {X i to j, circle j to i negative} # result_intersection = {X 0 to i, circle i to j positive, X j to end, Y} # 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. # call the arc that starts with the outside point X, the other arc Y. # result_subtraction = {X start to intersection, intersection to intersection along circle (negative direction), Y from intersection to end} # result_intersection = {X intersection to end, Y start to intersecton, intersection to intersecion along circle (positive)} center = np.asarray(center) intersections = [a.intersect_circle(center, radius) for a in self.arcs] if len(intersections[0]) == 0 and len(intersections[1]) == 0: # Case I if point_in_circle(self.arcs[0].start_point(), center, radius): # Case I.a) return (VennEmptyRegion(), self) else: # Case I.b) return (self, VennEmptyRegion()) elif len(intersections[0]) == 2 and len(intersections[1]) == 2: # Case II. a) or b) case_II_a = not point_in_circle(self.arcs[0].start_point(), center, radius) a1 = self.arcs[0].subarc_between_points(None, intersections[0][0]) a2 = Arc( center, radius, vector_angle_in_degrees(intersections[0][0] - center), vector_angle_in_degrees(intersections[1][1] - center), not case_II_a, ) a2.fix_360_to_0() a3 = self.arcs[1].subarc_between_points(intersections[1][1], None) piece1 = VennArcgonRegion([a1, a2, a3]) b1 = self.arcs[1].subarc_between_points(None, intersections[1][0]) b2 = Arc( center, radius, vector_angle_in_degrees(intersections[1][0] - center), vector_angle_in_degrees(intersections[0][1] - center), not case_II_a, ) b2.fix_360_to_0() b3 = self.arcs[0].subarc_between_points(intersections[0][1], None) piece2 = VennArcgonRegion([b1, b2, b3]) subtraction = VennMultipieceRegion([piece1, piece2]) c1 = self.arcs[0].subarc(a1.to_angle, b3.from_angle) c2 = b2.reversed() c3 = self.arcs[1].subarc(b1.to_angle, a3.from_angle) c4 = a2.reversed() intersection = VennArcgonRegion([c1, c2, c3, c4]) return ( (subtraction, intersection) if case_II_a else (intersection, subtraction) ) else: # Case III. Yuck. if len(intersections[0]) == 0 or len(intersections[1]) == 0: # Case III.a) x = 0 if len(intersections[0]) != 0 else 1 y = 1 - x if len(intersections[x]) != 2: warnings.warn( "Numeric precision error during polyarc intersection, case IIIa. Expect wrong results." ) intersections[x] = [ intersections[x][0], intersections[x][0], ] # This way we'll at least produce some result, although it will probably be wrong if not point_in_circle(self.arcs[0].start_point(), center, radius): # Case III.a.1) # result_subtraction = {X from start to i, circle i to j (direction = negative), X j to end, Y} a1 = self.arcs[x].subarc_between_points(None, intersections[x][0]) a2 = Arc( center, radius, vector_angle_in_degrees(intersections[x][0] - center), vector_angle_in_degrees(intersections[x][1] - center), False, ) a3 = self.arcs[x].subarc_between_points(intersections[x][1], None) a4 = self.arcs[y] subtraction = VennArcgonRegion([a1, a2, a3, a4]) # result_intersection = {X i to j, circle j to i (direction = positive)} b1 = self.arcs[x].subarc(a1.to_angle, a3.from_angle) b2 = a2.reversed() intersection = VennArcgonRegion([b1, b2]) return (subtraction, intersection) else: # Case III.a.2) # result_subtraction = {X i to j, circle j to i negative} a1 = self.arcs[x].subarc_between_points( intersections[x][0], intersections[x][1] ) a2 = Arc( center, radius, vector_angle_in_degrees(intersections[x][1] - center), vector_angle_in_degrees(intersections[x][0] - center), False, ) subtraction = VennArcgonRegion([a1, a2]) # result_intersection = {X 0 to i, circle i to j positive, X j to end, Y} b1 = self.arcs[x].subarc(None, a1.from_angle) b2 = a2.reversed() b3 = self.arcs[x].subarc(a1.to_angle, None) b4 = self.arcs[y] intersection = VennArcgonRegion([b1, b2, b3, b4]) return (subtraction, intersection) else: # Case III.b) if len(intersections[0]) == 2 or len(intersections[1]) == 2: warnings.warn( "Numeric precision error during polyarc intersection, case IIIb. Expect wrong results." ) # One of the arcs must start outside the circle, call it x x = ( 0 if not point_in_circle(self.arcs[0].start_point(), center, radius) else 1 ) y = 1 - x a1 = self.arcs[x].subarc_between_points(None, intersections[x][0]) a2 = Arc( center, radius, vector_angle_in_degrees(intersections[x][0] - center), vector_angle_in_degrees(intersections[y][0] - center), False, ) a3 = self.arcs[y].subarc_between_points(intersections[y][0], None) subtraction = VennArcgonRegion([a1, a2, a3]) b1 = self.arcs[x].subarc(a1.to_angle, None) b2 = self.arcs[y].subarc(None, a3.from_angle) b3 = a2.reversed() intersection = VennArcgonRegion([b1, b2, b3]) return (subtraction, intersection) def label_position(self) -> np.ndarray: # Position the label right inbetween the midpoints of the arcs midpoints = [a.mid_point() for a in self.arcs] # For two-arc regions take the usual average # For more than two arcs, use arc lengths as the weights. if len(self.arcs) == 2: return np.mean(midpoints, 0) else: lengths = [a.length_degrees() for a in self.arcs] avg = np.sum([mp * l for (mp, l) in zip(midpoints, lengths)], 0) return avg / np.sum(lengths) def size(self) -> float: """Return the area of the patch. The area can be computed using the standard polygon area formula + signed segment areas of each arc. """ polygon_area = 0 for a in self.arcs: polygon_area += box_product(a.start_point(), a.end_point()) polygon_area /= 2.0 return polygon_area + sum([a.sign * a.segment_area() for a in self.arcs]) def make_patch(self) -> Optional[Patch]: """ Retuns a matplotlib PathPatch representing the current region. """ path = [self.arcs[0].start_point()] for a in self.arcs: if a.direction: vertices = Path.arc(a.from_angle, a.to_angle).vertices else: vertices = Path.arc(a.to_angle, a.from_angle).vertices vertices = vertices[np.arange(len(vertices) - 1, -1, -1)] vertices = vertices * a.radius + a.center path = path + list(vertices[1:]) codes = [1] + [4] * ( len(path) - 1 ) # NB: We could also add a CLOSEPOLY code (and a random vertex) to the end return PathPatch(Path(path, codes)) class VennMultipieceRegion(VennRegion): """ A region containing several pieces. In principle, any number of pieces is supported, although no more than 2 should ever be needed in a 3-circle Venn diagram. Although subtraction/intersection are straightforward to implement we do not need those for matplotlib-venn, we raise exceptions in those methods. """ def __init__(self, pieces: Sequence[VennRegion]): """ Create a multi-piece region from a list of VennRegion objects. The list may be empty or contain a single item (although those regions can be converted to a VennEmptyRegion or a single region of the necessary type. """ self.pieces = pieces def label_position(self) -> np.ndarray: """ Find the largest region and position the label in that. """ reg_sizes = [(r.size(), r) for r in self.pieces] reg_sizes.sort() return reg_sizes[-1][1].label_position() def size(self) -> float: return sum([p.size() for p in self.pieces]) def make_patch(self) -> Optional[Patch]: """Currently only works if all the pieces are Arcgons. In this case returns a multiple-piece path. Otherwise throws an exception.""" paths = [p.make_patch().get_path() for p in self.pieces] vertices = np.concatenate([p.vertices for p in paths]) codes = np.concatenate([p.codes for p in paths]) return PathPatch(Path(vertices, codes)) def verify(self) -> None: for p in self.pieces: p.verify() ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722419543.0 matplotlib-venn-1.1.1/matplotlib_venn/_util.py0000644000175000017500000000405114652404527023273 0ustar00konstantintkonstantint""" Venn diagram plotting routines. Utility routines Copyright 2012-2024, Konstantin Tretyakov. http://kt.era.ee/ Licensed under MIT license. """ import warnings from matplotlib_venn._venn2 import venn2 from matplotlib_venn._venn3 import venn3 from matplotlib_venn.layout.venn2 import DefaultLayoutAlgorithm as Venn2Layout from matplotlib_venn.layout.venn3 import DefaultLayoutAlgorithm as Venn3Layout def venn2_unweighted( subsets, set_labels=("A", "B"), set_colors=("r", "g"), alpha=0.4, normalize_to=1.0, subset_areas=(1, 1, 1), ax=None, subset_label_formatter=None, ): """ This function is deprecated and will be removed in a future version. Use venn2(..., layout_algorithm=matplotlib_venn.layout.venn2.DefaultLayoutAlgorithm(fixed_subset_sizes=(1,1,1))) instead. """ warnings.warn( "venn2_unweighted is deprecated. Use venn2 with the appropriate layout_algorithm instead." ) return venn2( subsets, set_labels, set_colors, alpha, ax, subset_label_formatter=subset_label_formatter, layout_algorithm=Venn2Layout( normalize_to=normalize_to, fixed_subset_sizes=subset_areas ), ) def venn3_unweighted( subsets, set_labels=("A", "B", "C"), set_colors=("r", "g", "b"), alpha=0.4, normalize_to=1.0, subset_areas=(1, 1, 1, 1, 1, 1, 1), ax=None, subset_label_formatter=None, ): """ This function is deprecated and will be removed in a future version. Use venn3(..., layout_algorithm=matplotlib_venn.layout.venn3.DefaultLayoutAlgorithm(fixed_subset_sizes=(1,1,1,1,1,1,1))) instead. """ warnings.warn( "venn3_unweighted is deprecated. Use venn3 with the appropriate layout_algorithm instead." ) return venn3( subsets, set_labels, set_colors, alpha, ax, subset_label_formatter=subset_label_formatter, layout_algorithm=Venn3Layout( normalize_to=normalize_to, fixed_subset_sizes=subset_areas ), ) ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722419543.0 matplotlib-venn-1.1.1/matplotlib_venn/_venn2.py0000644000175000017500000002610714652404527023354 0ustar00konstantintkonstantint""" Venn diagram plotting routines. Two-circle venn plotter. Copyright 2012, Konstantin Tretyakov. http://kt.era.ee/ Licensed under MIT license. """ # Make sure we don't try to do GUI stuff when running tests import sys, os if "py.test" in os.path.basename(sys.argv[0]): # (XXX: Ugly hack) import matplotlib matplotlib.use("Agg") from typing import Any, Callable, Dict, Optional, Tuple, Union import numpy as np import warnings from collections import Counter from matplotlib.axes import Axes from matplotlib.patches import Circle from matplotlib.colors import ColorConverter from matplotlib.pyplot import gca from matplotlib_venn._math import Point2D from matplotlib_venn._common import VennDiagram, prepare_venn_axes, mix_colors from matplotlib_venn._region import VennRegion, VennCircleRegion from matplotlib_venn.layout.api import VennLayout, VennLayoutAlgorithm from matplotlib_venn.layout.venn2 import DefaultLayoutAlgorithm Venn2SubsetSizes = Tuple[float, float, float] def venn2_circles( subsets: Union[Tuple[set, set], Dict[str, float], Venn2SubsetSizes], normalize_to: Optional[float] = None, alpha: float = 1.0, color: Any = "black", linestyle: str = "solid", linewidth: float = 2.0, ax: Axes = None, layout_algorithm: Optional[VennLayoutAlgorithm] = None, **kwargs ): """ Plots only the two circles for the corresponding Venn diagram. Useful for debugging or enhancing the basic venn diagram. Args: subsets: Same as in `venn2`. normalize_to: Same as in `venn2`. alpha: The alpha parameter of the circle patches. color: The edgecolor of the circle patches (as understood by matplotlib). linestyle: The linestyle of the circle patches. linewidth: The line width of the circle patches. ax: Axis to draw upon, defaults to gca(). layout_algorithm: The layout algorithm to be used. Defaults to matplotlib_venn.layout.venn2.DefaultLayoutAlgorithm(normalize_to). **kwargs: passed as-is to matplotlib.patches.Circle. Returns: a list of two Circle patches plotted. >>> c = venn2_circles((1, 2, 3)) >>> c = venn2_circles({'10': 1, '01': 2, '11': 3}) # Same effect >>> c = venn2_circles([set([1,2,3,4]), set([2,3,4,5,6])]) # Also same effect """ if isinstance(subsets, dict): subsets = [subsets.get(t, 0) for t in ["10", "01", "11"]] elif len(subsets) == 2: subsets = _compute_subset_sizes(*subsets) if normalize_to is not None: if layout_algorithm is None: warnings.warn( "normalize_to is deprecated. Please use layout_algorithm=matplotlib_venn.layout.venn2.DefaultLayoutAlgorithm(normalize_to) instead." ) else: raise ValueError( "normalize_to is deprecated and may not be specified together with a custom layout algorithm." ) if layout_algorithm is None: layout_algorithm = DefaultLayoutAlgorithm(normalize_to=normalize_to or 1.0) layout = layout_algorithm(subsets) if ax is None: ax = gca() prepare_venn_axes(ax, layout.centers, layout.radii) result = [] for c, r in zip(layout.centers, layout.radii): circle = Circle( c.asarray(), r, alpha=alpha, edgecolor=color, facecolor="none", linestyle=linestyle, linewidth=linewidth, **kwargs ) ax.add_patch(circle) result.append(circle) return tuple(result) def venn2( subsets: Union[Tuple[set, set], Dict[str, float], Venn2SubsetSizes], set_labels: Optional[Tuple[str, str]] = ("A", "B"), set_colors: Tuple[Any, Any] = ("r", "g"), alpha: float = 0.4, normalize_to: Optional[float] = None, ax: Optional[Axes] = None, subset_label_formatter: Optional[Callable[[float], str]] = None, layout_algorithm: Optional[VennLayoutAlgorithm] = None, ): """Plots a 2-set area-weighted Venn diagram. Args: subsets: one of the following: - A tuple of two set objects. - A dict, providing relative sizes of the three diagram regions. The regions are identified via two-letter binary codes ('10', '01', '11'), hence a valid artgument could look like: {'01': 10, '11': 20}. Unmentioned codes are considered to map to 0. - A tuple with 3 numbers, denoting the sizes of the regions in the following order: (10, 01, 11). set_labels: An optional tuple of two strings - set labels. Set it to None to disable set labels. set_colors: A tuple of two color specifications, specifying the base colors of the two circles. The colors of circle intersection will be computed based on those. normalize_to: Deprecated. Use normalize_to argument of matplotlib_venn.layout.venn2.DefaultLayoutAlgorithm instead. ax: The axes to plot upon. Defaults to gca(). subset_label_formatter: A function that converts numeric subset sizes to strings to be shown on the subset patches in the diagram. Defaults to "str". layout_algorithm: The layout algorithm to determine the scale and position of the three circles. Defaults to matplotlib_venn.layout.venn2.DefaultLayoutAlgorithm(). Returns: a `VennDiagram` object that keeps references to the layout information, ``Text`` and ``Patch`` objects used on the plot. >>> from matplotlib_venn import * >>> v = venn2(subsets={'10': 1, '01': 1, '11': 1}, set_labels = ('A', 'B')) >>> c = venn2_circles(subsets=(1, 1, 1), linestyle='dashed') >>> v.get_patch_by_id('10').set_alpha(1.0) >>> v.get_patch_by_id('10').set_color('white') >>> v.get_label_by_id('10').set_text('Unknown') >>> v.get_label_by_id('A').set_text('Set A') You can provide sets themselves rather than subset sizes: >>> v = venn2(subsets=[set([1,2]), set([2,3,4,5])], set_labels = ('A', 'B')) >>> c = venn2_circles(subsets=[set([1,2]), set([2,3,4,5])], linestyle='dashed') >>> print("%0.2f" % (v.get_circle_radius(1)/v.get_circle_radius(0))) 1.41 """ if isinstance(subsets, dict): subsets = [subsets.get(t, 0) for t in ["10", "01", "11"]] elif len(subsets) == 2: subsets = _compute_subset_sizes(*subsets) if normalize_to is not None: if layout_algorithm is None: warnings.warn( "normalize_to is deprecated. Please use layout_algorithm=matplotlib_venn.layout.venn2.DefaultLayoutAlgorithm(normalize_to) instead." ) else: raise ValueError( "normalize_to is deprecated and may not be specified together with a custom layout algorithm." ) if layout_algorithm is None: layout_algorithm = DefaultLayoutAlgorithm(normalize_to=normalize_to or 1.0) layout = layout_algorithm(subsets, set_labels) return _render_layout( layout, subsets, set_labels, set_colors, alpha, ax, subset_label_formatter ) def _render_layout( layout: VennLayout, subsets: Venn2SubsetSizes, set_labels: Optional[Tuple[str, str]] = ("A", "B"), set_colors: Tuple[Any, Any] = ("r", "g"), alpha: float = 0.4, ax: Optional[Axes] = None, subset_label_formatter: Optional[Callable[[float], str]] = None, ) -> VennDiagram: """Renders the layout.""" if subset_label_formatter is None: subset_label_formatter = str if ax is None: ax = gca() prepare_venn_axes(ax, layout.centers, layout.radii) colors = _compute_colors(*set_colors) regions = _compute_regions(layout.centers, layout.radii) patches = [r.make_patch() for r in regions] for p, c in zip(patches, colors): if p is not None: p.set_facecolor(c) p.set_edgecolor("none") p.set_alpha(alpha) ax.add_patch(p) label_positions = [r.label_position() for r in regions] subset_labels = [ ( ax.text(lbl[0], lbl[1], subset_label_formatter(s), va="center", ha="center") if lbl is not None else None ) for (lbl, s) in zip(label_positions, subsets) ] if set_labels is not None: labels = [ ax.text(lbl.position.x, lbl.position.y, txt, size="large", **lbl.kwargs) for (lbl, txt) in zip(layout.set_labels_layout, set_labels) ] else: labels = None return VennDiagram(patches, subset_labels, labels, layout.centers, layout.radii) def _compute_regions( centers: Tuple[Point2D, Point2D], radii: Tuple[float, float] ) -> Tuple[VennRegion, VennRegion, VennRegion]: """ Returns a triple of VennRegion objects, describing the three regions of the diagram, corresponding to sets (Ab, aB, AB) >>> layout = DefaultLayoutAlgorithm()((1, 1, 0.5)) >>> regions = _compute_regions(layout.centers, layout.radii) """ A = VennCircleRegion(centers[0].asarray(), radii[0]) B = VennCircleRegion(centers[1].asarray(), radii[1]) Ab, AB = A.subtract_and_intersect_circle(B.center, B.radius) aB, _ = B.subtract_and_intersect_circle(A.center, A.radius) return (Ab, aB, AB) def _compute_colors( color_a: Any, color_b: Any ) -> Tuple[np.ndarray, np.ndarray, np.ndarray]: """ Given two base colors, computes combinations of colors corresponding to all regions of the venn diagram. returns a list of 3 elements, providing colors for regions (10, 01, 11). >>> str(_compute_colors('r', 'g')).replace(' ', '') '(array([1.,0.,0.]),array([0.,0.5,0.]),array([0.7,0.35,0.]))' """ ccv = ColorConverter() base_colors = [np.array(ccv.to_rgb(c)) for c in [color_a, color_b]] return (base_colors[0], base_colors[1], mix_colors(base_colors[0], base_colors[1])) def _compute_subset_sizes( a: Union[set, Counter], b: Union[set, Counter] ) -> Tuple[float, float, float]: """ Given two set or Counter objects, computes the sizes of (a & ~b, b & ~a, a & b). Returns the result as a tuple. >>> _compute_subset_sizes(set([1,2,3,4]), set([2,3,4,5,6])) (1, 2, 3) >>> _compute_subset_sizes(Counter([1,2,3,4]), Counter([2,3,4,5,6])) (1, 2, 3) >>> _compute_subset_sizes(Counter([]), Counter([])) (0, 0, 0) >>> _compute_subset_sizes(set([]), set([])) (0, 0, 0) >>> _compute_subset_sizes(set([1]), set([])) (1, 0, 0) >>> _compute_subset_sizes(set([1]), set([1])) (0, 0, 1) >>> _compute_subset_sizes(Counter([1]), Counter([1])) (0, 0, 1) >>> _compute_subset_sizes(set([1,2]), set([1])) (1, 0, 1) >>> _compute_subset_sizes(Counter([1,1,2,2,2]), Counter([1,2,3,3])) (3, 2, 2) >>> _compute_subset_sizes(Counter([1,1,2]), Counter([1,2,2])) (1, 1, 2) >>> _compute_subset_sizes(Counter([1,1]), set([])) Traceback (most recent call last): ... ValueError: Both arguments must be of the same type """ if not (type(a) == type(b)): raise ValueError("Both arguments must be of the same type") set_size = ( len if type(a) != Counter else lambda x: sum(x.values()) ) # We cannot use len to compute the cardinality of a Counter return (set_size(a - b), set_size(b - a), set_size(a & b)) ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722445724.0 matplotlib-venn-1.1.1/matplotlib_venn/_venn3.py0000644000175000017500000003523014652467634023362 0ustar00konstantintkonstantint""" Venn diagram plotting routines. Three-circle venn plotter. Copyright 2012-2024, Konstantin Tretyakov. http://kt.era.ee/ Licensed under MIT license. """ from typing import Any, Callable, Dict, Optional, Tuple, Union import numpy as np import warnings from collections import Counter from matplotlib.axes import Axes from matplotlib.patches import Circle, PathPatch from matplotlib.path import Path from matplotlib.colors import ColorConverter from matplotlib.pyplot import gca from matplotlib_venn._math import circle_circle_intersection, NUMERIC_TOLERANCE, Point2D from matplotlib_venn._common import VennDiagram, prepare_venn_axes, mix_colors from matplotlib_venn._region import VennRegion, VennCircleRegion, VennEmptyRegion from matplotlib_venn.layout.api import VennLayout, VennLayoutAlgorithm from matplotlib_venn.layout.venn3 import DefaultLayoutAlgorithm Venn3SubsetSizes = Tuple[float, float, float, float, float, float, float] def venn3_circles( subsets: Union[Tuple[set, set, set], Dict[str, float], Venn3SubsetSizes], normalize_to: Optional[float] = None, alpha: float = 1.0, color: Any = "black", linestyle: str = "solid", linewidth: str = 2.0, ax: Optional[Axes] = None, layout_algorithm: Optional[VennLayoutAlgorithm] = None, **kwargs ) -> Tuple[Circle, Circle, Circle]: """ Plots only the three circles for the corresponding Venn diagram. Useful for debugging or enhancing the basic venn diagram. Args: subsets: Same as in `venn3`. normalize_to: Same as in `venn3`. alpha: The alpha parameter of the circle patches. color: The edgecolor of the circle patches (as understood by matplotlib). linestyle: The linestyle of the circle patches. linewidth: The line width of the circle patches. ax: Axis to draw upon, defaults to gca(). layout_algorithm: The layout algorithm to be used. Defaults to matplotlib_venn.layout.venn3.DefaultLayoutAlgorithm(normalize_to). **kwargs: passed as-is to matplotlib.patches.Circle. Returns: a list of three Circle patches plotted. >>> plot = venn3_circles({'001': 10, '100': 20, '010': 21, '110': 13, '011': 14}) >>> plot = venn3_circles([set(['A','B','C']), set(['A','D','E','F']), set(['D','G','H'])]) """ # Prepare parameters if isinstance(subsets, dict): subsets = [ subsets.get(t, 0) for t in ["100", "010", "110", "001", "101", "011", "111"] ] elif len(subsets) == 3: subsets = _compute_subset_sizes(*subsets) if normalize_to is not None: if layout_algorithm is None: warnings.warn( "normalize_to is deprecated. Please use layout_algorithm=matplotlib_venn.layout.venn3.DefaultLayoutAlgorithm(normalize_to) instead." ) else: raise ValueError( "normalize_to is deprecated and may not be specified together with a custom layout algorithm." ) if layout_algorithm is None: layout_algorithm = DefaultLayoutAlgorithm(normalize_to=normalize_to or 1.0) layout = layout_algorithm(subsets) if ax is None: ax = gca() prepare_venn_axes(ax, layout.centers, layout.radii) result = [] for c, r in zip(layout.centers, layout.radii): circle = Circle( c.asarray(), r, alpha=alpha, edgecolor=color, facecolor="none", linestyle=linestyle, linewidth=linewidth, **kwargs ) ax.add_patch(circle) result.append(circle) return tuple(result) def venn3( subsets: Union[Tuple[set, set, set], Dict[str, float], Venn3SubsetSizes], set_labels: Optional[Tuple[str, str, str]] = ("A", "B", "C"), set_colors: Tuple[Any, Any, Any] = ("r", "g", "b"), alpha: float = 0.4, normalize_to: Optional[float] = None, ax: Optional[Axes] = None, subset_label_formatter: Optional[Callable[[float], str]] = None, layout_algorithm: Optional[VennLayoutAlgorithm] = None, ) -> VennDiagram: """Plots a 3-set area-weighted Venn diagram. Note: if some of the circles happen to have zero area, you will probably not get a nice picture. Args: subsets: one of the following: - A tuple of three set objects. - A dict, providing relative sizes of the seven diagram regions. The regions are identified via three-letter binary codes ('100', '010', etc), hence a valid artgument could look like: {'001': 10, '010': 20, '110':30, ...}. Unmentioned codes are considered to map to 0. - A tuple with 7 numbers, denoting the sizes of the regions in the following order: (100, 010, 110, 001, 101, 011, 111). set_labels: An optional tuple of three strings - set labels. Set it to None to disable set labels. set_colors: A tuple of three color specifications, specifying the base colors of the three circles. The colors of circle intersections will be computed based on those. normalize_to: Deprecated. Use normalize_to argument of matplotlib_venn.layout.venn3.DefaultLayoutAlgorithm instead. ax: The axes to plot upon. Defaults to gca(). subset_label_formatter: A function that converts numeric subset sizes to strings to be shown on the subset patches in the diagram. Defaults to "str". layout_algorithm: The layout algorithm to determine the scale and position of the three circles. Defaults to matplotlib_venn.layout.venn3.DefaultLayoutAlgorithm(). Returns: a `VennDiagram` object that keeps references to the layout information, ``Text`` and ``Patch`` objects used on the plot. >>> import matplotlib # (The first two lines prevent the doctest from falling when TCL not installed. Not really necessary in most cases) >>> matplotlib.use('Agg') >>> from matplotlib_venn import * >>> v = venn3(subsets=(1, 1, 1, 1, 1, 1, 1), set_labels = ('A', 'B', 'C')) >>> c = venn3_circles(subsets=(1, 1, 1, 1, 1, 1, 1), linestyle='dashed') >>> v.get_patch_by_id('100').set_alpha(1.0) >>> v.get_patch_by_id('100').set_color('white') >>> v.get_label_by_id('100').set_text('Unknown') >>> v.get_label_by_id('C').set_text('Set C') You can provide sets themselves rather than subset sizes: >>> v = venn3(subsets=[set([1,2]), set([2,3,4,5]), set([4,5,6,7,8,9,10,11])]) >>> 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))) 0.24 1.41 2.00 >>> c = venn3_circles(subsets=[set([1,2]), set([2,3,4,5]), set([4,5,6,7,8,9,10,11])]) """ # Prepare parameters if isinstance(subsets, dict): subsets = [ subsets.get(t, 0) for t in ["100", "010", "110", "001", "101", "011", "111"] ] elif len(subsets) == 3: subsets = _compute_subset_sizes(*subsets) if normalize_to is not None: if layout_algorithm is None: warnings.warn( "normalize_to is deprecated. Please use layout_algorithm=matplotlib_venn.layout.venn3.DefaultLayoutAlgorithm(normalize_to) instead." ) else: raise ValueError( "normalize_to is deprecated and may not be specified together with a custom layout algorithm." ) if layout_algorithm is None: layout_algorithm = DefaultLayoutAlgorithm(normalize_to=normalize_to or 1.0) layout = layout_algorithm(subsets, set_labels) return _render_layout( layout, subsets, set_labels, set_colors, alpha, ax, subset_label_formatter ) def _render_layout( layout: VennLayout, subsets: Venn3SubsetSizes, set_labels: Optional[Tuple[str, str, str]] = ("A", "B", "C"), set_colors: Tuple[Any, Any, Any] = ("r", "g", "b"), alpha: float = 0.4, ax: Optional[Axes] = None, subset_label_formatter: Optional[Callable[[float], str]] = None, ) -> VennDiagram: """Given a VennLayout and the relevant rendering information, generates the diagram.""" if subset_label_formatter is None: subset_label_formatter = str if ax is None: ax = gca() prepare_venn_axes(ax, layout.centers, layout.radii) colors = _compute_colors(*set_colors) regions = list(_compute_regions(layout.centers, layout.radii)) # Remove regions that are too small from the diagram MIN_REGION_SIZE = 1e-4 for i in range(len(regions)): if regions[i].size() < MIN_REGION_SIZE and subsets[i] == 0: regions[i] = VennEmptyRegion() # There is a rare case (Issue #12) when the middle region is visually empty # (the positioning of the circles does not let them intersect), yet the corresponding value is not 0. # we address it separately here by positioning the label of that empty region in a custom way if isinstance(regions[6], VennEmptyRegion) and subsets[6] > 0: intersections = [ circle_circle_intersection( layout.centers[i].asarray(), layout.radii[i] + 0.001, layout.centers[j].asarray(), layout.radii[j] + 0.001, ) for (i, j) in [(0, 1), (1, 2), (2, 0)] ] middle_pos = np.mean([i[0] for i in intersections], 0) regions[6] = VennEmptyRegion(middle_pos) # Create and add patches and text patches = [r.make_patch() for r in regions] for p, c in zip(patches, colors): if p is not None: p.set_facecolor(c) p.set_edgecolor("none") p.set_alpha(alpha) ax.add_patch(p) label_positions = [r.label_position() for r in regions] subset_labels = [ ( ax.text(lbl[0], lbl[1], subset_label_formatter(s), va="center", ha="center") if lbl is not None else None ) for (lbl, s) in zip(label_positions, subsets) ] # Position set labels if set_labels is not None: labels = [ ax.text(lbl.position.x, lbl.position.y, txt, size="large", **lbl.kwargs) for (lbl, txt) in zip(layout.set_labels_layout, set_labels) ] else: labels = None return VennDiagram(patches, subset_labels, labels, layout.centers, layout.radii) def _compute_regions( centers: Tuple[Point2D, Point2D, Point2D], radii: Tuple[float, float, float] ) -> Tuple[ VennRegion, VennRegion, VennRegion, VennRegion, VennRegion, VennRegion, VennRegion ]: """ Given the three centers and radii of circles, returns the 7 regions, comprising the venn diagram, as VennRegion objects. Regions are returned in order (Abc, aBc, ABc, abC, AbC, aBC, ABC) >>> layout = DefaultLayoutAlgorithm()((1, 1, 1, 1, 1, 1, 1)) >>> regions = _compute_regions(layout.centers, layout.radii) """ A = VennCircleRegion(centers[0].asarray(), radii[0]) B = VennCircleRegion(centers[1].asarray(), radii[1]) C = VennCircleRegion(centers[2].asarray(), radii[2]) Ab, AB = A.subtract_and_intersect_circle(B.center, B.radius) ABc, ABC = AB.subtract_and_intersect_circle(C.center, C.radius) Abc, AbC = Ab.subtract_and_intersect_circle(C.center, C.radius) aB, _ = B.subtract_and_intersect_circle(A.center, A.radius) aBc, aBC = aB.subtract_and_intersect_circle(C.center, C.radius) aC, _ = C.subtract_and_intersect_circle(A.center, A.radius) abC, _ = aC.subtract_and_intersect_circle(B.center, B.radius) return (Abc, aBc, ABc, abC, AbC, aBC, ABC) def _compute_colors( color_a: Any, color_b: Any, color_c: Any ) -> Tuple[ np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray ]: """ Given three base colors, computes combinations of colors corresponding to all regions of the venn diagram. returns a list of 7 elements, providing colors for regions (100, 010, 110, 001, 101, 011, 111). >>> str(_compute_colors('r', 'g', 'b')).replace(' ', '') '(array([1.,0.,0.]),...,array([0.4,0.2,0.4]))' """ ccv = ColorConverter() base_colors = [np.array(ccv.to_rgb(c)) for c in [color_a, color_b, color_c]] return ( base_colors[0], base_colors[1], mix_colors(base_colors[0], base_colors[1]), base_colors[2], mix_colors(base_colors[0], base_colors[2]), mix_colors(base_colors[1], base_colors[2]), mix_colors(base_colors[0], base_colors[1], base_colors[2]), ) def _compute_subset_sizes( a: Union[set, Counter], b: Union[set, Counter], c: Union[set, Counter] ) -> Venn3SubsetSizes: """ Given three set or Counter objects, computes the sizes of (a & ~b & ~c, ~a & b & ~c, a & b & ~c, ....), as needed by the subsets parameter of venn3 and venn3_circles. Returns the result as a tuple. >>> _compute_subset_sizes(set([1,2,3]), set([2,3,4]), set([3,4,5,6])) (1, 0, 1, 2, 0, 1, 1) >>> _compute_subset_sizes(Counter([1,2,3]), Counter([2,3,4]), Counter([3,4,5,6])) (1, 0, 1, 2, 0, 1, 1) >>> _compute_subset_sizes(Counter([1,1,1]), Counter([1,1,1]), Counter([1,1,1,1])) (0, 0, 0, 1, 0, 0, 3) >>> _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])) (2, 0, 2, 4, 0, 2, 2) >>> _compute_subset_sizes(Counter([1,2,3]), Counter([2,2,3,3,4,4]), Counter([3,3,4,4,4,5,5,6])) (1, 1, 1, 4, 0, 3, 1) >>> _compute_subset_sizes(set([]), set([]), set([])) (0, 0, 0, 0, 0, 0, 0) >>> _compute_subset_sizes(set([1]), set([]), set([])) (1, 0, 0, 0, 0, 0, 0) >>> _compute_subset_sizes(set([]), set([1]), set([])) (0, 1, 0, 0, 0, 0, 0) >>> _compute_subset_sizes(set([]), set([]), set([1])) (0, 0, 0, 1, 0, 0, 0) >>> _compute_subset_sizes(Counter([]), Counter([]), Counter([1])) (0, 0, 0, 1, 0, 0, 0) >>> _compute_subset_sizes(set([1]), set([1]), set([1])) (0, 0, 0, 0, 0, 0, 1) >>> _compute_subset_sizes(set([1,3,5,7]), set([2,3,6,7]), set([4,5,6,7])) (1, 1, 1, 1, 1, 1, 1) >>> _compute_subset_sizes(Counter([1,3,5,7]), Counter([2,3,6,7]), Counter([4,5,6,7])) (1, 1, 1, 1, 1, 1, 1) >>> _compute_subset_sizes(Counter([1,3,5,7]), set([2,3,6,7]), set([4,5,6,7])) Traceback (most recent call last): ... ValueError: All arguments must be of the same type """ if not (type(a) == type(b) == type(c)): raise ValueError("All arguments must be of the same type") set_size = ( len if type(a) != Counter else lambda x: sum(x.values()) ) # We cannot use len to compute the cardinality of a Counter return ( set_size( a - (b | c) ), # TODO: This is certainly not the most efficient way to compute. set_size(b - (a | c)), set_size((a & b) - c), set_size(c - (a | b)), set_size((a & c) - b), set_size((b & c) - a), set_size(a & b & c), ) ././@PaxHeader0000000000000000000000000000003400000000000010212 xustar0028 mtime=1722463586.3418362 matplotlib-venn-1.1.1/matplotlib_venn/layout/0000755000175000017500000000000014652532542023121 5ustar00konstantintkonstantint././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722419543.0 matplotlib-venn-1.1.1/matplotlib_venn/layout/__init__.py0000644000175000017500000000000014652404527025221 0ustar00konstantintkonstantint././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722444038.0 matplotlib-venn-1.1.1/matplotlib_venn/layout/api.py0000644000175000017500000000431614652464406024253 0ustar00konstantintkonstantint""" Specification of the "layout algorithm" interface. A layout algorithm is a method for mapping from subset sizes to circle centers, radii and label locations. Copyright 2024, Konstantin Tretyakov. http://kt.era.ee/ Licensed under MIT license. """ from typing import Any, Dict, Sequence, Optional from abc import ABC, abstractmethod from matplotlib_venn._math import Point2D SubsetSizes = Sequence[float] # .. of length 3 for venn2 and length 7 for venn3 # Failures that may be reported from the layout algorithm. class LayoutException(Exception): pass class LabelLayout: """Text label position in the diagram. Given via coordinates and a set of keyword arguments (e.g. "ha" or "va"). """ def __init__(self, position: Point2D, kwargs: Dict[str, Any]): self.position = position self.kwargs = kwargs class VennLayout: """The circle layout specification for a Venn diagram.""" # Centers of the (2 / 3) circles (in the Axes coordinates). # centers: Sequence[Point2D] # # Radii of the circles. # radii: Sequence[float] # # Layout information of set labels. If labels are missing, then None. # set_labels_layout: Optional[Sequence[LabelLayout]] = None def __init__( self, centers: Sequence[Point2D], radii: Sequence[float], set_labels_layout: Optional[Sequence[LabelLayout]] = None, ): self.centers = centers self.radii = radii self.set_labels_layout = set_labels_layout class VennLayoutAlgorithm(ABC): """Interface for a Venn layout algorithm.""" @abstractmethod def __call__( self, subsets: SubsetSizes, set_labels: Optional[Sequence[str]] = None, ) -> VennLayout: """Lay out the Venn circles, returning the diagram layout specification as VennLayout. Args: subsets: A tuple with 3 (for venn2) or 7 (for venn3) numbers, denoting the sizes of the Venn diagram regions in the following order: for venn2: (10, 01, 11) for venn3: (100, 010, 110, 001, 101, 011, 111). set_labels: Optional tuple of set labels. If None, resulting layout provides no label information. """ pass ././@PaxHeader0000000000000000000000000000003400000000000010212 xustar0028 mtime=1722463586.3418362 matplotlib-venn-1.1.1/matplotlib_venn/layout/venn2/0000755000175000017500000000000014652532542024151 5ustar00konstantintkonstantint././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722419543.0 matplotlib-venn-1.1.1/matplotlib_venn/layout/venn2/__init__.py0000644000175000017500000000017714652404527026270 0ustar00konstantintkonstantintfrom matplotlib_venn.layout.venn2.exact import LayoutAlgorithm as DefaultLayoutAlgorithm __all__ = ["DefaultLayoutAlgorithm"] ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722429774.0 matplotlib-venn-1.1.1/matplotlib_venn/layout/venn2/exact.py0000644000175000017500000001130114652430516025621 0ustar00konstantintkonstantint""" The exact area-weighted layout algorithm implementation. This is the default, original layout method. Copyright 2012-2024, Konstantin Tretyakov. http://kt.era.ee/ Licensed under MIT license. """ from typing import Optional, Sequence import warnings import numpy as np from matplotlib_venn._math import ( NUMERIC_TOLERANCE, Point2D, find_distance_by_area, normalize_by_center_of_mass, ) from matplotlib_venn.layout.api import ( LabelLayout, VennLayout, VennLayoutAlgorithm, SubsetSizes, ) # The format is the same but the semantics is different. VennAreas = SubsetSizes class LayoutAlgorithm(VennLayoutAlgorithm): def __init__( self, normalize_to: float = 1.0, fixed_subset_sizes: Optional[SubsetSizes] = None, ): """Initialize the layout algorithm. Args: normalize_to: Specifies the total (on-axes) area of the circles to be drawn. Sometimes tuning it (together with the overall figure size) can be useful to fit the text labels better. fixed_subset_sizes: If specified, the layout will always use these subset sizes, ignoring anything provided to the actual __call__. E.g. passing (1,1,1) here will result in a non-area-weighted layout algorithm. """ self._normalize_to = normalize_to self._fixed_subset_sizes = fixed_subset_sizes def __call__( self, subsets: SubsetSizes, set_labels: Optional[Sequence[str]] = None, # Not used in the layout algorithm ) -> VennLayout: if self._fixed_subset_sizes is not None: subsets = self._fixed_subset_sizes areas = _compute_areas(subsets, self._normalize_to) return _compute_layout(areas) def _compute_areas( subset_sizes: SubsetSizes, normalize_to: float = 1.0, _minimal_area: float = 1e-6 ) -> VennAreas: """ Convert the sizes of individual regions (Ab, aB, AB) into areas (A, B, AB), used to lay out the diagram, normalizing the areas to sum to a given number. If total area was 0, returns (1e-06, 1e-06, 0.0) Assumes all input values are nonnegative (to be more precise, all areas are passed through and abs() function) >>> _compute_areas((1, 1, 0)) (0.5, 0.5, 0.0) >>> _compute_areas((0, 0, 0)) (1e-06, 1e-06, 0.0) >>> _compute_areas((1, 1, 1), normalize_to=3) (2.0, 2.0, 1.0) >>> _compute_areas((1, 2, 3), normalize_to=6) (4.0, 5.0, 3.0) """ # Normalize input values to sum to 1 areas = np.array(np.abs(subset_sizes), float) total_area = np.sum(areas) if abs(total_area) < NUMERIC_TOLERANCE: warnings.warn("Both circles have zero area") return (1e-06, 1e-06, 0.0) else: areas = areas / total_area * normalize_to return (float(areas[0] + areas[2]), float(areas[1] + areas[2]), float(areas[2])) def _compute_layout(venn_areas: VennAreas) -> VennLayout: """ Given the list of "venn areas" (as output from compute_venn2_areas, i.e. [A, B, AB]), finds the positions and radii of the two circles. Assumes the input values to be nonnegative and not all zero. In particular, the first two values must be positive. >>> layout = _compute_layout((1, 1, 0)) >>> np.round(layout.radii, 3).tolist() [0.564, 0.564] >>> layout = _compute_layout(_compute_areas((1, 2, 3))) >>> np.round(layout.radii, 3).tolist() [0.461, 0.515] """ (A_a, A_b, A_ab) = list(map(float, venn_areas)) r_a, r_b = np.sqrt(A_a / np.pi), np.sqrt(A_b / np.pi) radii = np.array([r_a, r_b]) if A_ab > NUMERIC_TOLERANCE: # Nonzero intersection coords = np.zeros((2, 2)) coords[1][0] = find_distance_by_area(radii[0], radii[1], A_ab) else: # Zero intersection coords = np.zeros((2, 2)) coords[1][0] = ( radii[0] + radii[1] + max(np.mean(radii) * 1.1, 0.2) ) # The max here is needed for the case r_a = r_b = 0 coords = normalize_by_center_of_mass(coords, radii) layout = VennLayout( (Point2D(*coords[0]), Point2D(*coords[1])), (radii[0], radii[1]) ) _compute_set_labels_positions(layout) return layout def _compute_set_labels_positions(layout: VennLayout): """Updates the set_labels_positions field of the given layout object.""" padding = np.mean([r * 0.1 for r in layout.radii]) layout.set_labels_layout = ( LabelLayout( position=layout.centers[0] + Point2D(0.0, -layout.radii[0] - padding), kwargs={"ha": "right", "va": "top"}, ), LabelLayout( position=layout.centers[1] + Point2D(0.0, -layout.radii[1] - padding), kwargs={"ha": "left", "va": "top"}, ), ) ././@PaxHeader0000000000000000000000000000003400000000000010212 xustar0028 mtime=1722463586.3418362 matplotlib-venn-1.1.1/matplotlib_venn/layout/venn3/0000755000175000017500000000000014652532542024152 5ustar00konstantintkonstantint././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722419543.0 matplotlib-venn-1.1.1/matplotlib_venn/layout/venn3/__init__.py0000644000175000017500000000021314652404527026260 0ustar00konstantintkonstantintfrom matplotlib_venn.layout.venn3.pairwise import ( LayoutAlgorithm as DefaultLayoutAlgorithm, ) __all__ = ["DefaultLayoutAlgorithm"] ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722463475.0 matplotlib-venn-1.1.1/matplotlib_venn/layout/venn3/cost_based.py0000644000175000017500000003205314652532363026636 0ustar00konstantintkonstantint""" Cost optimization-based layout algorithm for 3-way Venn diagrams. Unlike the rest of the code in the package, this implementation depends on the shapely library. To include the dependency, the library should be installed as ``` pip install 'matplotlib-venn[shapely]' ``` (Shapely will quite probably become a core dependency in a future version). Usage ----- This layout algorithm makes most sense in the cases when the default, "pairwise" layout does not work well enough for your data (which is usually the case for very skewed subset sizes). In this case just try doing: >>> from matplotlib_venn.layout.venn3 import cost_based >>> from matplotlib_venn import venn3 >>> subset_sizes = (100,200,10000,10,20,3,1) >>> venn3(subset_sizes, layout_algorithm=cost_based.LayoutAlgorithm()) You may further tune the behaviour of the algorithm by redefining the cost function. By default the algorithm tries to optimize the sum of |log(1+target_size)-log(1+actual_size)| over all 7 regions. If for some reason you believe |target_size - actual_size| should work better for your case, you can achieve it as follows: >>> alg = cost_based.LayoutAlgorithm(cost_fn=cost_based.WeightedAggregateCost(transform_fn=lambda x: x)) >>> venn3(subset_sizes, layout_algorithm=alg) Alternatively, you may want the optimization to give more weight to some of the regions or even ignore some of the larger ones. >>> alg = cost_based.LayoutAlgorithm(cost_fn=cost_based.WeightedAggregateCost(weights=(0,0,0,1,1,1,1))) >>> venn3(subset_sizes, layout_algorithm=alg) In theory, if the cost is defined as a difference in sizes of "pairwise" regions (AB, BC, AC), the result of optimizing it should be equivalent to what the default ('pairwise') algorithm does. To play with this idea, the module defines the respective `pairwise_cost` function. The result is not exactly the same as that of the default algorithm, but it would nearly always succeed, even when the default algorithm sometimes fails. E.g.: >>> subset_sizes = (1, 0, 0, 650, 0, 76, 13) >>> # Fails >>> venn3(subset_sizes) # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): matplotlib_venn._region.VennRegionException: Invalid configuration of circular regions (holes are not supported). >>> # Succeeds, producing what the default algorithm should have produced >>> venn3(subset_sizes, layout_algorithm=cost_based.LayoutAlgorithm(cost_fn=cost_based.pairwise_cost)) NB: This implementation is still in "alpha" stage, the code and behaviour may change in backwards-incompatible ways. Copyright 2024, Konstantin Tretyakov. http://kt.era.ee/ Based on a prototype by Paul Brodersen (https://github.com/konstantint/matplotlib-venn/issues/35). Licensed under MIT license. """ from typing import Callable, Optional, Sequence import warnings import numpy as np from shapely.geometry import Point from scipy.optimize import minimize, NonlinearConstraint from scipy.spatial.distance import pdist from matplotlib_venn._math import NUMERIC_TOLERANCE from matplotlib_venn.layout.venn3 import pairwise from matplotlib_venn.layout.api import ( LayoutException, Point2D, SubsetSizes, VennLayout, VennLayoutAlgorithm, ) def _initialize_centers(radii: np.ndarray) -> np.ndarray: """Initialize centers on a small circle around (0, 0). The centers are positioned at 90+60, 90-60, -90, matching the positioning logic of the pairwise algorithm. >>> centers = _initialize_centers(np.asarray([1, 2, 3])) >>> np.allclose(centers, \ np.array([[-0.866, 0.5], \ [ 0.866, 0.5], \ [ 0, -1]]), atol=0.001) True """ angles = 2 * np.pi * np.array([1 / 12 + 1 / 3, 1 / 12, 1 / 12 + 2 / 3]) return np.vstack([np.cos(angles), np.sin(angles)]).T * np.min(radii) def _normalize_subset_sizes( subset_sizes: SubsetSizes, normalize_to: float = 1.0 ) -> SubsetSizes: """Normalize provided subset sizes to areas with total area equal to . If the total area is less than _minimal_area falls back to a normalized version of the 'unweighted' set of areas (1,1,1,1,1,1,1). >>> _normalize_subset_sizes((1,0,0,0,0,0,0)) array([1., 0., 0., 0., 0., 0., 0.]) >>> _normalize_subset_sizes((1,1,0,0,0,0,0)) array([0.5, 0.5, 0. , 0. , 0. , 0. , 0. ]) >>> _normalize_subset_sizes((0,0,0,0,0,0,0)) array([0.14..., 0.14..., 0.14..., 0.14..., 0.14..., 0.14..., 0.14...]) """ areas = np.array(np.abs(subset_sizes), float) total_area = np.sum(areas) if np.abs(total_area) < NUMERIC_TOLERANCE: warnings.warn( "All regions have zero area. Falling back to an unweighted diagram." ) return _normalize_subset_sizes((1, 1, 1, 1, 1, 1, 1), normalize_to) else: return areas / total_area * normalize_to def _compute_radii(areas: SubsetSizes) -> np.ndarray: """Compute radii of the three circles based on a given SubsetSizes vector. Returns an array of three radii for the three circles. >>> regions = np.pi*np.array([1, 2, 0, 3, 0, 0, 0])**2 >>> _compute_radii(regions) array([1., 2., 3.]) >>> deltas = np.array([-0.3, -0.3, 0.1, -0.3, 0.1, 0.1, 0.1]) >>> _compute_radii(regions + deltas) array([1., 2., 3.]) """ (Abc, aBc, ABc, abC, AbC, aBC, ABC) = areas A = Abc + ABc + AbC + ABC B = aBc + ABc + aBC + ABC C = abC + AbC + aBC + ABC return np.sqrt(np.array([A, B, C]) / np.pi) def _compute_subset_areas(centers: np.ndarray, radii: np.ndarray) -> SubsetSizes: """Given centers and radii of a venn3 diagram, return the respective subset areas. >>> areas = _compute_subset_areas(np.asarray([[0,0], [2,2], [4,4]]), np.asarray([1, 1, 1])) >>> np.allclose(areas, [np.pi, np.pi, 0, np.pi, 0, 0, 0], atol=0.01) True >>> from matplotlib_venn.layout.venn3 import DefaultLayoutAlgorithm >>> layout = DefaultLayoutAlgorithm()((1,1,1,1,1,1,1)) >>> areas = _compute_subset_areas(\ np.asarray([c.asarray() for c in layout.centers]),\ np.asarray(layout.radii)) >>> layout = DefaultLayoutAlgorithm()(areas) >>> new_areas = _compute_subset_areas(\ np.asarray([c.asarray() for c in layout.centers]),\ np.asarray(layout.radii)) >>> np.allclose(areas, new_areas, atol=0.01) True """ a, b, c = [Point(*center).buffer(radius) for center, radius in zip(centers, radii)] regions = [ a.difference(b).difference(c), # Abc b.difference(a).difference(c), # aBc a.intersection(b).difference(c), # ABc c.difference(a).difference(b), # abC a.intersection(c).difference(b), # AbC b.intersection(c).difference(a), # aBC a.intersection(b).intersection(c), # ABC ] return np.array([region.area for region in regions]) # A cost function is a callable that accepts the desired subset sizes # and the actual sizes (for the current layout) and returns the cost ("loss") # of the discrepancy. CostFunction = Callable[[SubsetSizes, SubsetSizes], float] class WeightedAggregateCost(CostFunction): """A cost function that aggregates differences over all regions. The function computes: np.dot(weights, np.abs(fn(target_size) - fn(current_size))**power). >>> fn = WeightedAggregateCost() >>> fn([1]*7, [1]*7) 0.0 >>> fn([1]*7, [0]*7) 7.0 >>> fn = WeightedAggregateCost(lambda x: x**2) >>> fn([1]*7, [1]*7) 0.0 >>> fn([2]*7, [0]*7) 28.0 >>> fn = WeightedAggregateCost(weights=(1,2,3)) >>> fn([1,2,0], [0,0,3]) 14.0 >>> fn = WeightedAggregateCost(weights=(0,0,1), power=2) >>> fn([1,2,0], [0,0,3]) 9.0 """ def __init__( self, transform_fn: Callable[[np.ndarray], np.ndarray] = lambda x: x, weights: Sequence[float] = (1, 1, 1, 1, 1, 1, 1), power: float = 1, ): self.transform_fn = transform_fn self.weights = np.asarray(weights) self.power = power def __call__(self, target_areas: SubsetSizes, current_areas: SubsetSizes) -> float: targets = self.transform_fn(np.asarray(target_areas)) current = self.transform_fn(np.asarray(current_areas)) return float(np.dot(self.weights, np.abs(targets - current) ** self.power)) def pairwise_cost(target_areas: SubsetSizes, actual_areas: SubsetSizes) -> float: """The cost, computed as the absolute difference between pairwise (A&B, B&C, A&C) areas. This matches the logic of the default ("pairwise") layout algorithm and thus produces mostly the same results (not exactly the same due to some randomness involved in the iterative nature of the optimization). It is here primarily for experimentation and "completeness' sake". >>> pairwise_cost([1]*7, [1]*7) 0.0 >>> pairwise_cost([1]*7, (2,2,1,2,1,1,1)) 0.0 >>> pairwise_cost([1]*7, (2,2,1,2,1,1,1.5)) 1.5 >>> pairwise_cost([1]*7, (2,2,1.5,2,1,1,1)) 0.5 >>> pairwise_cost([1]*7, (2,2,1.5,2,1.1,1,1)) 0.6... """ (tAbc, taBc, tABc, tabC, tAbC, taBC, tABC) = target_areas (Abc, aBc, ABc, abC, AbC, aBC, ABC) = actual_areas dAB = tABC + tABc - (ABC + ABc) dBC = tABC + taBC - (ABC + aBC) dAC = tABC + tAbC - (ABC + AbC) return float(abs(dAB) + abs(dBC) + abs(dAC)) class LayoutAlgorithm(VennLayoutAlgorithm): """3-way Venn layout that positions circles by numerically optimizing a given discrepancy cost. >>> alg = LayoutAlgorithm() >>> layout = alg((1,1,1,1,1,1,1), ("A", "B", "C")) >>> layout.centers [Point2D(-0.13..., 0.07...), Point2D(0.13..., 0.077...), Point2D(-1..., -0.15...)] >>> layout.radii [0.42..., 0.42..., 0.42...] """ def __init__(self, cost_fn: Optional[CostFunction] = None, fallback: bool = True): """Initialize the cost-based layout algorithm. Args: cost_fn: A cost function to be optimized. Default is WeightedAggregateCost(lambda x: np.log(1 + x)). This has been determined to work well enough in practice. fallback: Whether to fall back to the default ("pairwise") layout algorithm if optimization does not converge. True by default. If there is no fallback, a LayoutException will be raised if optimization fails. """ self._cost_fn = cost_fn or WeightedAggregateCost(lambda x: np.log(1 + x)) self._fallback = fallback # This is a convenience field that will carry the result of the most # recent "minimize" call. self.last_optimization_result = None def __call__( self, subsets: SubsetSizes, set_labels: Optional[Sequence[str]] = None, ) -> VennLayout: target_areas = _normalize_subset_sizes(subsets) radii = _compute_radii(target_areas) centers = _initialize_centers(radii) # We will position the circles by optimizing this cost function ... def _cost_function(centers_flattened: np.ndarray) -> np.ndarray: """Computes the cost of positioning circles at given centers.""" current_areas = _compute_subset_areas( centers_flattened.reshape(-1, 2), radii ) return self._cost_fn(target_areas, current_areas) # ... while making sure the pairwise distances between circles do not exceed sum of radii: # (this is the order in which pdist computes pairwise distances). upper_bounds = np.array( [radii[0] + radii[1], radii[0] + radii[2], radii[1] + radii[2]] ) # ... and are not below differences between radii: lower_bounds = np.abs( np.array([radii[0] - radii[1], radii[0] - radii[2], radii[1] - radii[2]]) ) def _pairwise_distances(centers_flattened: np.ndarray) -> np.ndarray: return pdist(np.reshape(centers_flattened, (-1, 2))) result = minimize( _cost_function, centers.flatten(), method="SLSQP", constraints=[ NonlinearConstraint( _pairwise_distances, ub=upper_bounds, lb=lower_bounds ) ], ) self.last_optimization_result = result if not result.success: warnings.warn("Optimization failed: {0}".format(result.message)) if self._fallback: # Fall back to _pairwise return pairwise.LayoutAlgorithm()(subsets, set_labels) else: raise LayoutException("Optimization failed: {0}".format(result.message)) centers = result.x.reshape((-1, 2)) result = VennLayout( centers=[Point2D(*center) for center in centers], radii=list(map(float, radii)), ) # TODO: We reuse the pairwise algorithm implementation for set label positioning. # It does not always do the most correct job. pairwise._compute_set_labels_positions(result) return result ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722429813.0 matplotlib-venn-1.1.1/matplotlib_venn/layout/venn3/pairwise.py0000644000175000017500000002702214652430565026354 0ustar00konstantintkonstantint""" The pairwise intersection-based layout algorithm implementation. This is the default, original layout method. Makes sure the full circle areas and the areas of their pairwise intersections exactly match the subset areas. The area of the triple intersection is not necessarily correct. For situations where the triple intersection is too small in comparison to other areas it often results in bad layout. Copyright 2012-2024, Konstantin Tretyakov. http://kt.era.ee/ Licensed under MIT license. """ from typing import Optional, Tuple import warnings import numpy as np from matplotlib_venn._math import ( NUMERIC_TOLERANCE, Point2D, find_distance_by_area, normalize_by_center_of_mass, ) from matplotlib_venn.layout.api import ( LabelLayout, VennLayout, VennLayoutAlgorithm, SubsetSizes, ) # The format is the same but the semantics is different. VennAreas = SubsetSizes class LayoutAlgorithm(VennLayoutAlgorithm): def __init__( self, normalize_to: float = 1.0, fixed_subset_sizes: Optional[SubsetSizes] = None, ): """Initialize the layout algorithm. Args: normalize_to: Specifies the total (on-axes) area of the circles to be drawn. Sometimes tuning it (together with the overall figure size) can be useful to fit the text labels better. fixed_subset_sizes: If specified, the layout will always use these subset sizes, ignoring anything provided to the actual __call__. E.g. passing (1,1,1,1,1,1,1) here will result in a non-area-weighted layout algorithm. """ self._normalize_to = normalize_to self._fixed_subset_sizes = fixed_subset_sizes def __call__( self, subsets: SubsetSizes, set_labels: Optional[ Tuple[str, str, str] ] = None, # Not used in the layout algorithm. ) -> VennLayout: if self._fixed_subset_sizes is not None: subsets = self._fixed_subset_sizes areas = _compute_areas(subsets, self._normalize_to) return _compute_layout(areas) def _compute_areas( subset_sizes: SubsetSizes, normalize_to: float = 1.0, _minimal_area: float = 1e-6 ) -> VennAreas: """ Compute areas of circles and their pairwise and triple intersections. Assumes all input values are nonnegative (to be more precise, all areas are passed through the abs() function) Args: subset_sizes: The relative sizes of the 7 diagram region in the following order: (Abc, aBc, ABc, abC, AbC, aBC, ABC) (i.e. last element corresponds to the size of intersection A&B&C). normalize_to: Normalize the values so that the total area sums to this value. _minimal_area: If the area of any circle is smaller than _minimal_area, makes it equal to _minimal_area. Returns: A list of areas (A_a, A_b, A_c, A_ab, A_bc, A_ac, A_abc), such that the total area of all circles is normalized to normalize_to (except corrections for _minimal_area) >>> _compute_areas((1, 1, 0, 1, 0, 0, 0)) (0.33..., 0.33..., 0.33..., 0.0, 0.0, 0.0, 0.0) >>> _compute_areas((0, 0, 0, 0, 0, 0, 0)) (1e-06, 1e-06, 1e-06, 0.0, 0.0, 0.0, 0.0) >>> _compute_areas((1, 1, 1, 1, 1, 1, 1), normalize_to=7) (4.0, 4.0, 4.0, 2.0, 2.0, 2.0, 1.0) >>> _compute_areas((1, 2, 3, 4, 5, 6, 7), normalize_to=56/2) (16.0, 18.0, 22.0, 10.0, 13.0, 12.0, 7.0) """ # Normalize input values to sum to 1 areas = np.array(np.abs(subset_sizes), float) total_area = np.sum(areas) if abs(total_area) < _minimal_area: warnings.warn("All circles have zero area.") return (1e-06, 1e-06, 1e-06, 0.0, 0.0, 0.0, 0.0) else: areas = areas / total_area * normalize_to A_a = areas[0] + areas[2] + areas[4] + areas[6] if A_a < _minimal_area: warnings.warn("Circle A has zero area.") A_a = _minimal_area A_b = areas[1] + areas[2] + areas[5] + areas[6] if A_b < _minimal_area: warnings.warn("Circle B has zero area.") A_b = _minimal_area A_c = areas[3] + areas[4] + areas[5] + areas[6] if A_c < _minimal_area: warnings.warn("Circle C has zero area.") A_c = _minimal_area # Areas of the three intersections (ab, ac, bc) A_ab, A_ac, A_bc = areas[2] + areas[6], areas[4] + areas[6], areas[5] + areas[6] return tuple(map(float, (A_a, A_b, A_c, A_ab, A_bc, A_ac, areas[6]))) def _compute_layout(venn_areas: VennAreas) -> VennLayout: """ Given the list of "venn areas" (as output from _compute_areas, i.e. (A, B, C, AB, BC, AC, ABC)), finds the positions and radii of the three circles. Assumes the input values to be nonnegative and not all zero. In particular, the first three values must all be positive. The return value is a VennLayout struct with just the coords and radii fields. The overall match is only approximate (to be precise, what is matched are the areas of the circles and the three pairwise intersections). >>> layout = _compute_layout((1, 1, 1, 0, 0, 0, 0)) >>> np.round(layout.radii, 3).tolist() [0.564, 0.564, 0.564] >>> layout = _compute_layout(_compute_areas((1, 2, 40, 30, 4, 40, 4))) >>> np.round(layout.radii, 3).tolist() [0.359, 0.476, 0.453] """ (A_a, A_b, A_c, A_ab, A_bc, A_ac, A_abc) = list(map(float, venn_areas)) r_a, r_b, r_c = np.sqrt(A_a / np.pi), np.sqrt(A_b / np.pi), np.sqrt(A_c / np.pi) intersection_areas = [A_ab, A_bc, A_ac] radii = np.array([r_a, r_b, r_c]) # Hypothetical distances between circle centers that assure # that their pairwise intersection areas match the requirements. dists = [ find_distance_by_area(radii[i], radii[j], intersection_areas[i]) for (i, j) in [(0, 1), (1, 2), (2, 0)] ] # How many intersections have nonzero area? num_nonzero = sum(np.array([A_ab, A_bc, A_ac]) > NUMERIC_TOLERANCE) # Handle four separate cases: # 1. All pairwise areas nonzero # 2. Two pairwise areas nonzero # 3. One pairwise area nonzero # 4. All pairwise areas zero. if num_nonzero == 3: # The "generic" case, simply use dists to position circles at the vertices of a triangle. # Before we need to ensure that resulting circles can be at all positioned on a triangle, # use an ad-hoc fix. for i in range(3): i, j, k = (i, (i + 1) % 3, (i + 2) % 3) if dists[i] > dists[j] + dists[k]: a, b = (j, k) if dists[j] < dists[k] else (k, j) dists[i] = dists[b] + dists[a] * 0.8 warnings.warn("Bad circle positioning.") coords = _compute_triangle_layout_coords(radii, dists) elif num_nonzero == 2: # One pair of circles is not intersecting. # In this case we can position all three circles in a line # The two circles that have no intersection will be on either sides. for i in range(3): if intersection_areas[i] < NUMERIC_TOLERANCE: (left, right, middle) = (i, (i + 1) % 3, (i + 2) % 3) coords = np.zeros((3, 2)) coords[middle][0] = dists[middle] coords[right][0] = dists[middle] + dists[right] # We want to avoid the situation where left & right still intersect if coords[left][0] + radii[left] > coords[right][0] - radii[right]: mid = ( coords[left][0] + radii[left] + coords[right][0] - radii[right] ) / 2.0 coords[left][0] = mid - radii[left] - 1e-5 coords[right][0] = mid + radii[right] + 1e-5 break elif num_nonzero == 1: # Only one pair of circles is intersecting, and one circle is independent. # Position all on a line first two intersecting, then the free one. for i in range(3): if intersection_areas[i] > NUMERIC_TOLERANCE: (left, right, side) = (i, (i + 1) % 3, (i + 2) % 3) coords = np.zeros((3, 2)) coords[right][0] = dists[left] coords[side][0] = ( dists[left] + radii[right] + radii[side] * 1.1 ) # Pad by 10% break else: # All circles are non-touching. Put them all in a sequence coords = np.zeros((3, 2)) coords[1][0] = radii[0] + radii[1] * 1.1 coords[2][0] = radii[0] + radii[1] * 1.1 + radii[1] + radii[2] * 1.1 coords = normalize_by_center_of_mass(coords, radii) result = VennLayout( centers=( Point2D(coords[0][0], coords[0][1]), Point2D(coords[1][0], coords[1][1]), Point2D(coords[2][0], coords[2][1]), ), radii=(radii[0], radii[1], radii[2]), ) _compute_set_labels_positions(result) return result def _compute_triangle_layout_coords( radii: Tuple[float, float, float], dists: Tuple[float, float, float] ) -> np.ndarray: """ Finds three centers for circles which form a proper triangle with given side lengths. The method puts the center of A and B on a horizontal line y==0, and C just below. Args: radii: The radii of the three circles (r_a, r_b, r_c). dists: The pairwise distances between the circle centers (d_ab, d_bc, d_ac), Returns: Coordinates of the circles to be laid out. >>> _compute_triangle_layout_coords((1, 1, 1), (0, 0, 0)) array([[ 0., 0.], [ 0., 0.], [ 0., -0.]]) >>> _compute_triangle_layout_coords((1, 1, 1), (2, 2, 2)) array([[ 0. , 0. ], [ 2. , 0. ], [ 1. , -1.73205081]]) """ (d_ab, d_bc, d_ac) = dists (r_a, r_b, r_c) = radii coords = np.array([[0, 0], [d_ab, 0], [0, 0]], float) C_x = ( (d_ac**2 - d_bc**2 + d_ab**2) / 2.0 / d_ab if np.abs(d_ab) > NUMERIC_TOLERANCE else 0.0 ) C_y = -np.sqrt(d_ac**2 - C_x**2) coords[2, :] = C_x, C_y return coords def _compute_set_labels_positions(layout: VennLayout): """Updates the set_labels_positions field of the given layout object.""" if abs(layout.centers[2].y - layout.centers[0].y) > NUMERIC_TOLERANCE: # Three circles NOT on the same line layout.set_labels_layout = ( LabelLayout( position=layout.centers[0] + Point2D(-layout.radii[0] / 2, layout.radii[0]), kwargs={"ha": "right"}, ), LabelLayout( position=layout.centers[1] + Point2D(layout.radii[1] / 2, layout.radii[1]), kwargs={"ha": "left"}, ), LabelLayout( position=layout.centers[2] + Point2D(0.0, -layout.radii[2] * 1.1), kwargs={"ha": "center", "va": "top"}, ), ) else: # Three circles on the same line padding = np.mean([r * 0.1 for r in layout.radii]) layout.set_labels_layout = ( LabelLayout( position=layout.centers[0] + Point2D(0.0, -layout.radii[0] - padding), kwargs={"ha": "center", "va": "top"}, ), LabelLayout( position=layout.centers[1] + Point2D(0.0, -layout.radii[1] - padding), kwargs={"ha": "center", "va": "top"}, ), LabelLayout( position=layout.centers[2] + Point2D(0.0, -layout.radii[2] - padding), kwargs={"ha": "center", "va": "top"}, ), ) ././@PaxHeader0000000000000000000000000000003400000000000010212 xustar0028 mtime=1722463586.3418362 matplotlib-venn-1.1.1/matplotlib_venn.egg-info/0000755000175000017500000000000014652532542023276 5ustar00konstantintkonstantint././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722463586.0 matplotlib-venn-1.1.1/matplotlib_venn.egg-info/PKG-INFO0000644000175000017500000002137214652532542024400 0ustar00konstantintkonstantintMetadata-Version: 2.1 Name: matplotlib-venn Version: 1.1.1 Summary: Functions for plotting area-proportional two- and three-way Venn diagrams in matplotlib. Home-page: https://github.com/konstantint/matplotlib-venn Author: Konstantin Tretyakov Author-email: kt@umn.ee License: MIT Keywords: matplotlib plotting charts venn-diagrams Platform: Platform Independent Classifier: Development Status :: 5 - Production/Stable Classifier: Intended Audience :: Science/Research Classifier: License :: OSI Approved :: MIT License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python :: 3 Classifier: Topic :: Scientific/Engineering :: Visualization Provides-Extra: shapely License-File: LICENSE ==================================================== Venn diagram plotting routines for Python/Matplotlib ==================================================== .. image:: https://travis-ci.org/konstantint/matplotlib-venn.png?branch=master :target: https://travis-ci.org/konstantint/matplotlib-venn Routines for plotting area-weighted two- and three-circle venn diagrams. Installation ------------ Install the package as usual via ``pip``:: $ python -m pip install matplotlib-venn Since version 1.1.0 the package includes an extra "cost based" layout algorithm for `venn3` diagrams, that relies on the `shapely` package, which is not installed as a default dependency. If you need the new algorithm (or just have nothing against installing `shapely` along the way), instead do:: $ python -m pip install "matplotlib-venn[shapely]" It is quite probable that `shapely` will become a required dependency eventually in one of the future versions. Dependencies ------------ - ``numpy``, - ``scipy``, - ``matplotlib``, - ``shapely`` (optional). Usage ----- The package provides four main functions: ``venn2``, ``venn2_circles``, ``venn3`` and ``venn3_circles``. The functions ``venn2`` and ``venn2_circles`` accept as their only required argument a 3-element tuple ``(Ab, aB, AB)`` of subset sizes, and draw a two-circle venn diagram with respective region areas, e.g.:: venn2(subsets = (3, 2, 1)) In this example, the region, corresponding to subset ``A and not B`` will be three times larger in area than the region, corresponding to subset ``A and B``. You can also provide a tuple of two ``set`` or ``Counter`` (i.e. multi-set) objects instead (new in version 0.7), e.g.:: venn2((set(['A', 'B', 'C', 'D']), set(['D', 'E', 'F']))) Similarly, the functions ``venn3`` and ``venn3_circles`` take a 7-element tuple of subset sizes ``(Abc, aBc, ABc, abC, AbC, aBC, ABC)``, and draw a three-circle area-weighted Venn diagram: .. image:: https://user-images.githubusercontent.com/13646666/87874366-96924800-c9c9-11ea-8b06-ac1336506b59.png Alternatively, a tuple of three ``set`` or ``Counter`` objects may be provided. The functions ``venn2`` and ``venn3`` draw the diagrams as a collection of colored patches, annotated with text labels. The functions ``venn2_circles`` and ``venn3_circles`` draw just the circles. The functions ``venn2_circles`` and ``venn3_circles`` return the list of ``matplotlib.patch.Circle`` objects that may be tuned further to your liking. The functions ``venn2`` and ``venn3`` return an object of class ``VennDiagram``, which gives access to constituent patches, text elements, and (since version 0.7) the information about the centers and radii of the circles. Basic Example:: from matplotlib_venn import venn2 venn2(subsets = (3, 2, 1)) For the three-circle case:: from matplotlib_venn import venn3 venn3(subsets = (1, 1, 1, 2, 1, 2, 2), set_labels = ('Set1', 'Set2', 'Set3')) A more elaborate example:: from matplotlib import pyplot as plt import numpy as np from matplotlib_venn import venn3, venn3_circles plt.figure(figsize=(4,4)) v = venn3(subsets=(1, 1, 1, 1, 1, 1, 1), set_labels = ('A', 'B', 'C')) v.get_patch_by_id('100').set_alpha(1.0) v.get_patch_by_id('100').set_color('white') v.get_label_by_id('100').set_text('Unknown') v.get_label_by_id('A').set_text('Set "A"') c = venn3_circles(subsets=(1, 1, 1, 1, 1, 1, 1), linestyle='dashed') c[0].set_lw(1.0) c[0].set_ls('dotted') plt.title("Sample Venn diagram") plt.annotate('Unknown set', xy=v.get_label_by_id('100').get_position() - np.array([0, 0.05]), xytext=(-70,-70), ha='center', textcoords='offset points', bbox=dict(boxstyle='round,pad=0.5', fc='gray', alpha=0.1), arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0.5',color='gray')) plt.show() An example with multiple subplots:: from matplotlib_venn import venn2, venn2_circles figure, axes = plt.subplots(2, 2) venn2(subsets={'10': 1, '01': 1, '11': 1}, set_labels = ('A', 'B'), ax=axes[0][0]) venn2_circles((1, 2, 3), ax=axes[0][1]) venn3(subsets=(1, 1, 1, 1, 1, 1, 1), set_labels = ('A', 'B', 'C'), ax=axes[1][0]) venn3_circles({'001': 10, '100': 20, '010': 21, '110': 13, '011': 14}, ax=axes[1][1]) plt.show() Perhaps the most common use case is generating a Venn diagram given three sets of objects:: set1 = set(['A', 'B', 'C', 'D']) set2 = set(['B', 'C', 'D', 'E']) set3 = set(['C', 'D',' E', 'F', 'G']) venn3([set1, set2, set3], ('Set1', 'Set2', 'Set3')) plt.show() Tuning the diagram layout ------------------------- Note that for a three-circle venn diagram it is not in general possible to achieve exact correspondence between the required set sizes and region areas. The default layout algorithm aims to correctly represent: * Relative areas of the full individual sets (A, B, C). * Relative areas of pairwise intersections of sets (A&B, A&C, B&C, not to be confused with the regions A&B&~C, A&~B&C, ~A&B&C, on the diagram). Sometimes the result is unsatisfactory and either the area weighting or the layout logic needs to be tuned. The area weighing can be adjusted by providing a `fixed_subset_sizes` argument to the `DefaultLayoutAlgorithm`:: from matplotlib_venn.layout.venn2 import DefaultLayoutAlgorithm venn2((1,2,3), layout_algorithm=DefaultLayoutAlgorithm(fixed_subset_sizes=(1,1,1))) from matplotlib_venn.layout.venn3 import DefaultLayoutAlgorithm venn3((7,6,5,4,3,2,1), layout_algorithm=DefaultLayoutAlgorithm(fixed_subset_sizes=(1,1,1,1,1,1,1))) 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 invoked, yet the actual numbers will be `(1,2,3)` and `(7,6,5,4,3,2,1)` respectively. The diagram can be tuned further by switching the layout algorithm to a different implementation. At the moment the package offers an alternative layout algorithm for `venn3` diagrams that lays the circles out by optimizing a user-provided *cost function*. The following examples illustrate its usage:: from matplotlib_venn.layout.venn3 import cost_based subset_sizes = (100,200,10000,10,20,3,1) venn3(subset_sizes, layout_algorithm=cost_based.LayoutAlgorithm()) alg = cost_based.LayoutAlgorithm(cost_fn=cost_based.WeightedAggregateCost(transform_fn=lambda x: x)) venn3(subset_sizes, layout_algorithm=alg) alg = cost_based.LayoutAlgorithm(cost_fn=cost_based.WeightedAggregateCost(weights=(0,0,0,1,1,1,1))) venn3(subset_sizes, layout_algorithm=alg) The default "pairwise" algorithm is, theoretically, a special case of the cost-based method with the respective cost function:: alg = cost_based.LayoutAlgorithm(cost_fn=cost_based.pairwise_cost) venn3(subset_sizes, layout_algorithm=alg) (The latter plot will be close, but not perfectly equal to the outcome of `DefaultLayoutAlgorithm()`). Note that the import:: from matplotlib_venn.layout.venn3 import cost_based will fail unless you have the optional `shapely` package installed (see "Installation" above). Questions --------- * If you ask your questions at `StackOverflow `_ and tag them `matplotlib-venn `_, chances are high you could get an answer from the maintainer of this package. See also -------- * Report issues and submit fixes at Github: https://github.com/konstantint/matplotlib-venn Check out the ``DEVELOPER-README.rst`` for development-related notes. * Some alternative means of plotting a Venn diagram (as of October 2012) are reviewed in the blog post: http://fouryears.eu/2012/10/13/venn-diagrams-in-python/ * The `matplotlib-subsets `_ package visualizes a hierarchy of sets as a tree of rectangles. * The `matplotlib_set_diagrams `_ package is a GPL-licensed alternative that offers a different layout algorithm, which supports more than three sets and provides a cool ability to incorporate wordclouds into your Venn (Euler) diagrams. ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722463586.0 matplotlib-venn-1.1.1/matplotlib_venn.egg-info/SOURCES.txt0000644000175000017500000000141714652532542025165 0ustar00konstantintkonstantintCHANGELOG.txt LICENSE MANIFEST.in README.rst pyproject.toml setup.cfg setup.py matplotlib_venn/__init__.py matplotlib_venn/_arc.py matplotlib_venn/_common.py matplotlib_venn/_math.py matplotlib_venn/_region.py matplotlib_venn/_util.py matplotlib_venn/_venn2.py matplotlib_venn/_venn3.py matplotlib_venn.egg-info/PKG-INFO matplotlib_venn.egg-info/SOURCES.txt matplotlib_venn.egg-info/dependency_links.txt matplotlib_venn.egg-info/requires.txt matplotlib_venn.egg-info/top_level.txt matplotlib_venn.egg-info/zip-safe matplotlib_venn/layout/__init__.py matplotlib_venn/layout/api.py matplotlib_venn/layout/venn2/__init__.py matplotlib_venn/layout/venn2/exact.py matplotlib_venn/layout/venn3/__init__.py matplotlib_venn/layout/venn3/cost_based.py matplotlib_venn/layout/venn3/pairwise.py././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722463586.0 matplotlib-venn-1.1.1/matplotlib_venn.egg-info/dependency_links.txt0000644000175000017500000000000114652532542027344 0ustar00konstantintkonstantint ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722463586.0 matplotlib-venn-1.1.1/matplotlib_venn.egg-info/requires.txt0000644000175000017500000000005214652532542025673 0ustar00konstantintkonstantintmatplotlib numpy scipy [shapely] shapely ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722463586.0 matplotlib-venn-1.1.1/matplotlib_venn.egg-info/top_level.txt0000644000175000017500000000002014652532542026020 0ustar00konstantintkonstantintmatplotlib_venn ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722419575.0 matplotlib-venn-1.1.1/matplotlib_venn.egg-info/zip-safe0000644000175000017500000000000114652404567024733 0ustar00konstantintkonstantint ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722419543.0 matplotlib-venn-1.1.1/pyproject.toml0000644000175000017500000000012214652404527021317 0ustar00konstantintkonstantint[build-system] requires = ["setuptools"] build-backend = "setuptools.build_meta" ././@PaxHeader0000000000000000000000000000003400000000000010212 xustar0028 mtime=1722463586.3428361 matplotlib-venn-1.1.1/setup.cfg0000644000175000017500000000034314652532542020230 0ustar00konstantintkonstantint[egg_info] tag_build = tag_svn_revision = false tag_date = 0 [tool:pytest] addopts = --ignore=setup.py --ignore=build --ignore=dist --doctest-modules norecursedirs = *.egg doctest_optionflags = NORMALIZE_WHITESPACE ELLIPSIS ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1722440778.0 matplotlib-venn-1.1.1/setup.py0000644000175000017500000000350314652456112020120 0ustar00konstantintkonstantint""" Venn diagram plotting routines. Setup script. Note that "python setup.py test" invokes pytest on the package. This checks both xxx_test modules and docstrings. Copyright 2012-2024, Konstantin Tretyakov. http://kt.era.ee/ Licensed under MIT license. """ from setuptools import setup, find_namespace_packages from setuptools.command.test import test as TestCommand class PyTest(TestCommand): def run_tests(self): import sys import pytest # import here, cause outside the eggs aren't loaded sys.exit(pytest.main(self.test_args)) version = [ ln.split('"')[1] for ln in open("matplotlib_venn/__init__.py") if "__version__" in ln ][0] setup( name="matplotlib-venn", version=version, description="Functions for plotting area-proportional two- and three-way Venn diagrams in matplotlib.", long_description=open("README.rst").read(), classifiers=[ # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers "Development Status :: 5 - Production/Stable", "Intended Audience :: Science/Research", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3", "Topic :: Scientific/Engineering :: Visualization", ], platforms=["Platform Independent"], keywords="matplotlib plotting charts venn-diagrams", author="Konstantin Tretyakov", author_email="kt@umn.ee", url="https://github.com/konstantint/matplotlib-venn", license="MIT", packages=find_namespace_packages(include=["matplotlib_venn*"]), include_package_data=True, zip_safe=True, install_requires=["matplotlib", "numpy", "scipy"], extras_require={ "shapely": ["shapely"], }, tests_require=["pytest"], cmdclass={"test": PyTest}, entry_points="", )