Skip to content

Additional Patches

ptfp.patches

MulticolorPatch

MulticolorPatch(colors)

Bases: object

A matplotlib patch containing multiple colors, for multi-color legend entries. Directly taken from https://stackoverflow.com/questions/31908982/multi-color-legend-entry

Example usage:

multicolor_patch = ptfp.patches.MulticolorPatch(colors)
ax.legend(
    handles=[multicolor_patch],
    labels=labels,
    handler_map={
        ptfp.patches.MulticolorPatch: ptfp.patches.MulticolorPatchHandler()
    }
)

Parameters

colors : matplotlib.colors.Colormap
    The discrete colormap to make the patch from
Source code in ptfp/patches.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def __init__(self, colors: matplotlib.colors.Colormap):
    """A matplotlib patch containing multiple colors,
    for multi-color legend entries.
    Directly taken from
    https://stackoverflow.com/questions/31908982/multi-color-legend-entry

    Example usage:
    ```
    multicolor_patch = ptfp.patches.MulticolorPatch(colors)
    ax.legend(
        handles=[multicolor_patch],
        labels=labels,
        handler_map={
            ptfp.patches.MulticolorPatch: ptfp.patches.MulticolorPatchHandler()
        }
    )
    ```

    Parameters
    ==========
        colors : matplotlib.colors.Colormap
            The discrete colormap to make the patch from
    """
    self.colors = colors

colors instance-attribute

colors = colors

MulticolorPatchHandler

Bases: object

The handler for the multicolor patch

legend_artist

legend_artist(legend, orig_handle, fontsize, handlebox)

The artist to use for illustrating the multicolor patch on the legend

Parameters
legend: matplotlib.legend.Legend
    The legend to write the artist to
orig_handle: matplotlib.artist.Artist
    The original artist in use
fontsize: float
    The fonstize for the corresponding text
handlebox: matplotlib.offsetbox.OffsetBox
    The box used to set the dimensions of the final illustration
Source code in ptfp/patches.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def legend_artist(
    self,
    legend: matplotlib.legend.Legend,
    orig_handle: matplotlib.artist.Artist,
    fontsize: float,
    handlebox: matplotlib.offsetbox.OffsetBox,
):
    """The artist to use for illustrating the multicolor patch on the legend

    Parameters
    ==========
        legend: matplotlib.legend.Legend
            The legend to write the artist to
        orig_handle: matplotlib.artist.Artist
            The original artist in use
        fontsize: float
            The fonstize for the corresponding text
        handlebox: matplotlib.offsetbox.OffsetBox
            The box used to set the dimensions of the final illustration

    """
    width, height = handlebox.width, handlebox.height
    patches = []
    for i, c in enumerate(orig_handle.colors):
        patches.append(
            plt.Rectangle(
                [
                    width / len(orig_handle.colors) * i - handlebox.xdescent,
                    -handlebox.ydescent,
                ],
                width / len(orig_handle.colors),
                height,
                facecolor=c,
                edgecolor="none",
            )
        )

    patch = PatchCollection(patches, match_original=True)

    handlebox.add_artist(patch)
    return patch