Skip to content

Mulitple axes for figures #23

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

Merged
merged 8 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,31 @@ import matplotlib as mpl

# If you only want the default style
mpl.style.use("cosmoplots.default")
# If you want the two column style, combine it with the default. Setting it after is
# important, since values from the default is overridden.
mpl.style.use(["cosmoplots.default", "cosmoplots.two_columns"])
```

### Muliple subfigures
To make a figure with multiple rows or columns, use `cosmoplots.figure_multiple_rows_columns`:
```python
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

import cosmoplots

mpl.style.use(["cosmoplots.default"])
rows = 1
columns = 2

fig, ax = cosmoplots.figure_multiple_rows_columns(rows, columns)
a = np.linspace(-3,3,100)
for i in range(rows*columns):
ax[i].set_xlabel("X Axis")
ax[i].set_ylabel("Y Axis")
ax[i].plot(i*a)
plt.show()
```
![multifig](./assets/multifig.png)

## `change_log_axis_base`

```python
Expand Down Expand Up @@ -203,3 +223,4 @@ cosmoplots.Combine().help()
```

![concat](./assets/concat.png)

Binary file added 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.
34 changes: 34 additions & 0 deletions cosmoplots/axes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Module for modifying the axis properties of plots."""

from typing import List, Tuple, Union
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import ticker
Expand Down Expand Up @@ -88,3 +89,36 @@ def change_log_axis_base(
)
)
return axes

def figure_multiple_rows_columns(rows: int, columns: int):
"""Returns a figure with axes which is appropriate for (rows, columns) subfigures.

Parameters
----------
rows : int
The number of rows in the figure
columns : int
The number of columns in the figure

Returns
-------
plt.Figure
The figure object
plt.Axes
A list of all the axes objects owned by the figure
"""
# Returns a figure with axes which is appropriate for
# (rows, columns) subfigures
fig = plt.figure(figsize = (columns*3.37, rows*2.08277))
axes = [None]*rows*columns
for c in range(columns):
for r in range(rows):
index = r*columns + c
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[index] = fig.add_axes([left, bottom, width, height])

return fig, axes

Loading