catppuccin.extras.matplotlib

Soothing pastel theme for matplotlib.

The following code was ported from catppuccin/matplotlib. Thanks to Bram de Wilde for the original source code and for allowing this port.

The library tries to register styles and colormaps if matplotlib is installed. See the examples below for some use cases:

  1. Load a style, using plt.style.use

    import matplotlib.pyplot as plt
    
    plt.style.use("catppuccin.mocha")
    plt.plot([0,1,2,3], [1,2,3,4])
    plt.show()
    
  2. Mix it with different stylesheets!

    import matplotlib.pyplot as plt
    
    plt.style.use(["ggplot", "catppuccin.mocha"])
    plt.plot([0,1,2,3], [1,2,3,4])
    plt.show()
    
  3. Pick the style from a flavor, using style_name

    import catppuccin
    import matplotlib.pyplot as plt
    from catppuccin.extras.matplotlib import style_name
    
    flavor = catppuccin.PALETTE.latte
    plt.style.use(style_name(flavor))
    plt.plot([0,1,2,3], [1,2,3,4])
    plt.show()
    
  4. Load individual colors

    import matplotlib.pyplot as plt
    import catppuccin
    
    plt.plot([0,1,2,3], [1,2,3,4], color=catppuccin.PALETTE.latte.colors.peach.hex)
    plt.show()
    
  5. Just use the colormaps in your plots

    import matplotlib.pyplot as plt
    import numpy as np
    
    rng = np.random.default_rng()
    data = rng.integers(10, size=(30, 30))
    
    plt.imshow(data, cmap="catppuccin.mocha")
    plt.show()
    
  6. Define custom colormaps

    import matplotlib.pyplot as plt
    import numpy as np
    import catppuccin
    from matplotlib.colors import LinearSegmentedColormap
    
    cmap = LinearSegmentedColormap.from_list(
        "my_custom_palette",
        [
            catppuccin.PALETTE.frappe.colors[name].hex
            for name in ["red", "peach", "yellow", "green"]
        ],
    )
    rng = np.random.default_rng()
    data = rng.integers(10, size=(30, 30))
    
    plt.imshow(data, cmap=cmap)
    plt.show()
    
  1"""Soothing pastel theme for `matplotlib`.
  2
  3The following code was ported from [catppuccin/matplotlib](https://github.com/catppuccin/matplotlib).
  4Thanks to [Bram de Wilde](https://github.com/brambozz) for the original source code and
  5for allowing this port.
  6
  7The library tries to register styles and colormaps if `matplotlib` is installed.
  8See the examples below for some use cases:
  9
 101. Load a style, using `plt.style.use`
 11
 12    ```python
 13    import matplotlib.pyplot as plt
 14
 15    plt.style.use("catppuccin.mocha")
 16    plt.plot([0,1,2,3], [1,2,3,4])
 17    plt.show()
 18    ```
 19
 201. Mix it with different stylesheets!
 21
 22    ```python
 23    import matplotlib.pyplot as plt
 24
 25    plt.style.use(["ggplot", "catppuccin.mocha"])
 26    plt.plot([0,1,2,3], [1,2,3,4])
 27    plt.show()
 28    ```
 29
 301. Pick the style from a flavor, using `style_name`
 31
 32    ```python
 33    import catppuccin
 34    import matplotlib.pyplot as plt
 35    from catppuccin.extras.matplotlib import style_name
 36
 37    flavor = catppuccin.PALETTE.latte
 38    plt.style.use(style_name(flavor))
 39    plt.plot([0,1,2,3], [1,2,3,4])
 40    plt.show()
 41    ```
 42
 431. Load individual colors
 44
 45    ```python
 46    import matplotlib.pyplot as plt
 47    import catppuccin
 48
 49    plt.plot([0,1,2,3], [1,2,3,4], color=catppuccin.PALETTE.latte.colors.peach.hex)
 50    plt.show()
 51    ```
 52
 531. Just use the colormaps in your plots
 54
 55    ```python
 56    import matplotlib.pyplot as plt
 57    import numpy as np
 58
 59    rng = np.random.default_rng()
 60    data = rng.integers(10, size=(30, 30))
 61
 62    plt.imshow(data, cmap="catppuccin.mocha")
 63    plt.show()
 64    ```
 65
 661. Define custom colormaps
 67
 68    ```python
 69    import matplotlib.pyplot as plt
 70    import numpy as np
 71    import catppuccin
 72    from matplotlib.colors import LinearSegmentedColormap
 73
 74    cmap = LinearSegmentedColormap.from_list(
 75        "my_custom_palette",
 76        [
 77            catppuccin.PALETTE.frappe.colors[name].hex
 78            for name in ["red", "peach", "yellow", "green"]
 79        ],
 80    )
 81    rng = np.random.default_rng()
 82    data = rng.integers(10, size=(30, 30))
 83
 84    plt.imshow(data, cmap=cmap)
 85    plt.show()
 86    ```
 87"""
 88
 89from __future__ import annotations
 90
 91from typing import TYPE_CHECKING
 92
 93import matplotlib as mpl
 94import matplotlib.colors
 95
 96from catppuccin.palette import PALETTE
 97
 98if TYPE_CHECKING:
 99    from catppuccin.models import Flavor
100
101DEFAULT_COLORMAP_COLORS = ("blue", "teal", "yellow", "peach", "red")
102
103
104def style_name(flavor: Flavor) -> str:
105    """Name of the matplotlib style for a flavor, for use with `plt.style.use`.
106
107    The flavor's colormap is registered under the same name.
108    """
109    return f"catppuccin.{flavor.identifier}"
110
111
112# Register the included color maps in the `matplotlib` colormap library.
113for _palette in PALETTE:
114    _cmap = matplotlib.colors.LinearSegmentedColormap.from_list(
115        style_name(_palette),
116        [_palette.colors[name].hex for name in DEFAULT_COLORMAP_COLORS],
117    )
118    mpl.colormaps.register(cmap=_cmap, force=True)
def style_name(flavor: catppuccin.models.Flavor) -> str:
105def style_name(flavor: Flavor) -> str:
106    """Name of the matplotlib style for a flavor, for use with `plt.style.use`.
107
108    The flavor's colormap is registered under the same name.
109    """
110    return f"catppuccin.{flavor.identifier}"

Name of the matplotlib style for a flavor, for use with plt.style.use.

The flavor's colormap is registered under the same name.