https://github.com/grfrederic/subplots-from-axsize
matplotlib subplots() but axsize= instead of figsize=
https://github.com/grfrederic/subplots-from-axsize
Last synced: 9 months ago
JSON representation
matplotlib subplots() but axsize= instead of figsize=
- Host: GitHub
- URL: https://github.com/grfrederic/subplots-from-axsize
- Owner: grfrederic
- License: bsd-3-clause
- Created: 2023-06-15T13:41:33.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-10-13T13:05:05.000Z (over 2 years ago)
- Last Synced: 2025-08-30T06:45:35.453Z (9 months ago)
- Language: Python
- Size: 24.4 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## subplots_from_axsize
This package provides a single function, `subplots_from_axsize()`, which is based on matplotlib's `subplots()` and `adjust_subplots()` functions, but:
* `figsize` is replaced by `axsize`
* `left`, `bottom`, `right`, `top` use inches instead of fractions
* `wspace`, `hspace` use inches instead of fractions
* `axsize`, `wspace`, `hspace` can take lists as arguments (see example #2 below)
## rationale
Specifying sizes directly makes it easier to achieve consistent ax sizes across figures. Suppose you're plotting various time series data and you would like consistent inches / week on your x axis. Or you carefully crafted your figures but suddenly you need a little extra space for you labels. Or you would want to move the axes a little bit apart and *not* rescale everything else in the whole world simultaneously.
## getting started
The package is available on [PyPi](https://pypi.org/project/subplots-from-axsize/).
## example #1
```
import matplotlib.pyplot as plt
from subplots_from_axsize import subplots_from_axsize
fig, ax = subplots_from_axsize(
axsize=(4, 3),
left=0.9, bottom=0.5, top=0.3, right=0.2,
)
ax.set_xlabel('x label')
ax.set_ylabel('y label\nbut much\nmuch longer')
ax.set_title('important!')
fig.patch.set_facecolor('#ffbbff')
```

Since the default dpi in matplotlib is 100, the axis is exactly 400 by 300 pixels. The left margin, for example, is 0.9 × 100 = 90 pixels. Changing the margins does not change the axis size (you have my promise).
## example #2
Additionally, `subplots_from_axsize` makes it easy to create multiple axes.
```
import matplotlib.pyplot as plt
from subplots_from_axsize import subplots_from_axsize
fig, axs = subplots_from_axsize(
axsize=([1, 3], 1),
hspace=[0.5, 1.0],
left=0.4, bottom=0.3,
)
fig.patch.set_facecolor('#ffbbff')
```

The number of columns (or rows) is inferred automatically based on three arguments: `nrows` (if given), length of `axsize[0]` (if it is a list), length of `wspace` *+ 1* (if it is a list). If none of the conditions is met, the default is 1. If they disagree you get an error (xor create an Issue).