Skip to content

anydyce.viz package reference

Experimental

This package is an attempt to explore conveniences for integration with Matplotlib. It is an explicit departure from RFC 1925, ยง 2.2 and should be considered experimental. Be warned that future release may introduce incompatibilities or remove this package altogether. Feedback, suggestions, and contributions are welcome and appreciated.

HLikeT = Union[H, HableT] module-attribute

ImageType

Bases: str, Enum

Source code in anydyce/viz.py
73
74
75
class ImageType(str, Enum):
    PNG = "PNG"
    SVG = "SVG"

PNG = 'PNG' class-attribute instance-attribute

SVG = 'SVG' class-attribute instance-attribute

TraditionalPlotType

Bases: str, Enum

Source code in anydyce/viz.py
78
79
80
81
class TraditionalPlotType(str, Enum):
    NORMAL = "Normal"
    AT_MOST = "At Most"
    AT_LEAST = "At Least"

AT_LEAST = 'At Least' class-attribute instance-attribute

AT_MOST = 'At Most' class-attribute instance-attribute

NORMAL = 'Normal' class-attribute instance-attribute

HPlotter

Experimental

This class should be considered experimental and may change or disappear in future versions.

A plotter responsible for laying out control widgets and visualizing data provided by primary and optional secondary histograms. (See the plot method.)

Source code in anydyce/viz.py
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
class HPlotter:
    r"""
    !!! warning "Experimental"

        This class should be considered experimental and may change or disappear in
        future versions.

    A plotter responsible for laying out control widgets and visualizing data provided
    by primary and optional secondary histograms. (See the
    [*plot* method][anydyce.viz.HPlotter.plot].)
    """

    @abstractproperty
    def NAME(self) -> str:
        r"""
        The display name of the plotter.
        """
        raise NotImplementedError

    @beartype
    def layout(self, plot_widgets: PlotWidgets) -> widgets.Widget:
        r"""
        Takes a set of widgets (*plot_widgets*) and returns a container (layout) widget
        selecting those needed by the plotter.
        """
        return widgets.VBox(
            [
                plot_widgets.enable_cutoff,
                plot_widgets.cutoff,
                plot_widgets.img_type,
                plot_widgets.resolution,
            ]
        )

    @abstractmethod
    def plot(
        self,
        hs: Sequence[Tuple[str, H, Optional[H]]],
        settings: SettingsDict,
    ):
        r"""
        Creates and displays a visualization of the provided histograms. *fig* is the
        [``#!python
        matplotlib.figure.Figure``](https://matplotlib.org/stable/api/figure_api.html#matplotlib.figure.Figure)
        in which the visualization should be constructed. *hs* is a sequence of
        three-tuples, a name, a primary histogram, and an optional secondary histogram
        (``#!python None`` if omitted). Plotters should implement this function to
        display at least the primary histogram and visually associate it with the name.
        """
        raise NotImplementedError

    @beartype
    def transparent(self, requested: bool) -> bool:
        r"""
        Returns whether this plotter produces plots which support transparency if
        *requested*. The default implementation always returns ``#!python False``.
        """
        return False

NAME() -> str

The display name of the plotter.

Source code in anydyce/viz.py
649
650
651
652
653
654
@abstractproperty
def NAME(self) -> str:
    r"""
    The display name of the plotter.
    """
    raise NotImplementedError

layout(plot_widgets: PlotWidgets) -> widgets.Widget

Takes a set of widgets (plot_widgets) and returns a container (layout) widget selecting those needed by the plotter.

Source code in anydyce/viz.py
656
657
658
659
660
661
662
663
664
665
666
667
668
669
@beartype
def layout(self, plot_widgets: PlotWidgets) -> widgets.Widget:
    r"""
    Takes a set of widgets (*plot_widgets*) and returns a container (layout) widget
    selecting those needed by the plotter.
    """
    return widgets.VBox(
        [
            plot_widgets.enable_cutoff,
            plot_widgets.cutoff,
            plot_widgets.img_type,
            plot_widgets.resolution,
        ]
    )

plot(hs: Sequence[Tuple[str, H, Optional[H]]], settings: SettingsDict) abstractmethod

Creates and displays a visualization of the provided histograms. fig is the matplotlib.figure.Figure in which the visualization should be constructed. hs is a sequence of three-tuples, a name, a primary histogram, and an optional secondary histogram (None if omitted). Plotters should implement this function to display at least the primary histogram and visually associate it with the name.

Source code in anydyce/viz.py
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
@abstractmethod
def plot(
    self,
    hs: Sequence[Tuple[str, H, Optional[H]]],
    settings: SettingsDict,
):
    r"""
    Creates and displays a visualization of the provided histograms. *fig* is the
    [``#!python
    matplotlib.figure.Figure``](https://matplotlib.org/stable/api/figure_api.html#matplotlib.figure.Figure)
    in which the visualization should be constructed. *hs* is a sequence of
    three-tuples, a name, a primary histogram, and an optional secondary histogram
    (``#!python None`` if omitted). Plotters should implement this function to
    display at least the primary histogram and visually associate it with the name.
    """
    raise NotImplementedError

transparent(requested: bool) -> bool

Returns whether this plotter produces plots which support transparency if requested. The default implementation always returns False.

Source code in anydyce/viz.py
688
689
690
691
692
693
694
@beartype
def transparent(self, requested: bool) -> bool:
    r"""
    Returns whether this plotter produces plots which support transparency if
    *requested*. The default implementation always returns ``#!python False``.
    """
    return False

BarHPlotter

Bases: HPlotter

Experimental

This class should be considered experimental and may change or disappear in future versions.

A plotter for creating a single vertical bar plot visualizing all primary histograms. Secondary histograms are ignored.

Source code in anydyce/viz.py
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
class BarHPlotter(HPlotter):
    r"""
    !!! warning "Experimental"

        This class should be considered experimental and may change or disappear in
        future versions.

    A plotter for creating a single vertical bar plot visualizing all primary
    histograms. Secondary histograms are ignored.
    """

    NAME = "Bar Plot"

    @beartype
    def layout(self, plot_widgets: PlotWidgets) -> widgets.Widget:
        cutoff_layout_widget = super().layout(plot_widgets)

        return widgets.VBox(
            [
                widgets.HBox(
                    [
                        cutoff_layout_widget,
                        plot_widgets.graph_type,
                        widgets.VBox(
                            [
                                plot_widgets.alpha,
                                plot_widgets.plot_style,
                                plot_widgets.show_shadow,
                            ]
                        ),
                    ]
                ),
            ]
        )

    @beartype
    def plot(
        self,
        hs: Sequence[Tuple[str, H, Optional[H]]],
        settings: SettingsDict,
    ) -> None:
        _, ax = matplotlib.pyplot.subplots(
            figsize=(
                settings["resolution"],
                settings["resolution"] / 16 * 9,
            )
        )

        plot_bar(
            ax,
            tuple((label, h) for label, h, _ in hs),
            alpha=settings["alpha"],
            graph_type=settings["graph_type"],
            shadow=settings["show_shadow"],
        )

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            ax.legend()

NAME = 'Bar Plot' class-attribute instance-attribute

layout(plot_widgets: PlotWidgets) -> widgets.Widget

Source code in anydyce/viz.py
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
@beartype
def layout(self, plot_widgets: PlotWidgets) -> widgets.Widget:
    cutoff_layout_widget = super().layout(plot_widgets)

    return widgets.VBox(
        [
            widgets.HBox(
                [
                    cutoff_layout_widget,
                    plot_widgets.graph_type,
                    widgets.VBox(
                        [
                            plot_widgets.alpha,
                            plot_widgets.plot_style,
                            plot_widgets.show_shadow,
                        ]
                    ),
                ]
            ),
        ]
    )

plot(hs: Sequence[Tuple[str, H, Optional[H]]], settings: SettingsDict) -> None

Source code in anydyce/viz.py
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
@beartype
def plot(
    self,
    hs: Sequence[Tuple[str, H, Optional[H]]],
    settings: SettingsDict,
) -> None:
    _, ax = matplotlib.pyplot.subplots(
        figsize=(
            settings["resolution"],
            settings["resolution"] / 16 * 9,
        )
    )

    plot_bar(
        ax,
        tuple((label, h) for label, h, _ in hs),
        alpha=settings["alpha"],
        graph_type=settings["graph_type"],
        shadow=settings["show_shadow"],
    )

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        ax.legend()

BurstHPlotter

Bases: HPlotter

Experimental

This class should be considered experimental and may change or disappear in future versions.

A plotter for creating one burst plot per primary histogram. If provided, associated secondary histograms are used for the outer rings.

Source code in anydyce/viz.py
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
class BurstHPlotter(HPlotter):
    r"""
    !!! warning "Experimental"

        This class should be considered experimental and may change or disappear in
        future versions.

    A plotter for creating one burst plot per primary histogram. If provided, associated
    secondary histograms are used for the outer rings.
    """

    NAME = "Burst Plots"

    @beartype
    def layout(self, plot_widgets: PlotWidgets) -> widgets.Widget:
        cutoff_layout_widget = super().layout(plot_widgets)

        return widgets.VBox(
            [
                widgets.HBox(
                    [
                        widgets.VBox(
                            [
                                cutoff_layout_widget,
                            ]
                        ),
                        widgets.VBox(
                            [
                                plot_widgets.burst_swap,
                                plot_widgets.burst_zero_fill_normalize,
                                plot_widgets.burst_cmap_inner,
                                plot_widgets.burst_cmap_outer,
                                plot_widgets.burst_cmap_link,
                            ]
                        ),
                        widgets.VBox(
                            [
                                plot_widgets.alpha,
                                plot_widgets.burst_color_text,
                                plot_widgets.burst_color_bg,
                                plot_widgets.burst_color_bg_trnsp,
                                plot_widgets.burst_columns,
                            ]
                        ),
                    ]
                ),
            ]
        )

    @beartype
    def plot(
        self,
        hs: Sequence[Tuple[str, H, Optional[H]]],
        settings: SettingsDict,
    ) -> None:
        cols = settings["burst_columns"]
        assert cols > 0
        logical_rows = len(hs) // cols + (len(hs) % cols != 0)
        # Height of row gaps in relation to height of figs
        gap_size_ratio = Fraction(1, 5)
        total_gaps = max(0, logical_rows - 1)
        figsize = (
            settings["resolution"],
            float(
                settings["resolution"]
                * (logical_rows + total_gaps * gap_size_ratio)
                / cols
            ),
        )
        matplotlib.pyplot.figure(facecolor=settings["burst_color_bg"], figsize=figsize)
        actual_rows_per_fig = gap_size_ratio.denominator
        actual_rows_per_gap = gap_size_ratio.numerator
        total_actual_rows = (
            logical_rows * actual_rows_per_fig + total_gaps * actual_rows_per_gap
        )

        def _zero_fill_normalize():
            unique_outcomes: Set[RealLike] = set()

            for i, (_, first_h, second_h) in enumerate(hs):
                unique_outcomes.update(first_h)

                if second_h:
                    unique_outcomes.update(second_h)

            for i, (label, first_h, second_h) in enumerate(hs):
                yield (
                    label,
                    first_h.zero_fill(unique_outcomes),
                    None if second_h is None else second_h.zero_fill(unique_outcomes),
                )

        if settings["burst_zero_fill_normalize"]:
            hs = tuple(_zero_fill_normalize())

        for i, (label, h_inner, h_outer) in enumerate(hs):
            plot_burst_kw: Dict[str, Any] = dict(
                title=label,
                inner_cmap=settings["burst_cmap_inner"],
                outer_cmap=settings["burst_cmap_outer"]
                if not settings["burst_cmap_link"]
                else settings["burst_cmap_inner"],
                text_color=settings["burst_color_text"],
                alpha=settings["alpha"],
            )

            if h_outer is not None:
                if settings["burst_swap"]:
                    h_inner, h_outer = h_outer, h_inner

            logical_row = i // cols
            actual_row_start = logical_row * (actual_rows_per_gap + actual_rows_per_fig)
            ax = matplotlib.pyplot.subplot2grid(
                (total_actual_rows, cols),
                (actual_row_start, i % cols),
                rowspan=actual_rows_per_fig,
            )
            plot_burst(
                ax,
                h_inner,
                h_outer,
                **plot_burst_kw,
            )

    @beartype
    def transparent(self, requested: bool) -> bool:
        return requested

NAME = 'Burst Plots' class-attribute instance-attribute

layout(plot_widgets: PlotWidgets) -> widgets.Widget

Source code in anydyce/viz.py
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
@beartype
def layout(self, plot_widgets: PlotWidgets) -> widgets.Widget:
    cutoff_layout_widget = super().layout(plot_widgets)

    return widgets.VBox(
        [
            widgets.HBox(
                [
                    widgets.VBox(
                        [
                            cutoff_layout_widget,
                        ]
                    ),
                    widgets.VBox(
                        [
                            plot_widgets.burst_swap,
                            plot_widgets.burst_zero_fill_normalize,
                            plot_widgets.burst_cmap_inner,
                            plot_widgets.burst_cmap_outer,
                            plot_widgets.burst_cmap_link,
                        ]
                    ),
                    widgets.VBox(
                        [
                            plot_widgets.alpha,
                            plot_widgets.burst_color_text,
                            plot_widgets.burst_color_bg,
                            plot_widgets.burst_color_bg_trnsp,
                            plot_widgets.burst_columns,
                        ]
                    ),
                ]
            ),
        ]
    )

plot(hs: Sequence[Tuple[str, H, Optional[H]]], settings: SettingsDict) -> None

Source code in anydyce/viz.py
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
@beartype
def plot(
    self,
    hs: Sequence[Tuple[str, H, Optional[H]]],
    settings: SettingsDict,
) -> None:
    cols = settings["burst_columns"]
    assert cols > 0
    logical_rows = len(hs) // cols + (len(hs) % cols != 0)
    # Height of row gaps in relation to height of figs
    gap_size_ratio = Fraction(1, 5)
    total_gaps = max(0, logical_rows - 1)
    figsize = (
        settings["resolution"],
        float(
            settings["resolution"]
            * (logical_rows + total_gaps * gap_size_ratio)
            / cols
        ),
    )
    matplotlib.pyplot.figure(facecolor=settings["burst_color_bg"], figsize=figsize)
    actual_rows_per_fig = gap_size_ratio.denominator
    actual_rows_per_gap = gap_size_ratio.numerator
    total_actual_rows = (
        logical_rows * actual_rows_per_fig + total_gaps * actual_rows_per_gap
    )

    def _zero_fill_normalize():
        unique_outcomes: Set[RealLike] = set()

        for i, (_, first_h, second_h) in enumerate(hs):
            unique_outcomes.update(first_h)

            if second_h:
                unique_outcomes.update(second_h)

        for i, (label, first_h, second_h) in enumerate(hs):
            yield (
                label,
                first_h.zero_fill(unique_outcomes),
                None if second_h is None else second_h.zero_fill(unique_outcomes),
            )

    if settings["burst_zero_fill_normalize"]:
        hs = tuple(_zero_fill_normalize())

    for i, (label, h_inner, h_outer) in enumerate(hs):
        plot_burst_kw: Dict[str, Any] = dict(
            title=label,
            inner_cmap=settings["burst_cmap_inner"],
            outer_cmap=settings["burst_cmap_outer"]
            if not settings["burst_cmap_link"]
            else settings["burst_cmap_inner"],
            text_color=settings["burst_color_text"],
            alpha=settings["alpha"],
        )

        if h_outer is not None:
            if settings["burst_swap"]:
                h_inner, h_outer = h_outer, h_inner

        logical_row = i // cols
        actual_row_start = logical_row * (actual_rows_per_gap + actual_rows_per_fig)
        ax = matplotlib.pyplot.subplot2grid(
            (total_actual_rows, cols),
            (actual_row_start, i % cols),
            rowspan=actual_rows_per_fig,
        )
        plot_burst(
            ax,
            h_inner,
            h_outer,
            **plot_burst_kw,
        )

transparent(requested: bool) -> bool

Source code in anydyce/viz.py
882
883
884
@beartype
def transparent(self, requested: bool) -> bool:
    return requested

HorizontalBarHPlotter

Bases: BarHPlotter

Experimental

This class should be considered experimental and may change or disappear in future versions.

A plotter for creating one horizontal bar plot per primary histogram. Secondary histograms are ignored.

Source code in anydyce/viz.py
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
class HorizontalBarHPlotter(BarHPlotter):
    r"""
    !!! warning "Experimental"

        This class should be considered experimental and may change or disappear in
        future versions.

    A plotter for creating one horizontal bar plot per primary histogram. Secondary
    histograms are ignored.
    """

    NAME = "Horizontal Bar Plots"

    @beartype
    def plot(
        self,
        hs: Sequence[Tuple[str, H, Optional[H]]],
        settings: SettingsDict,
    ) -> None:
        total_outcomes = sum(
            1 for _ in chain.from_iterable(h.outcomes() for _, h, _ in hs)
        )
        total_height = total_outcomes + 1  # one extra to accommodate the axis
        inches_per_height_unit = settings["resolution"] / 64
        figsize = (
            settings["resolution"],
            total_height * inches_per_height_unit,
        )
        matplotlib.pyplot.figure(figsize=figsize)
        barh_kw: Dict[str, Any] = dict(alpha=settings["alpha"])

        if settings["show_shadow"]:
            barh_kw.update(
                dict(
                    path_effects=[
                        matplotlib.patheffects.withSimplePatchShadow(),
                        matplotlib.patheffects.Normal(),
                    ]
                )
            )

        plot_style = settings["plot_style"]

        if (
            plot_style in matplotlib.style.library
            and "axes.prop_cycle" in matplotlib.style.library[plot_style]
            and "color" in matplotlib.style.library[plot_style]["axes.prop_cycle"]
        ):
            # Our current style has a cycler with colors, so use it
            cycler = matplotlib.style.library[plot_style]["axes.prop_cycle"]
        else:
            # Revert to the global default
            cycler = matplotlib.rcParams["axes.prop_cycle"]

        color_iter = cycle(cycler.by_key().get("color", (None,)))
        row_start = 0
        first_ax = ax = None

        for i, (label, h, _) in enumerate(hs):
            if not h:
                continue

            outcomes, values = values_xy_for_graph_type(h, settings["graph_type"])
            rowspan = len(outcomes)

            if first_ax is None:
                first_ax = ax = matplotlib.pyplot.subplot2grid(
                    (total_height, 1), (row_start, 0), rowspan=rowspan
                )
            else:
                ax = matplotlib.pyplot.subplot2grid(
                    (total_height, 1), (row_start, 0), rowspan=rowspan, sharex=first_ax
                )

            ax.set_yticks(outcomes)
            ax.tick_params(labelbottom=False)
            ax.set_ylim((max(outcomes) + 0.5, min(outcomes) - 0.5))
            ax.barh(outcomes, values, color=next(color_iter), label=label, **barh_kw)
            ax.legend(loc="upper right")
            row_start += rowspan

        if ax is not None:
            ax.tick_params(labelbottom=True)
            ax.xaxis.set_major_formatter(matplotlib.ticker.PercentFormatter(xmax=1))

NAME = 'Horizontal Bar Plots' class-attribute instance-attribute

plot(hs: Sequence[Tuple[str, H, Optional[H]]], settings: SettingsDict) -> None

Source code in anydyce/viz.py
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
@beartype
def plot(
    self,
    hs: Sequence[Tuple[str, H, Optional[H]]],
    settings: SettingsDict,
) -> None:
    total_outcomes = sum(
        1 for _ in chain.from_iterable(h.outcomes() for _, h, _ in hs)
    )
    total_height = total_outcomes + 1  # one extra to accommodate the axis
    inches_per_height_unit = settings["resolution"] / 64
    figsize = (
        settings["resolution"],
        total_height * inches_per_height_unit,
    )
    matplotlib.pyplot.figure(figsize=figsize)
    barh_kw: Dict[str, Any] = dict(alpha=settings["alpha"])

    if settings["show_shadow"]:
        barh_kw.update(
            dict(
                path_effects=[
                    matplotlib.patheffects.withSimplePatchShadow(),
                    matplotlib.patheffects.Normal(),
                ]
            )
        )

    plot_style = settings["plot_style"]

    if (
        plot_style in matplotlib.style.library
        and "axes.prop_cycle" in matplotlib.style.library[plot_style]
        and "color" in matplotlib.style.library[plot_style]["axes.prop_cycle"]
    ):
        # Our current style has a cycler with colors, so use it
        cycler = matplotlib.style.library[plot_style]["axes.prop_cycle"]
    else:
        # Revert to the global default
        cycler = matplotlib.rcParams["axes.prop_cycle"]

    color_iter = cycle(cycler.by_key().get("color", (None,)))
    row_start = 0
    first_ax = ax = None

    for i, (label, h, _) in enumerate(hs):
        if not h:
            continue

        outcomes, values = values_xy_for_graph_type(h, settings["graph_type"])
        rowspan = len(outcomes)

        if first_ax is None:
            first_ax = ax = matplotlib.pyplot.subplot2grid(
                (total_height, 1), (row_start, 0), rowspan=rowspan
            )
        else:
            ax = matplotlib.pyplot.subplot2grid(
                (total_height, 1), (row_start, 0), rowspan=rowspan, sharex=first_ax
            )

        ax.set_yticks(outcomes)
        ax.tick_params(labelbottom=False)
        ax.set_ylim((max(outcomes) + 0.5, min(outcomes) - 0.5))
        ax.barh(outcomes, values, color=next(color_iter), label=label, **barh_kw)
        ax.legend(loc="upper right")
        row_start += rowspan

    if ax is not None:
        ax.tick_params(labelbottom=True)
        ax.xaxis.set_major_formatter(matplotlib.ticker.PercentFormatter(xmax=1))

LineHPlotter

Bases: HPlotter

Experimental

This class should be considered experimental and may change or disappear in future versions.

A plotter for creating a single line plot visualizing all primary histograms. Secondary histograms are ignored.

Source code in anydyce/viz.py
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
class LineHPlotter(HPlotter):
    r"""
    !!! warning "Experimental"

        This class should be considered experimental and may change or disappear in
        future versions.

    A plotter for creating a single line plot visualizing all primary histograms.
    Secondary histograms are ignored.
    """

    NAME = "Line Plot"

    @beartype
    def layout(self, plot_widgets: PlotWidgets) -> widgets.Widget:
        cutoff_layout_widget = super().layout(plot_widgets)

        return widgets.VBox(
            [
                widgets.HBox(
                    [
                        cutoff_layout_widget,
                        plot_widgets.graph_type,
                        widgets.VBox(
                            [
                                plot_widgets.alpha,
                                plot_widgets.plot_style,
                                plot_widgets.show_shadow,
                                plot_widgets.markers,
                            ]
                        ),
                    ]
                ),
            ]
        )

    @beartype
    def plot(
        self,
        hs: Sequence[Tuple[str, H, Optional[H]]],
        settings: SettingsDict,
    ) -> None:
        _, ax = matplotlib.pyplot.subplots(
            figsize=(
                settings["resolution"],
                settings["resolution"] / 16 * 9,
            )
        )

        plot_line(
            ax,
            tuple((label, h) for label, h, _ in hs),
            alpha=settings["alpha"],
            graph_type=settings["graph_type"],
            markers=settings["markers"],
            shadow=settings["show_shadow"],
        )

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            ax.legend()

NAME = 'Line Plot' class-attribute instance-attribute

layout(plot_widgets: PlotWidgets) -> widgets.Widget

Source code in anydyce/viz.py
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
@beartype
def layout(self, plot_widgets: PlotWidgets) -> widgets.Widget:
    cutoff_layout_widget = super().layout(plot_widgets)

    return widgets.VBox(
        [
            widgets.HBox(
                [
                    cutoff_layout_widget,
                    plot_widgets.graph_type,
                    widgets.VBox(
                        [
                            plot_widgets.alpha,
                            plot_widgets.plot_style,
                            plot_widgets.show_shadow,
                            plot_widgets.markers,
                        ]
                    ),
                ]
            ),
        ]
    )

plot(hs: Sequence[Tuple[str, H, Optional[H]]], settings: SettingsDict) -> None

Source code in anydyce/viz.py
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
@beartype
def plot(
    self,
    hs: Sequence[Tuple[str, H, Optional[H]]],
    settings: SettingsDict,
) -> None:
    _, ax = matplotlib.pyplot.subplots(
        figsize=(
            settings["resolution"],
            settings["resolution"] / 16 * 9,
        )
    )

    plot_line(
        ax,
        tuple((label, h) for label, h, _ in hs),
        alpha=settings["alpha"],
        graph_type=settings["graph_type"],
        markers=settings["markers"],
        shadow=settings["show_shadow"],
    )

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        ax.legend()

ScatterHPlotter

Bases: LineHPlotter

Experimental

This class should be considered experimental and may change or disappear in future versions.

A plotter for creating a single scatter plot visualizing all primary histograms. Secondary histograms are ignored.

Source code in anydyce/viz.py
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
class ScatterHPlotter(LineHPlotter):
    r"""
    !!! warning "Experimental"

        This class should be considered experimental and may change or disappear in
        future versions.

    A plotter for creating a single scatter plot visualizing all primary histograms.
    Secondary histograms are ignored.
    """

    NAME = "Scatter Plot"

    @beartype
    def plot(
        self,
        hs: Sequence[Tuple[str, H, Optional[H]]],
        settings: SettingsDict,
    ) -> None:
        _, ax = matplotlib.pyplot.subplots(
            figsize=(
                settings["resolution"],
                settings["resolution"] / 16 * 9,
            )
        )

        plot_scatter(
            ax,
            tuple((label, h) for label, h, _ in hs),
            alpha=settings["alpha"],
            graph_type=settings["graph_type"],
            markers=settings["markers"],
            shadow=settings["show_shadow"],
        )

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            ax.legend()

NAME = 'Scatter Plot' class-attribute instance-attribute

plot(hs: Sequence[Tuple[str, H, Optional[H]]], settings: SettingsDict) -> None

Source code in anydyce/viz.py
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
@beartype
def plot(
    self,
    hs: Sequence[Tuple[str, H, Optional[H]]],
    settings: SettingsDict,
) -> None:
    _, ax = matplotlib.pyplot.subplots(
        figsize=(
            settings["resolution"],
            settings["resolution"] / 16 * 9,
        )
    )

    plot_scatter(
        ax,
        tuple((label, h) for label, h, _ in hs),
        alpha=settings["alpha"],
        graph_type=settings["graph_type"],
        markers=settings["markers"],
        shadow=settings["show_shadow"],
    )

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        ax.legend()

PlotWidgets

Bases: _PlotWidgetsDataclass

Experimental

This class should be considered experimental and may change or disappear in future versions.

Class to encapsulate interactive plot control widgets. All parameters for the initializer are optional.

  • initial_alpha is the starting alpha value for graphs (defaults to 0.75).

  • initial_burst_cmap_inner is the initially selected color map for inner burst graphs (defaults to "RdYlGn_r").

  • initial_burst_cmap_link is the starting value for linking the color maps for inner and outer burst graphs (defaults to True).

  • initial_burst_cmap_outer is the initially selected color map for outer burst graphs (defaults to "RdYlBu_r").

  • initial_burst_color_bg is the initially selected background color for burst graphs (defaults to "white").

  • initial_burst_color_bg_trnsp is the initially selected background transparency color burst graphs (defaults to False).

  • initial_burst_color_text is the initially selected text color for burst graphs (defaults to "black").

  • initial_burst_columns is the initially selected number of columns for displaying burst graphs (defaults to 3).

  • initial_burst_swap is whether the inner and outer burst graphs should be swapped at first (defaults to False).

  • initial_burst_zero_fill_normalize is whether all burst graphs should share a scale at first (i.e., so similar values share similar colors across burst graphs) (defaults to False).

  • initial_enable_cutoff is whether small values should be omitted from graphs at first (defaults to True).

  • initial_graph_type is the type of graph first shown (defaults to TraditionalPlotType.NORMAL).

  • initial_img_type is the initially selected image type (defaults to ImageType.PNG).

  • initial_markers are the starting set of markers for line and scatter plots (defaults to "oX^v><dP").

  • initial_plot_style is the starting color style for non-burst graphs (defaults to "bmh").

  • initial_show_shadow is whether shadows should be shown for non-burst graphs at first (defaults to False).

  • initial_resolution is the starting value for the graph resolution (defaults to 12).

Source code in anydyce/viz.py
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
class PlotWidgets(_PlotWidgetsDataclass):
    r"""
    !!! warning "Experimental"

        This class should be considered experimental and may change or disappear in
        future versions.

    Class to encapsulate interactive plot control widgets. All parameters for the
    [initializer][anydyce.viz.PlotWidgets.__init__] are optional.

    - *initial_alpha* is the starting alpha value for graphs (defaults to ``#!python
       0.75``).

    - *initial_burst_cmap_inner* is the initially selected color map for inner burst
       graphs (defaults to ``#!python "RdYlGn_r"``).

    - *initial_burst_cmap_link* is the starting value for linking the color maps for
       inner and outer burst graphs (defaults to ``#!python True``).

    - *initial_burst_cmap_outer* is the initially selected color map for outer burst
       graphs (defaults to ``#!python "RdYlBu_r"``).

    - *initial_burst_color_bg* is the initially selected background color for burst
       graphs (defaults to ``#!python "white"``).

    - *initial_burst_color_bg_trnsp* is the initially selected background transparency
       color burst graphs (defaults to ``#!python False``).

    - *initial_burst_color_text* is the initially selected text color for burst graphs
       (defaults to ``#!python "black"``).

    - *initial_burst_columns* is the initially selected number of columns for displaying
       burst graphs (defaults to ``#!python 3``).

    - *initial_burst_swap* is whether the inner and outer burst graphs should be swapped
       at first (defaults to ``#!python False``).

    - *initial_burst_zero_fill_normalize* is whether all burst graphs should share a
       scale at first (i.e., so similar values share similar colors across burst graphs)
       (defaults to ``#!python False``).

    - *initial_enable_cutoff* is whether small values should be omitted from graphs at
       first (defaults to ``#!python True``).

    - *initial_graph_type* is the type of graph first shown (defaults to
       [``TraditionalPlotType.NORMAL``][anydyce.viz.TraditionalPlotType.NORMAL]).

    - *initial_img_type* is the initially selected image type (defaults to
       [``ImageType.PNG``][anydyce.viz.ImageType.PNG]).

    - *initial_markers* are the starting set of markers for line and scatter plots
       (defaults to ``#!python "oX^v><dP"``).

    - *initial_plot_style* is the starting color style for non-burst graphs (defaults to
       ``#!python "bmh"``).

    - *initial_show_shadow* is whether shadows should be shown for non-burst graphs at
       first (defaults to ``#!python False``).

    - *initial_resolution* is the starting value for the graph resolution (defaults to
      ``#!python 12``).
    """

    @beartype
    def __init__(
        self,
        *,
        initial_alpha: float = DEFAULT_ALPHA,
        initial_burst_cmap_inner: str = DEFAULT_CMAP_BURST_INNER,
        initial_burst_cmap_link: bool = True,
        initial_burst_cmap_outer: str = DEFAULT_CMAP_BURST_OUTER,
        initial_burst_color_bg: str = DEFAULT_COLOR_BG,
        initial_burst_color_bg_trnsp: bool = False,
        initial_burst_color_text: str = DEFAULT_COLOR_TEXT,
        initial_burst_columns: int = DEFAULT_COLS_BURST,
        initial_burst_swap: bool = False,
        initial_burst_zero_fill_normalize: bool = False,
        initial_enable_cutoff: bool = True,
        initial_graph_type: TraditionalPlotType = TraditionalPlotType.NORMAL,
        initial_img_type: ImageType = ImageType.PNG,
        initial_markers: str = DEFAULT_MARKERS,
        initial_plot_style: str = DEFAULT_PLOT_STYLE,
        initial_resolution: int = DEFAULT_RESOLUTION,
        initial_show_shadow: bool = False,
    ):
        super().__init__()

        if initial_plot_style not in matplotlib.style.available:
            warnings.warn(
                f"unrecognized plot style {initial_plot_style!r}; reverting to 'default'",
                category=RuntimeWarning,
            )
            initial_plot_style = "default"

        self.alpha.value = initial_alpha
        self.burst_cmap_inner.value = initial_burst_cmap_inner
        self.burst_cmap_link.value = initial_burst_cmap_link
        self.burst_cmap_outer.disabled = initial_burst_cmap_link
        self.burst_cmap_outer.value = initial_burst_cmap_outer
        self.burst_color_bg.value = initial_burst_color_bg
        self.burst_color_bg_trnsp.value = initial_burst_color_bg_trnsp
        self.burst_color_text.value = initial_burst_color_text
        self.burst_columns.value = initial_burst_columns
        self.burst_swap.value = initial_burst_swap
        self.burst_zero_fill_normalize.value = initial_burst_zero_fill_normalize
        self.cutoff.disabled = not initial_enable_cutoff
        self.enable_cutoff.value = initial_enable_cutoff
        self.graph_type.value = initial_graph_type
        self.img_type.value = initial_img_type
        self.markers.value = initial_markers
        self.plot_style.value = initial_plot_style
        self.resolution.value = initial_resolution
        self.show_shadow.value = initial_show_shadow

        def _handle_burst_cmap_link(change) -> None:
            self.burst_cmap_outer.disabled = change["new"]

        self.burst_cmap_link.observe(_handle_burst_cmap_link, names="value")

        def _handle_burst_color_bg_trnsp(change) -> None:
            self.burst_color_bg.disabled = change["new"]

        self.burst_color_bg_trnsp.observe(_handle_burst_color_bg_trnsp, names="value")

        def _handle_cutoff(change) -> None:
            self.cutoff.disabled = not change["new"]

        self.enable_cutoff.observe(_handle_cutoff, names="value")

    @beartype
    def asdict(self) -> Dict[str, Any]:
        return dict((field.name, getattr(self, field.name)) for field in fields(self))

__init__(*, initial_alpha: float = DEFAULT_ALPHA, initial_burst_cmap_inner: str = DEFAULT_CMAP_BURST_INNER, initial_burst_cmap_link: bool = True, initial_burst_cmap_outer: str = DEFAULT_CMAP_BURST_OUTER, initial_burst_color_bg: str = DEFAULT_COLOR_BG, initial_burst_color_bg_trnsp: bool = False, initial_burst_color_text: str = DEFAULT_COLOR_TEXT, initial_burst_columns: int = DEFAULT_COLS_BURST, initial_burst_swap: bool = False, initial_burst_zero_fill_normalize: bool = False, initial_enable_cutoff: bool = True, initial_graph_type: TraditionalPlotType = TraditionalPlotType.NORMAL, initial_img_type: ImageType = ImageType.PNG, initial_markers: str = DEFAULT_MARKERS, initial_plot_style: str = DEFAULT_PLOT_STYLE, initial_resolution: int = DEFAULT_RESOLUTION, initial_show_shadow: bool = False)

Source code in anydyce/viz.py
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
@beartype
def __init__(
    self,
    *,
    initial_alpha: float = DEFAULT_ALPHA,
    initial_burst_cmap_inner: str = DEFAULT_CMAP_BURST_INNER,
    initial_burst_cmap_link: bool = True,
    initial_burst_cmap_outer: str = DEFAULT_CMAP_BURST_OUTER,
    initial_burst_color_bg: str = DEFAULT_COLOR_BG,
    initial_burst_color_bg_trnsp: bool = False,
    initial_burst_color_text: str = DEFAULT_COLOR_TEXT,
    initial_burst_columns: int = DEFAULT_COLS_BURST,
    initial_burst_swap: bool = False,
    initial_burst_zero_fill_normalize: bool = False,
    initial_enable_cutoff: bool = True,
    initial_graph_type: TraditionalPlotType = TraditionalPlotType.NORMAL,
    initial_img_type: ImageType = ImageType.PNG,
    initial_markers: str = DEFAULT_MARKERS,
    initial_plot_style: str = DEFAULT_PLOT_STYLE,
    initial_resolution: int = DEFAULT_RESOLUTION,
    initial_show_shadow: bool = False,
):
    super().__init__()

    if initial_plot_style not in matplotlib.style.available:
        warnings.warn(
            f"unrecognized plot style {initial_plot_style!r}; reverting to 'default'",
            category=RuntimeWarning,
        )
        initial_plot_style = "default"

    self.alpha.value = initial_alpha
    self.burst_cmap_inner.value = initial_burst_cmap_inner
    self.burst_cmap_link.value = initial_burst_cmap_link
    self.burst_cmap_outer.disabled = initial_burst_cmap_link
    self.burst_cmap_outer.value = initial_burst_cmap_outer
    self.burst_color_bg.value = initial_burst_color_bg
    self.burst_color_bg_trnsp.value = initial_burst_color_bg_trnsp
    self.burst_color_text.value = initial_burst_color_text
    self.burst_columns.value = initial_burst_columns
    self.burst_swap.value = initial_burst_swap
    self.burst_zero_fill_normalize.value = initial_burst_zero_fill_normalize
    self.cutoff.disabled = not initial_enable_cutoff
    self.enable_cutoff.value = initial_enable_cutoff
    self.graph_type.value = initial_graph_type
    self.img_type.value = initial_img_type
    self.markers.value = initial_markers
    self.plot_style.value = initial_plot_style
    self.resolution.value = initial_resolution
    self.show_shadow.value = initial_show_shadow

    def _handle_burst_cmap_link(change) -> None:
        self.burst_cmap_outer.disabled = change["new"]

    self.burst_cmap_link.observe(_handle_burst_cmap_link, names="value")

    def _handle_burst_color_bg_trnsp(change) -> None:
        self.burst_color_bg.disabled = change["new"]

    self.burst_color_bg_trnsp.observe(_handle_burst_color_bg_trnsp, names="value")

    def _handle_cutoff(change) -> None:
        self.cutoff.disabled = not change["new"]

    self.enable_cutoff.observe(_handle_cutoff, names="value")

asdict() -> Dict[str, Any]

Source code in anydyce/viz.py
632
633
634
@beartype
def asdict(self) -> Dict[str, Any]:
    return dict((field.name, getattr(self, field.name)) for field in fields(self))

cumulative_probability_formatter(outcome: RealLike, probability: Fraction, h: H) -> str

Experimental

This function should be considered experimental and may change or disappear in future versions.

Formatter for use with plot_burst to inefficiently (i.e., \(O \left( {n} ^ {2} \right)\)) calculate and format cumulative probability pairs for outcome in h.

Source code in anydyce/viz.py
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
@experimental
@beartype
def cumulative_probability_formatter(
    outcome: RealLike,
    probability: Fraction,
    h: H,
) -> str:
    r"""
    !!! warning "Experimental"

        This function should be considered experimental and may change or disappear in
        future versions.

    Formatter for use with [``plot_burst``][anydyce.viz.plot_burst] to inefficiently
    (i.e., $O \left( {n} ^ {2} \right)$) calculate and format cumulative probability
    pairs for *outcome* in *h*.
    """
    le_total, ge_total = Fraction(0), Fraction(1)

    for h_outcome, h_probability in h.distribution():
        le_total += h_probability

        if math.isclose(h_outcome, outcome):
            return f"{outcome} {float(probability):.2%}; โ‰ฅ{float(le_total):.2%}; โ‰ค{float(ge_total):.2%}"

        ge_total -= h_probability

    return f"{outcome} {float(probability):.2%}"

outcome_name_formatter(outcome: RealLike, _: RealLike, __: RealLike) -> str

Experimental

This function should be considered experimental and may change or disappear in future versions.

Formatter for use with plot_burst to format each outcome. If outcome has a name attribute (e.g., as with an Enum), that is used. Otherwise outcome is passed to str and the result is used.

Source code in anydyce/viz.py
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
@experimental
@beartype
def outcome_name_formatter(outcome: RealLike, _, __) -> str:
    r"""
    !!! warning "Experimental"

        This function should be considered experimental and may change or disappear in
        future versions.

    Formatter for use with [``plot_burst``][anydyce.viz.plot_burst] to format each
    *outcome*. If *outcome* has a *name* attribute (e.g., as with an ``#!python Enum``),
    that is used. Otherwise *outcome* is passed to ``#!pythonn str`` and the result is
    used.
    """
    if hasattr(outcome, "name"):
        return f"{outcome.name}"
    else:
        return f"{str(outcome)}"

outcome_name_probability_formatter(outcome: RealLike, probability: Fraction, __: Fraction) -> str

Experimental

This function should be considered experimental and may change or disappear in future versions.

Formatter for use with plot_burst to display each outcome and probability (separated by a newline). If outcome has a name attribute (e.g., as with an Enum), that is used. Otherwise outcome is passed to str and the result is used. probability is passed to float and formatted to two decimal places.

Source code in anydyce/viz.py
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
@experimental
@beartype
def outcome_name_probability_formatter(
    outcome: RealLike, probability: Fraction, __
) -> str:
    r"""
    !!! warning "Experimental"

        This function should be considered experimental and may change or disappear in
        future versions.

    Formatter for use with [``plot_burst``][anydyce.viz.plot_burst] to display each
    outcome and probability (separated by a newline). If *outcome* has a *name*
    attribute (e.g., as with an ``#!python Enum``), that is used. Otherwise *outcome* is
    passed to ``#!pythonn str`` and the result is used. *probability* is passed to
    ``#!python float`` and formatted to two decimal places.
    """
    if hasattr(outcome, "name"):
        return f"{outcome.name}\n{float(probability):.2%}"
    else:
        return f"{str(outcome)}\n{float(probability):.2%}"

limit_for_display(h: H, cutoff: H) -> H

Experimental

This function should be considered experimental and may change or disappear in future versions.

Discards outcomes in h, starting with the smallest counts as long as the total discarded in proportion to h.total does not exceed cutoff. This can be useful in speeding up plots where there are large number of negligible probabilities.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
>>> from anydyce.viz import limit_for_display
>>> from dyce import H
>>> from fractions import Fraction
>>> h = H({1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6})
>>> h.total
21
>>> limit_for_display(h, cutoff=Fraction(5, 21))
H({3: 3, 4: 4, 5: 5, 6: 6})
>>> limit_for_display(h, cutoff=Fraction(6, 21))
H({4: 4, 5: 5, 6: 6})
Source code in anydyce/viz.py
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
@experimental
@beartype
def limit_for_display(h: H, cutoff) -> H:
    r"""
    !!! warning "Experimental"

        This function should be considered experimental and may change or disappear in
        future versions.

    Discards outcomes in *h*, starting with the smallest counts as long as the total
    discarded in proportion to ``#!python h.total`` does not exceed *cutoff*. This can
    be useful in speeding up plots where there are large number of negligible
    probabilities.

    ``` python
    >>> from anydyce.viz import limit_for_display
    >>> from dyce import H
    >>> from fractions import Fraction
    >>> h = H({1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6})
    >>> h.total
    21
    >>> limit_for_display(h, cutoff=Fraction(5, 21))
    H({3: 3, 4: 4, 5: 5, 6: 6})
    >>> limit_for_display(h, cutoff=Fraction(6, 21))
    H({4: 4, 5: 5, 6: 6})

    ```
    """
    if cutoff < 0 or cutoff > 1:
        raise ValueError(f"cutoff ({cutoff}) must be between zero and one, inclusive")

    cutoff_count = int(cutoff * h.total)

    if cutoff_count == 0:
        return h

    def _cull() -> Iterator[Tuple[RealLike, int]]:
        so_far = 0

        for outcome, count in sorted(h.items(), key=itemgetter(1)):
            so_far += count

            if so_far > cutoff_count:
                yield outcome, count

    return H(_cull())

probability_formatter(_, probability: Fraction, __: Fraction) -> str

Experimental

This function should be considered experimental and may change or disappear in future versions.

Formatter for use with plot_burst to display the probability for each outcome (but not the outcome itself). probability is passed to float and formatted to two decimal places.

Source code in anydyce/viz.py
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
@experimental
@beartype
def probability_formatter(_, probability: Fraction, __) -> str:
    r"""
    !!! warning "Experimental"

        This function should be considered experimental and may change or disappear in
        future versions.

    Formatter for use with [``plot_burst``][anydyce.viz.plot_burst] to display the
    probability for each outcome (but not the outcome itself). *probability* is passed
    to ``#!python float`` and formatted to two decimal places.
    """
    return f"{float(probability):.2%}"

values_xy_for_graph_type(h: H, graph_type: TraditionalPlotType) -> Tuple[Tuple[RealLike, ...], Tuple[float, ...]]

Source code in anydyce/viz.py
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
@experimental
@beartype
def values_xy_for_graph_type(
    h: H,
    graph_type: TraditionalPlotType,
) -> Tuple[Tuple[RealLike, ...], Tuple[float, ...]]:
    outcomes, probabilities = h.distribution_xy() if h else ((), ())

    if graph_type is TraditionalPlotType.AT_LEAST:
        probabilities = tuple(accumulate(probabilities, __sub__, initial=1.0))[:-1]
    elif graph_type is TraditionalPlotType.AT_MOST:
        probabilities = tuple(accumulate(probabilities, __add__, initial=0.0))[1:]
    elif graph_type is TraditionalPlotType.NORMAL:
        pass
    else:
        assert False, f"unrecognized graph type {graph_type}"

    return outcomes, probabilities

plot_bar(ax: Axes, hs: Sequence[Tuple[str, H]], graph_type: TraditionalPlotType = TraditionalPlotType.NORMAL, alpha: float = DEFAULT_ALPHA, shadow: bool = False) -> None

Experimental

This function should be considered experimental and may change or disappear in future versions.

Plots a bar graph of hs using ax with alpha and shadow. hs is a sequence of two-tuples (pairs) of strings (labels) and H objects. Bars are interleaved and non-overlapping, so this is best suited to plots where hs contains a small number of histograms.

Source code in anydyce/viz.py
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
@experimental
@beartype
def plot_bar(
    ax: Axes,
    hs: Sequence[Tuple[str, H]],
    graph_type: TraditionalPlotType = TraditionalPlotType.NORMAL,
    alpha: float = DEFAULT_ALPHA,
    shadow: bool = False,
) -> None:
    r"""
    !!! warning "Experimental"

        This function should be considered experimental and may change or disappear in
        future versions.

    Plots a bar graph of *hs* using
    [*ax*](https://matplotlib.org/stable/api/axes_api.html#the-axes-class) with *alpha*
    and *shadow*. *hs* is a sequence of two-tuples (pairs) of strings (labels) and ``H``
    objects. Bars are interleaved and non-overlapping, so this is best suited to plots
    where *hs* contains a small number of histograms.
    """
    ax.yaxis.set_major_formatter(matplotlib.ticker.PercentFormatter(xmax=1))
    width = 0.8
    bar_kw: Dict[str, Any] = dict(alpha=alpha)

    if hs:
        bar_kw.update(dict(width=width / len(hs)))

    if shadow:
        bar_kw.update(
            dict(
                path_effects=[
                    matplotlib.patheffects.withSimplePatchShadow(),
                    matplotlib.patheffects.Normal(),
                ]
            )
        )

    unique_outcomes = sorted(set(chain.from_iterable(h.outcomes() for _, h in hs)))

    if hs:
        ax.set_xticks(unique_outcomes)
        ax.set_xlim(
            (
                min(unique_outcomes, default=0) - 1.0,
                max(unique_outcomes, default=0) + 1.0,
            )
        )

    for i, (label, h) in enumerate(hs):
        # Orient to the middle of each bar ((i + 0.5) ... ) whose width is an even share
        # of the total width (... * width / len(hs) ...) and center the whole cluster of
        # bars around the data point (... - width / 2)
        adj = (i + 0.5) * width / len(hs) - width / 2
        outcomes, values = values_xy_for_graph_type(h, graph_type)
        ax.bar(
            [outcome + adj for outcome in outcomes],
            values,
            label=label,
            **bar_kw,
        )

plot_line(ax: Axes, hs: Sequence[Tuple[str, H]], graph_type: TraditionalPlotType = TraditionalPlotType.NORMAL, alpha: float = DEFAULT_ALPHA, shadow: bool = False, markers: str = 'o') -> None

Experimental

This function should be considered experimental and may change or disappear in future versions.

Plots a line graph of hs using ax with alpha and shadow. hs is a sequence of two-tuples (pairs) of strings (labels) and dyce.H objects. markers is cycled through when creating each line. For example, if markers is "o+", the first histogram in hs will be plotted with a circle, the second will be plotted with a plus, the third will be plotted with a circle, the fourth will be plotted with a plus, and so on.

Source code in anydyce/viz.py
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
@experimental
@beartype
def plot_line(
    ax: Axes,
    hs: Sequence[Tuple[str, H]],
    graph_type: TraditionalPlotType = TraditionalPlotType.NORMAL,
    alpha: float = DEFAULT_ALPHA,
    shadow: bool = False,
    markers: str = "o",
) -> None:
    r"""
    !!! warning "Experimental"

        This function should be considered experimental and may change or disappear in
        future versions.

    Plots a line graph of *hs* using
    [*ax*](https://matplotlib.org/stable/api/axes_api.html#the-axes-class) with *alpha*
    and *shadow*. *hs* is a sequence of two-tuples (pairs) of strings (labels) and
    ``#!python dyce.H`` objects. *markers* is cycled through when creating each line.
    For example, if *markers* is ``#!python "o+"``, the first histogram in *hs* will be
    plotted with a circle, the second will be plotted with a plus, the third will be
    plotted with a circle, the fourth will be plotted with a plus, and so on.
    """
    ax.yaxis.set_major_formatter(matplotlib.ticker.PercentFormatter(xmax=1))
    plot_kw: Dict[str, Any] = dict(alpha=alpha)

    if shadow:
        plot_kw.update(
            dict(
                path_effects=[
                    matplotlib.patheffects.SimpleLineShadow(),
                    matplotlib.patheffects.Normal(),
                ]
            )
        )

    unique_outcomes = sorted(set(chain.from_iterable(h.outcomes() for _, h in hs)))

    if hs:
        ax.set_xticks(unique_outcomes)
        ax.set_xlim(
            (
                min(unique_outcomes, default=0) - 0.5,
                max(unique_outcomes, default=0) + 0.5,
            )
        )

    for (label, h), marker in zip(hs, cycle(markers if markers else " ")):
        outcomes, values = values_xy_for_graph_type(h, graph_type)
        ax.plot(outcomes, values, label=label, marker=marker, **plot_kw)

plot_scatter(ax: Axes, hs: Sequence[Tuple[str, H]], graph_type: TraditionalPlotType = TraditionalPlotType.NORMAL, alpha: float = DEFAULT_ALPHA, shadow: bool = False, markers: str = '<>v^dPXo') -> None

Experimental

This function should be considered experimental and may change or disappear in future versions.

Plots a scatter graph of hs using ax with alpha and shadow. hs is a sequence of two-tuples (pairs) of strings (labels) and dyce.H objects. markers is cycled through when creating each line. For example, if markers is "o+", the first histogram in hs will be plotted with a circle, the second will be plotted with a plus, the third will be plotted with a circle, the fourth will be plotted with a plus, and so on.

Source code in anydyce/viz.py
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
@experimental
@beartype
def plot_scatter(
    ax: Axes,
    hs: Sequence[Tuple[str, H]],
    graph_type: TraditionalPlotType = TraditionalPlotType.NORMAL,
    alpha: float = DEFAULT_ALPHA,
    shadow: bool = False,
    markers: str = "<>v^dPXo",
) -> None:
    r"""
    !!! warning "Experimental"

        This function should be considered experimental and may change or disappear in
        future versions.

    Plots a scatter graph of *hs* using
    [*ax*](https://matplotlib.org/stable/api/axes_api.html#the-axes-class) with *alpha*
    and *shadow*. *hs* is a sequence of two-tuples (pairs) of strings (labels) and
    ``dyce.H`` objects. *markers* is cycled through when creating each line. For
    example, if *markers* is ``#!python "o+"``, the first histogram in *hs* will be
    plotted with a circle, the second will be plotted with a plus, the third will be
    plotted with a circle, the fourth will be plotted with a plus, and so on.
    """
    ax.yaxis.set_major_formatter(matplotlib.ticker.PercentFormatter(xmax=1))
    scatter_kw: Dict[str, Any] = dict(alpha=alpha)

    if shadow:
        scatter_kw.update(
            dict(
                path_effects=[
                    matplotlib.patheffects.SimpleLineShadow(),
                    matplotlib.patheffects.Normal(),
                ]
            )
        )

    unique_outcomes = sorted(set(chain.from_iterable(h.outcomes() for _, h in hs)))

    if hs:
        ax.set_xticks(unique_outcomes)
        ax.set_xlim(
            (
                min(unique_outcomes, default=0) - 0.5,
                max(unique_outcomes, default=0) + 0.5,
            )
        )

    for (label, h), marker in zip(hs, cycle(markers if markers else " ")):
        outcomes, values = values_xy_for_graph_type(h, graph_type)
        ax.scatter(outcomes, values, label=label, marker=marker, **scatter_kw)

plot_burst(ax: Axes, h_inner: H, h_outer: Optional[H] = None, title: Optional[str] = None, inner_formatter: HFormatterT = outcome_name_formatter, inner_cmap: Union[str, matplotlib.colors.Colormap] = DEFAULT_CMAP_BURST_INNER, outer_formatter: Optional[HFormatterT] = None, outer_cmap: Union[str, matplotlib.colors.Colormap, None] = None, text_color: str = DEFAULT_COLOR_TEXT, alpha: float = DEFAULT_ALPHA) -> None

Experimental

This function should be considered experimental and may change or disappear in future versions.

Creates a dual, overlapping, cocentric pie chart in ax, which can be useful for visualizing relative probability distributions. Examples can be found in Additional interfaces.

Source code in anydyce/viz.py
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
@experimental
@beartype
def plot_burst(
    ax: Axes,
    h_inner: H,
    h_outer: Optional[H] = None,
    title: Optional[str] = None,
    inner_formatter: HFormatterT = outcome_name_formatter,
    inner_cmap: Union[str, matplotlib.colors.Colormap] = DEFAULT_CMAP_BURST_INNER,
    outer_formatter: Optional[HFormatterT] = None,
    outer_cmap: Union[str, matplotlib.colors.Colormap, None] = None,
    text_color: str = DEFAULT_COLOR_TEXT,
    alpha: float = DEFAULT_ALPHA,
) -> None:
    r"""
    !!! warning "Experimental"

        This function should be considered experimental and may change or disappear in
        future versions.

    Creates a dual, overlapping, cocentric pie chart in
    [*ax*](https://matplotlib.org/stable/api/axes_api.html#the-axes-class), which can be
    useful for visualizing relative probability distributions. Examples can be found in
    [Additional interfaces](index.md#additional-interfaces).
    """
    h_outer = h_inner if h_outer is None else h_outer

    if outer_formatter is None:
        if h_outer == h_inner:
            outer_formatter = probability_formatter
        else:
            outer_formatter = inner_formatter

    outer_cmap = inner_cmap if outer_cmap is None else outer_cmap

    inner = (
        (
            inner_formatter(outcome, probability, h_inner)
            if probability >= _LABEL_LIM
            else "",
            probability,
        )
        for outcome, probability in h_inner.distribution()
    )

    inner_labels, inner_values = tuple(zip(*inner)) if h_inner else ((), ())
    inner_colors = graph_colors(inner_cmap, inner_values, alpha)

    outer = (
        (
            outer_formatter(outcome, probability, h_outer)
            if probability >= _LABEL_LIM
            else "",
            probability,
        )
        for outcome, probability in h_outer.distribution()
    )

    outer_labels, outer_values = tuple(zip(*outer)) if h_outer else ((), ())
    outer_colors = graph_colors(outer_cmap, outer_values, alpha)

    if title:
        ax.set_title(
            title,
            fontdict={"fontweight": "bold", "color": text_color},
            pad=24.0,
        )

    ax.pie(
        outer_values,
        labels=outer_labels,
        radius=1.0,
        labeldistance=1.15,
        startangle=90,
        colors=outer_colors,
        textprops=dict(color=text_color),
        wedgeprops=dict(width=0.8, edgecolor=text_color),
    )
    ax.pie(
        inner_values,
        labels=inner_labels,
        radius=0.85,
        labeldistance=0.7,
        startangle=90,
        colors=inner_colors,
        textprops=dict(color=text_color),
        wedgeprops=dict(width=0.5, edgecolor=text_color),
    )
    ax.set(aspect="equal")

plot_burst_subplot(h_inner: H, h_outer: Optional[H] = None, title: Optional[str] = None, inner_formatter: HFormatterT = outcome_name_formatter, inner_cmap: Union[str, matplotlib.colors.Colormap] = DEFAULT_CMAP_BURST_INNER, outer_formatter: Optional[HFormatterT] = None, outer_cmap: Union[str, matplotlib.colors.Colormap, None] = None, text_color: str = DEFAULT_COLOR_TEXT, alpha: float = DEFAULT_ALPHA) -> Tuple[Figure, Axes]

Experimental

This function should be considered experimental and may change or disappear in future versions.

Wrapper around plot_burst that creates a figure, axis pair, calls matplotlib.pyplot.tight_layout, and returns the pair.

Source code in anydyce/viz.py
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
@experimental
@beartype
def plot_burst_subplot(
    h_inner: H,
    h_outer: Optional[H] = None,
    title: Optional[str] = None,
    inner_formatter: HFormatterT = outcome_name_formatter,
    inner_cmap: Union[str, matplotlib.colors.Colormap] = DEFAULT_CMAP_BURST_INNER,
    outer_formatter: Optional[HFormatterT] = None,
    outer_cmap: Union[str, matplotlib.colors.Colormap, None] = None,
    text_color: str = DEFAULT_COLOR_TEXT,
    alpha: float = DEFAULT_ALPHA,
) -> Tuple[Figure, Axes]:
    r"""
    !!! warning "Experimental"

        This function should be considered experimental and may change or disappear in
        future versions.

    Wrapper around [``plot_burst``][anydyce.viz.plot_burst] that creates a figure, axis
    pair, calls
    [``matplotlib.pyplot.tight_layout``](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.tight_layout.html),
    and returns the pair.
    """
    fig, ax = matplotlib.pyplot.subplots()
    plot_burst(
        ax,
        h_inner,
        h_outer,
        title,
        inner_formatter,
        inner_cmap,
        outer_formatter,
        outer_cmap,
        text_color,
        alpha,
    )

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        matplotlib.pyplot.tight_layout()

    return fig, ax