Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subfiglabels #30

Merged
merged 5 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ plt.style.use(["cosmoplots.default"])
```

### Muliple subfigures
To make a figure with multiple rows or columns, use `cosmoplots.figure_multiple_rows_columns`:
To make a figure with multiple rows or columns, use `cosmoplots.figure_multiple_rows_columns`.
By default, the labels are $\mathrm{(a)}$, $\mathrm{(b)}$, $\mathrm{(c)}$, ..., but they may be replaced using the `labels` argument.
```python
import matplotlib.pyplot as plt
import cosmoplots
Expand Down
Binary file modified assets/multifig.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 13 additions & 2 deletions cosmoplots/axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ def change_log_axis_base(
)
return axes

def figure_multiple_rows_columns(rows: int, columns: int) -> Tuple[Figure, List[Axes]]:
def figure_multiple_rows_columns(rows: int, columns: int,
labels: List[str] | None = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The | type specifier was added in 3.10, so since cosmoplots supports 3.8 it should probably be Union[List[str], None].

label_x: float = -0.2, label_y: float = 0.95,
**kwargs) -> Tuple[Figure, List[Axes]]:
"""Returns a figure with axes which is appropriate for (rows, columns) subfigures.

Parameters
Expand All @@ -100,7 +103,13 @@ def figure_multiple_rows_columns(rows: int, columns: int) -> Tuple[Figure, List[
The number of rows in the figure
columns : int
The number of columns in the figure

labels : List[str] | None
The labels to be applied to each subfigure. Defaults to (a), (b), (c), ...
label_x and label_y: float
x- and y- positions of the labels relative to each Axes object.
**kwargs:
Additional keyword arguments to be passed to Axes.text.

Returns
-------
plt.Figure
Expand All @@ -110,13 +119,15 @@ def figure_multiple_rows_columns(rows: int, columns: int) -> Tuple[Figure, List[
"""
fig = plt.figure(figsize = (columns*3.37, rows*2.08277))
axes = []
labels = labels or [r"$\mathrm{{({})}}$".format(chr(97+l)) for l in range(rows*columns)]
for c in range(columns):
for r in range(rows):
left = (0.2)/columns + c/columns
bottom = (0.2)/rows + (rows-1-r)/rows # Start at the top
width = 0.75/columns
height = 0.75/rows
axes.append(fig.add_axes((left, bottom, width, height)))
axes[-1].text(label_x, label_y, labels[columns*r+c], transform=axes[-1].transAxes, **kwargs)

return fig, axes

Loading