• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

domdfcoding / domplotlib / 15188046115

22 May 2025 01:35PM UTC coverage: 92.683%. Remained the same
15188046115

push

github

domdfcoding
Drop support for Python 3.6

76 of 82 relevant lines covered (92.68%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.83
/domplotlib/plots.py
1
#!/usr/bin/env python3
2
#
3
#  plots.py
4
"""
5
Custom plotting functions.
6

7
.. versionadded:: 0.2.0
8
"""
9
#
10
#  Copyright © 2021 Dominic Davis-Foster <dominic@davis-foster.co.uk>
11
#
12
#  Permission is hereby granted, free of charge, to any person obtaining a copy
13
#  of this software and associated documentation files (the "Software"), to deal
14
#  in the Software without restriction, including without limitation the rights
15
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
#  copies of the Software, and to permit persons to whom the Software is
17
#  furnished to do so, subject to the following conditions:
18
#
19
#  The above copyright notice and this permission notice shall be included in all
20
#  copies or substantial portions of the Software.
21
#
22
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23
#  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24
#  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25
#  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
26
#  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
27
#  OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
28
#  OR OTHER DEALINGS IN THE SOFTWARE.
29
#
30

31
# stdlib
32
from typing import Collection, List, Optional, Tuple, overload
1✔
33

34
# 3rd party
35
from cawdrey.tally import SupportsMostCommon, Tally
1✔
36
from matplotlib.patches import Wedge  # type: ignore[import]
1✔
37
from matplotlib.text import Text  # type: ignore[import]
1✔
38

39
__all__ = ["pie_from_tally"]
1✔
40

41

42
@overload
1✔
43
def pie_from_tally(
1✔
44
                tally: "Tally[str]",
45
                explode: Collection[str] = (),
46
                *,
47
                percent: bool = ...,
48
                reverse: bool = ...,
49
                autopct: None = ...,
50
                **kwargs,
51
                ) -> Tuple[List[Wedge], List[Text]]: ...
52

53

54
@overload
1✔
55
def pie_from_tally(
1✔
56
                tally: Tally[str],
57
                explode: Collection[str] = (),
58
                *,
59
                percent: bool = ...,
60
                reverse: bool = ...,
61
                autopct: str,
62
                **kwargs,
63
                ) -> Tuple[List[Wedge], List[Text], List[Text]]: ...
64

65

66
def pie_from_tally(
1✔
67
                tally: Tally[str],
68
                explode: Collection[str] = (),
69
                *,
70
                percent: bool = False,
71
                reverse: bool = False,
72
                autopct: Optional[str] = None,
73
                **kwargs,
74
                ) -> Tuple[List, ...]:
75
        r"""
76
        Construct a pie chart from :class:`cawdrey.tally.Tally`.
77

78
        :param tally:
79
        :param explode: A list of key names to explode the segments for.
80
        :param percent: If :py:obj:`True`, shows the percentage of each element out of the sum of all elements.
81
        :param reverse: Order the wedges clockwise rather than anticlockwise..
82
        :param \*\*kwargs: Other keyword arguments taken by :meth:`matplotlib.axes.Axes.pie`.
83

84
        :return:
85

86
                * patches (:class:`list`\) -- A sequence of `matplotlib.patches.Wedge` instances
87
                * texts (:class:`list`\) -- A list of the label `.Text` instances.
88
                * autotexts (:class:`list`\) -- A list of `.Text` instances for the numeric labels. This will only
89
                  be returned if the parameter *autopct* is not *None*.
90
        """
91

92
        if "ax" in kwargs:
1✔
93
                ax = kwargs.pop("ax")
1✔
94
        else:  # pragma: no cover
95

96
                # 3rd party
97
                from matplotlib import pyplot  # type: ignore[import]
98
                ax = pyplot.gca()
99

100
        kwargs.pop("labels", None)
1✔
101
        kwargs["autopct"] = autopct
1✔
102

103
        data: SupportsMostCommon[str]
104
        if percent:
1✔
105
                data = tally.as_percentage()
×
106
        else:
107
                data = tally
1✔
108

109
        if reverse:
1✔
110
                labels, sizes = list(zip(*reversed(data.most_common())))
1✔
111
        else:
112
                labels, sizes = list(zip(*data.most_common()))
1✔
113

114
        if explode:
1✔
115
                kwargs["explode"] = tuple(0.1 if label in explode else 0.0 for label in labels)
1✔
116
        else:
117
                kwargs.pop("explode", None)
1✔
118

119
        return ax.pie(sizes, labels=labels, **kwargs)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc