Skip to content

Commit 0693028

Browse files
committed
feat: update mpl-size documentation and examples; add DPI explanation
1 parent 6906d2f commit 0693028

4 files changed

Lines changed: 29 additions & 11 deletions

File tree

docs/mpl/mpl-size-lg.png

61.2 KB
Loading

docs/mpl/mpl-size-sm.png

52.2 KB
Loading

docs/mpl/mpl-size.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## Decreasing Figure Size to Increase Font Size
2+
13
The first and the most important trick when plotting with Matplotlib is to set the figure size appropriately.
24
If you want to increase the font size and line width everywhere in the figure, the easiest way is to **decrease the figure size**!
35

@@ -11,4 +13,22 @@ If you want to increase the font size and line width everywhere in the figure, t
1113
```
1214

1315
![small size](mpl-size-sm.png)
14-
![large size](mpl-size-lg.png)
16+
![large size](mpl-size-lg.png)
17+
18+
19+
## DPI
20+
21+
The native figure size unit in Matplotlib is inches as documented [here](https://matplotlib.org/stable/gallery/subplots_axes_and_figures/figure_size_units.html).
22+
When we intentionally set a small figure size (e.g., 4x2 inches), we can increase the DPI (dots per inch) to get a larger (higher resolution / size in pixels) image without changing the relative sizes of text elements.
23+
24+
The DPI can be set when creating the figure using the `dpi` argument:
25+
26+
```python
27+
import matplotlib.pyplot as plt
28+
fig, ax = plt.subplots(figsize=(4, 2), dpi=600)
29+
```
30+
or specified when saving the figure using the `dpi` argument of `savefig`:
31+
32+
```python
33+
fig.savefig("figure.png", dpi=600)
34+
```

docs/mpl/mpl-size.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,21 @@
33

44
plt.style.use("physics_plot.pp_base")
55

6+
x = np.linspace(0, 10, 1000)
7+
y = np.sin(5 * x)
8+
69
fig, ax = plt.subplots(figsize=(4, 2), constrained_layout=True)
7-
x = np.linspace(0, 10, 100)
8-
y = np.sin(x)
9-
ax.plot(x, y, label="Sine Wave")
10+
ax.plot(x, y, label=r"$y = \sin(5x)$")
1011
ax.set_ylabel(r"$y(x)$")
1112
ax.set_xlabel(r"$x$")
1213
ax.legend(loc="upper right")
13-
fig.suptitle(r"\texttt{figsize=(4, 2)}")
14+
fig.suptitle(r"Small Figure Size (4x2 inches), i.e., \texttt{figsize=(4, 2)}")
1415
fig.savefig("mpl-size-sm.png")
1516

16-
17-
fig, ax = plt.subplots(figsize=(10, 5), constrained_layout=True)
18-
x = np.linspace(0, 10, 100)
19-
y = np.sin(x)
20-
ax.plot(x, y, label="Sine Wave")
17+
fig, ax = plt.subplots(figsize=(8, 4), constrained_layout=True)
18+
ax.plot(x, y, label=r"$y = \sin(5x)$")
2119
ax.set_ylabel(r"$y(x)$")
2220
ax.set_xlabel(r"$x$")
2321
ax.legend(loc="upper right")
24-
fig.suptitle(r"\texttt{figsize=(10, 5)}")
22+
fig.suptitle(r"Large Figure Size (8x4 inches), i.e., \texttt{figsize=(8, 4)}")
2523
fig.savefig("mpl-size-lg.png")

0 commit comments

Comments
 (0)