Skip to content

Commit

Permalink
Merge pull request #23 from uit-cosmo/multiple_figures
Browse files Browse the repository at this point in the history
Support for figures with multiple axes
  • Loading branch information
audunth authored Apr 29, 2024
2 parents 3c02c3b + 78ef08c commit 8baab5b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
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.
32 changes: 32 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,34 @@ 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
"""
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

0 comments on commit 8baab5b

Please sign in to comment.