Skip to content

Commit f499771

Browse files
committed
feat: add color examples and documentation for Matplotlib styles; include named, default, and colorblind palettes
1 parent d680243 commit f499771

File tree

11 files changed

+133
-1
lines changed

11 files changed

+133
-1
lines changed

docs/color/color-CN.png

126 KB
Loading

docs/color/color-CN.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
4+
plt.style.use(["physics_plot.pp_base", "physics_plot.colors.ggplot"])
5+
6+
t = np.linspace(0, 10, 100)
7+
y = np.sin(t)
8+
9+
fig, ax = plt.subplots(figsize=(4, 2), constrained_layout=True)
10+
ax.plot(t, y, "C0", label="C0")
11+
ax.plot(t, y + 1, "C4", label="C4")
12+
ax.set_ylabel(r"$y(t)$")
13+
ax.set_xlabel(r"Time $t$ [s]")
14+
ax.legend(loc="upper right")
15+
fig.suptitle(r"\texttt{ggplot} - color (from Matplotlib)")
16+
fig.savefig("color-CN.png")

docs/color/color-colorblind.png

208 KB
Loading

docs/color/color-default.png

203 KB
Loading

docs/color/color-ggplot.png

201 KB
Loading

docs/color/color-named.png

295 KB
Loading

docs/color/color-named.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
4+
t = np.linspace(0, 10, 100)
5+
y = np.sin(t)
6+
7+
with plt.style.context("physics_plot.pp_base"):
8+
fig, ax = plt.subplots(figsize=(4, 2.5), constrained_layout=True)
9+
ax.plot(t, y - 0.0, "r", label="r (red)")
10+
ax.plot(t, y - 0.3, "k", label="k (black)")
11+
ax.plot(t, y - 0.6, "b", label="b (blue)")
12+
ax.plot(t, y - 0.9, "g", label="g (green)")
13+
ax.plot(t, y - 1.2, "c", label="c (cyan)")
14+
ax.plot(t, y - 1.5, "m", label="m (magenta)")
15+
16+
ax.set_ylabel(r"$y(t)$")
17+
ax.set_xlabel(r"Time $t$ [s]")
18+
ax.legend(loc="upper right")
19+
fig.suptitle(r"Named colors (from Matplotlib)")
20+
fig.savefig("color-named.png")

docs/color/color.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
There are several convenient ways to specify colors in Matplotlib.
2+
3+
## Named colors
4+
5+
List of named colors is documented in the [Matplotlib documentation - Named Colors](https://matplotlib.org/stable/gallery/color/named_colors.html).
6+
A simple example using named colors is shown below.
7+
8+
```python
9+
--8<-- "docs/color/color-named.py"
10+
```
11+
![Named colors example](color-named.png)
12+
13+
14+
## Color palettes
15+
If you do not specify a color, Matplotlib will cycle through a set of colors. In addition to the default color cycle, we provide a few additional color palettes: `ggplot`[^ggplot] and `colorblind`[^colorblind] gathered from the Matplotlib and seaborn.
16+
17+
To use a different color palette in combination with the Physics Plot style sheet, just combine the style sheets as a list. For example, to use the `physics_plot.colors.ggplot` color palette globally:
18+
```python
19+
import matplotlib.pyplot as plt
20+
21+
plt.style.use(["physics_plot.pp_base", "physics_plot.colors.ggplot"])
22+
23+
# ... your plotting code here ...
24+
```
25+
or locally with a context manager:
26+
```python
27+
import matplotlib.pyplot as plt
28+
29+
with plt.style.context(["physics_plot.pp_base", "physics_plot.colors.ggplot"]):
30+
# ... your plotting code here ...
31+
```
32+
33+
The available color palettes shipped with Physics Plot are: `physics_plot.colors.ggplot` and `physics_plot.colors.colorblind`, but you can also combine with any other Matplotlib style sheets.
34+
35+
---
36+
37+
Following is an example that demonstrates the different color cycles:
38+
```python linenums="1" hl_lines="7 18 29"
39+
--8<-- "docs/color/color.py"
40+
```
41+
42+
![Default color cycle example](color-default.png)
43+
![ggplot color cycle example](color-ggplot.png)
44+
![colorblind color cycle example](color-colorblind.png)
45+
46+
47+
!!! tip
48+
It is also possible to specify which color among the current color cycle to choose by using the `C0`, `C1`, ... notation.
49+
For instance,
50+
```python linenums="1" hl_lines="10 11"
51+
---8<-- "docs/color/color-CN.py"
52+
```
53+
![CN color example](color-CN.png)
54+
55+
56+
[^ggplot]: The `ggplot` color palette is taken from Matplotlib at [https://github.com/matplotlib/matplotlib/blob/v3.10.7/lib/matplotlib/mpl-data/stylelib/ggplot.mplstyle](https://github.com/matplotlib/matplotlib/blob/v3.10.7/lib/matplotlib/mpl-data/stylelib/ggplot.mplstyle).
57+
58+
[^colorblind]: The `colorblind` palette is taken from seaborn at [https://github.com/mwaskom/seaborn/blob/v0.13/seaborn/palettes.py](https://github.com/mwaskom/seaborn/blob/v0.13/seaborn/palettes.py).

docs/color/color.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
4+
t = np.linspace(0, 10, 100)
5+
y = np.sin(t)
6+
7+
with plt.style.context("physics_plot.pp_base"):
8+
fig, ax = plt.subplots(figsize=(4, 2), constrained_layout=True)
9+
for i, offset in enumerate(np.linspace(0, 2, 5)):
10+
ax.plot(t, y - offset, label=f"C{i}")
11+
ax.set_ylabel(r"$y(t)$")
12+
ax.set_xlabel(r"Time $t$ [s]")
13+
ax.legend(loc="upper right")
14+
fig.suptitle(r"Default color (from Matplotlib)")
15+
fig.savefig("color-default.png")
16+
17+
18+
with plt.style.context(["physics_plot.pp_base", "physics_plot.colors.ggplot"]):
19+
fig, ax = plt.subplots(figsize=(4, 2), constrained_layout=True)
20+
for i, offset in enumerate(np.linspace(0, 2, 5)):
21+
ax.plot(t, y - offset, label=f"C{i}")
22+
ax.set_ylabel(r"$y(t)$")
23+
ax.set_xlabel(r"Time $t$ [s]")
24+
ax.legend(loc="upper right")
25+
fig.suptitle(r"\texttt{ggplot} - color (from Matplotlib)")
26+
fig.savefig("color-ggplot.png")
27+
28+
29+
with plt.style.context(["physics_plot.pp_base", "physics_plot.colors.colorblind"]):
30+
fig, ax = plt.subplots(figsize=(4, 2), constrained_layout=True)
31+
for i, offset in enumerate(np.linspace(0, 2, 5)):
32+
ax.plot(t, y - offset, label=f"C{i}")
33+
ax.set_ylabel(r"$y(t)$")
34+
ax.set_xlabel(r"Time $t$ [s]")
35+
ax.legend(loc="upper right")
36+
fig.suptitle(r"\texttt{colorblind} - color (from Seaborn)")
37+
fig.savefig("color-colorblind.png")

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ with plt.style.context("physics_plot.pp_base"):
4848
Yes! That's it! You are now ready to create beautiful, publication-quality plots with minimal effort using the `physics-plot` style. Enjoy plotting!
4949

5050

51-
[^mplstyle]: Matplotlib style sheets (`.mplstyle` files) are configuration files that define the default appearance of plots created with Matplotlib. They allow users to customize various aspects of plot aesthetics, such as colors, fonts, line styles, and more, by specifying these settings in a structured format. See [Matplotlib Style Sheets Reference](https://matplotlib.org/stable/gallery/style_sheets/style_sheets_reference.html) for more details.
51+
[^mplstyle]: Matplotlib style sheets (`.mplstyle` files) are configuration files that define the default appearance of plots created with Matplotlib. They allow users to customize various aspects of plot aesthetics, such as colors, fonts, line styles, and more, by specifying these settings in a structured format. See [Matplotlib Style Sheets Reference](https://matplotlib.org/stable/gallery/style_sheets/style_sheets_reference.html) for more details. Also, a default template can be found in the [Matplotlib documentation - Customizing Matplotlib with style sheets and rcParams](https://matplotlib.org/stable/users/explain/customizing.html#the-default-matplotlibrc-file) and its [source](https://github.com/matplotlib/matplotlib/blob/main/lib/matplotlib/mpl-data/matplotlibrc). Finally, several built-in `.mplstyle` files can be found [here](https://github.com/matplotlib/matplotlib/tree/main/lib/matplotlib/mpl-data/stylelib).

0 commit comments

Comments
 (0)