Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8dbb6b7
fix: lock
miloth Jun 28, 2026
cf703af
chore: update dependencies, drop 3.9 and 3.10
miloth Jun 28, 2026
cc8b065
fix: update to "new" `matplotlib` API
miloth Jun 28, 2026
84f4485
fix: issue with multiple shows without closing
miloth Jun 28, 2026
caf121b
chore: update upstream palette
miloth Jun 28, 2026
2d06118
chore: ignore macOS temp files
miloth Jun 28, 2026
f0940be
fix: imports and reload
miloth Jun 28, 2026
309c066
fix: new mpl style loading
miloth Jun 28, 2026
f7ed114
feat: remove asset get as not used in docs
miloth Jun 28, 2026
79ada89
feat: codegen base style out of module
miloth Jun 28, 2026
870196f
feat: styles generated at module root
miloth Jun 28, 2026
9fe79cd
feat: example loads styles from module root
miloth Jun 28, 2026
05c4823
feat: modularize codegen
miloth Jun 28, 2026
7184279
feat: expose style and cmap as properties
miloth Jun 28, 2026
4c043d7
feat!: do not expose functions as all easily obtainable through plt
miloth Jun 28, 2026
43de2fd
feat: update docs
miloth Jun 28, 2026
b4eab8f
feat: slimmer examples
miloth Jun 28, 2026
965c549
feat: update matplotlib tests
miloth Jun 28, 2026
07853d3
chore: major bump
miloth Jun 28, 2026
327ce21
feat: plot examples more "typed"
miloth Aug 3, 2026
a8ce239
fix: please `mypy` and `ruff`
miloth Aug 3, 2026
df22edf
fix: better map definition
miloth Aug 3, 2026
7e8d493
fix: better map definition
miloth Aug 3, 2026
93fd4a5
chore: merge main
backwardspy Nov 10, 2026
0db00b0
refactor(matplotlib): apply review suggestions
backwardspy Nov 10, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ coverage.xml
htmlcov

# gh-pages build script output
/gh-pages
/gh-pages

# OS Files
.DS_Store
62 changes: 35 additions & 27 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def load_palette() -> Palette:
)


def codegen(palette: Palette) -> str:
def palette_codegen(palette: Palette) -> str:
"""Generate contents of `catppuccin/palette.py`."""
lines = [
HEADER,
Expand All @@ -152,28 +152,9 @@ def codegen(palette: Palette) -> str:
return "\n".join(lines)


if __name__ == "__main__":
# we build from the latest palette data rather than a stale `catppuccin.PALETTE`.
# `catppuccin.PALETTE` gets rewritten later, but even then we'd have to
# reload a bunch of modules to get it loaded.
palette = load_palette()

print("running codegen")
palette_path = Path.cwd() / "catppuccin" / "palette.py"
with palette_path.open("w", newline="\n") as f:
f.write(codegen(palette))
print("formatting with ruff")
ruff_format = f"ruff format {palette_path}"
subprocess.run(ruff_format.split(), check=True, stdout=subprocess.DEVNULL)
print("palette.py generation complete")

# Generate the matplotlib styles
print("generating matplotlib styles")
from catppuccin.extras.matplotlib import CATPPUCCIN_STYLE_DIRECTORY

template_text = (
CATPPUCCIN_STYLE_DIRECTORY / "_catppuccin_template.txt"
).read_text()
def generate_mpl_styles(palette: Palette) -> None:
"""Generate the matplotlib .mplstyle files."""
template_text = (Path.cwd() / "matplotlib_template.txt").read_text()

for key, flavor in asdict(palette).items():
print(f"- {key}")
Expand All @@ -184,13 +165,13 @@ def codegen(palette: Palette) -> str:
f"<{color}>",
flavor["colors"][color]["hex"].replace("#", ""),
)
style_path = CATPPUCCIN_STYLE_DIRECTORY / f"{key}.mplstyle"
style_path = Path.cwd() / "catppuccin" / f"{key}.mplstyle"
with style_path.open("w", newline="\n") as f:
f.write(text)
print("matplotlib styles generation complete")

# Generate the IPython themes
print("generating ipython themes")

def generate_ipython_themes(palette: Palette) -> None:
"""Generate the IPython theme JSON files."""
IPYTHON_THEME_DIRECTORY.mkdir(exist_ok=True)
for flavor in palette:
for theme in (ipython_theme(flavor), ipython_noitalic_theme(flavor)):
Expand All @@ -199,4 +180,31 @@ def codegen(palette: Palette) -> str:
with theme_path.open("w", newline="\n") as f:
json.dump(theme, f, indent=2)
f.write("\n")


def main() -> None: # noqa: D103
# we build from the latest palette data rather than a stale `catppuccin.PALETTE`.
# `catppuccin.PALETTE` gets rewritten below, but even then we'd have to
# reload a bunch of modules to get it loaded.
palette = load_palette()

print("running palette codegen")
palette_path = Path.cwd() / "catppuccin" / "palette.py"
with palette_path.open("w", newline="\n") as f:
f.write(palette_codegen(palette))
print("formatting with ruff")
ruff_format = f"ruff format {palette_path}"
subprocess.run(ruff_format.split(), check=True, stdout=subprocess.DEVNULL)
print("palette.py generation complete")

print("generating matplotlib styles")
generate_mpl_styles(palette)
print("matplotlib styles generation complete")

print("generating ipython themes")
generate_ipython_themes(palette)
print("ipython themes generation complete")


if __name__ == "__main__":
main()
7 changes: 2 additions & 5 deletions catppuccin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@

from catppuccin.palette import PALETTE as PALETTE

# Attempt to register styles and colormaps if matplotlib is available
# Register colormaps if matplotlib is available
if importlib.util.find_spec("matplotlib") is not None:
from catppuccin.extras.matplotlib import _register_colormap_list, _register_styles

_register_styles()
_register_colormap_list()
import catppuccin.extras.matplotlib # noqa: F401
151 changes: 78 additions & 73 deletions catppuccin/extras/matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,107 +7,112 @@
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 `mpl.style.use`
1. Load a style, using `plt.style.use`

```python
import catppuccin
import matplotlib as mpl
import matplotlib.pyplot as plt
```python
import matplotlib.pyplot as plt

mpl.style.use(catppuccin.PALETTE.mocha.identifier)
plt.plot([0,1,2,3], [1,2,3,4])
plt.show()
```
plt.style.use("catppuccin.mocha")
plt.plot([0,1,2,3], [1,2,3,4])
plt.show()
```

1. Mix it with different stylesheets!

```python
import catppuccin
import matplotlib as mpl
import matplotlib.pyplot as plt
```python
import matplotlib.pyplot as plt

mpl.style.use(["ggplot", catppuccin.PALETTE.mocha.identifier])
plt.plot([0,1,2,3], [1,2,3,4])
plt.show()
```
plt.style.use(["ggplot", "catppuccin.mocha"])
plt.plot([0,1,2,3], [1,2,3,4])
plt.show()
```

1. Pick the style from a flavor, using `style_name`

```python
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()
```

1. Load individual colors

```python
import matplotlib.pyplot as plt
import catppuccin
from catppuccin.extras.matplotlib import load_color
```python
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()
```

1. Just use the colormaps in your plots

```python
import matplotlib.pyplot as plt
import numpy as np

color = load_color(catppuccin.PALETTE.latte.identifier, "peach")
plt.plot([0,1,2,3], [1,2,3,4], color=color)
plt.show()
```
rng = np.random.default_rng()
data = rng.integers(10, size=(30, 30))

plt.imshow(data, cmap="catppuccin.mocha")
plt.show()
```

1. Define custom colormaps

```python
import matplotlib.pyplot as plt
import numpy as np
import catppuccin
from catppuccin.extras.matplotlib import get_colormap_from_list

cmap = get_colormap_from_list(
catppuccin.PALETTE.frappe.identifier,
["red", "peach", "yellow", "green"],
)
rng = np.random.default_rng()
data = rng.integers(2, size=(30, 30))

plt.imshow(data, cmap=cmap)
plt.show()
```
```python
import matplotlib.pyplot as plt
import numpy as np
import catppuccin
from matplotlib.colors import LinearSegmentedColormap

cmap = LinearSegmentedColormap.from_list(
"my_custom_palette",
[
color.hex for color in catppuccin.PALETTE.frappe.colors
if color.identifier in ["red", "peach", "yellow", "green"]
],
)
rng = np.random.default_rng()
data = rng.integers(10, size=(30, 30))

plt.imshow(data, cmap=cmap)
plt.show()
```
"""

from __future__ import annotations

from dataclasses import asdict
from pathlib import Path
from typing import TYPE_CHECKING, cast
from typing import TYPE_CHECKING

import matplotlib as mpl
import matplotlib.colors
import matplotlib.style

from catppuccin.palette import PALETTE

if TYPE_CHECKING:
from collections.abc import Iterable

CATPPUCCIN_STYLE_DIRECTORY = Path(__file__).parent / "matplotlib_styles"
DEFAULT_COLORMAP_COLORS = ("base", "blue")


def _register_styles() -> None:
"""Register the included stylesheets in the mpl style library."""
for path in CATPPUCCIN_STYLE_DIRECTORY.glob("*.mplstyle"):
style = mpl.rc_params_from_file(path, use_default_template=False)
mpl.style.library.setdefault(path.stem, mpl.RcParams()).update(style)
from catppuccin.models import Flavor

DEFAULT_COLORMAP_COLORS = ("blue", "teal", "yellow", "peach", "red")

def _register_colormap_list() -> None:
"""Register the included color maps in the `matplotlib` colormap library."""
for palette_name in asdict(PALETTE):
cmap = get_colormap_from_list(palette_name, DEFAULT_COLORMAP_COLORS)
mpl.colormaps.register(cmap=cmap, name=palette_name, force=True)

def style_name(flavor: Flavor) -> str:
"""Name of the matplotlib style for a flavor, for use with `plt.style.use`.

def get_colormap_from_list(
palette_name: str,
color_names: Iterable[str],
) -> matplotlib.colors.LinearSegmentedColormap:
"""Get a `matplotlib` colormap from a list of colors for a specific palette."""
colors = [load_color(palette_name, color_name) for color_name in color_names]
return matplotlib.colors.LinearSegmentedColormap.from_list(palette_name, colors)
The flavor's colormap is registered under the same name.
"""
return f"catppuccin.{flavor.identifier}"


def load_color(palette_name: str, color_name: str) -> str:
"""Load the color for a given palette and color name."""
return cast(
str,
PALETTE.__getattribute__(palette_name).colors.__getattribute__(color_name).hex,
# Register the included color maps in the `matplotlib` colormap library.
for _palette in PALETTE:
_cmap = matplotlib.colors.LinearSegmentedColormap.from_list(
style_name(_palette),
[getattr(_palette.colors, name).hex for name in DEFAULT_COLORMAP_COLORS],
)
mpl.colormaps.register(cmap=_cmap, force=True)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Main colors: blue, peach, green, red, mauve, maroon, pink, rosewater, teal, lavender
axes.prop_cycle: cycler('color', ['8caaee', 'ef9f76', 'a6d189', 'e78284', 'ca9ee6', 'ea999c', 'f4b8e4', 'f2d5cf', '81c8be', 'babbf1'])

image.cmap: frappe
image.cmap: catppuccin.frappe

# Font color: Text
text.color: c6d0f5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Main colors: blue, peach, green, red, mauve, maroon, pink, rosewater, teal, lavender
axes.prop_cycle: cycler('color', ['1e66f5', 'fe640b', '40a02b', 'd20f39', '8839ef', 'e64553', 'ea76cb', 'dc8a78', '179299', '7287fd'])

image.cmap: latte
image.cmap: catppuccin.latte

# Font color: Text
text.color: 4c4f69
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Main colors: blue, peach, green, red, mauve, maroon, pink, rosewater, teal, lavender
axes.prop_cycle: cycler('color', ['8aadf4', 'f5a97f', 'a6da95', 'ed8796', 'c6a0f6', 'ee99a0', 'f5bde6', 'f4dbd6', '8bd5ca', 'b7bdf8'])

image.cmap: macchiato
image.cmap: catppuccin.macchiato

# Font color: Text
text.color: cad3f5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Main colors: blue, peach, green, red, mauve, maroon, pink, rosewater, teal, lavender
axes.prop_cycle: cycler('color', ['89b4fa', 'fab387', 'a6e3a1', 'f38ba8', 'cba6f7', 'eba0ac', 'f5c2e7', 'f5e0dc', '94e2d5', 'b4befe'])

image.cmap: mocha
image.cmap: catppuccin.mocha

# Font color: Text
text.color: cdd6f4
Expand Down
Loading
Loading