cosmoplots
Routines to get a sane default configuration for production quality plots. Used by complex systems modelling group at UiT.
Installation
The package is published to PyPI and can be installed with
pip install cosmoplots
If you want the development version you must first clone the repo to your local machine, then install the project and its dependencies with poetry:
git clone https://github.com/uit-cosmo/cosmoplots.git
cd cosmoplots
poetry install
LaTeX and Latin Modern dependency
The default style uses LaTeX to render text and equations. The default font is Latin Modern.
text.usetex : True
font.family : "Latin Modern"
Installations of TeX Live should include Latin Modern, but it may be manually downloaded from here.
Usage
Set your rcparams before plotting in your code, for example:
import matplotlib.pyplot as plt
import cosmoplots
# If you only want the default style
plt.style.use(["cosmoplots.default"])
Muliple subfigures
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.
import matplotlib.pyplot as plt
import cosmoplots
plt.style.use(["cosmoplots.default"])
import numpy as np
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.savefig("assets/multifig.png")
plt.show()
Position of the y-axis
By default, the x-coordinates of the y-axis labels are aligned with the subfigure labels, but the y-axis label position can be adjusted using
ax[i].yaxis.set_label_coords(x_coordinate, y_coordinate)
change_log_axis_base
import matplotlib.pyplot as plt
import cosmoplots
plt.style.use(["cosmoplots.default"])
import numpy as np
a = np.exp(np.linspace(-3, 1, 100))
# Plotting
fig = plt.figure()
ax1 = plt.gca()
ax1.set_xlabel("X Axis")
ax1.set_ylabel("Y Axis")
base = 2 # Default is 10, but 2 works equally well
# Do plotting ...
ax1.semilogx(a)
# It is recommended to call the change_log_axis_base function after doing all the
# plotting. By default, it will try to infer the scaling used for the axis and only
# adjust accordingly.
cosmoplots.change_log_axis_base(ax1, base=base)
# Plotting
fig = plt.figure()
ax2 = plt.gca()
ax2.set_xlabel("X Axis")
ax2.set_ylabel("Y Axis")
base = 2 # Default is 10, but 2 works equally well
cosmoplots.change_log_axis_base(ax2, "x", base=base)
# Do plotting ...
# If you use "plot", the change_log_axis_base can be called at the top (along with add_axes
# etc.), but using loglog, semilogx, semilogy will re-set, and the change_log_axis_base
# function must be called again.
ax2.plot(a)
plt.show()
matplotlib vs. cosmoplots defaults
import matplotlib.pyplot as plt
import cosmoplots
import numpy as np
def plot() -> None:
a = np.exp(np.linspace(-3, 5, 100))
fig = plt.figure()
ax = fig.add_subplot()
ax.set_xlabel("X Axis")
ax.set_ylabel("Y Axis")
ax.semilogy(a)
# Matplotlib ------------------------------------------------------------------------- #
with plt.style.context("default"):
plot()
# plt.savefig("assets/matplotlib.png")
plt.show()
# Cosmoplots ------------------------------------------------------------------------- #
with plt.style.context("cosmoplots.default"):
plot()
# plt.savefig("assets/cosmoplots.png")
plt.show()
matplotlib |
cosmoplots |
|---|---|
generate_hex_colors
This function generates the hex numbers for the colours extracted from a matplotlib colour map based on the number of points of interest.
The colors change gradually from bright to dark or vice versa.
import matplotlib.pyplot as plt
import cosmoplots
plt.style.use(["cosmoplots.default"])
color_list = cosmoplots.generate_hex_colors(5, 'viridis', show_swatch=True, ascending=True)
# plt.savefig("./assets/hex_colors.png")
# Print color_list to retrieve the hex numbers
print(color_list) #['#fde725', '#5ec962', '#21918c', '#3b528b', '#440154']
fig = plt.figure()
ax = plt.gca()
for i, color in enumerate(color_list):
ax.plot([1,2],[i,i+1], c = color)
# plt.savefig("./assets/hex_colors_example.png")
plt.show()
hex_colors.png |
hex_colors_example.png |
|---|---|
combine
Sometimes, plots might be related and better placed as subfigures in a larger figure. If
combining the plots using the subfigure environment in latex or similar is not an
option, this is easily done with imagemagick in a
systematic way.
The Combine class within the concat module implements such procedures, and is also
conveniently available from the combine function in cosmoplots.
An example is shown below. Also see the tests directory for more examples.
A help method that prints the imagemagick commands that are used under the hood is
also available.
import matplotlib.pyplot as plt
import cosmoplots
plt.style.use("cosmoplots.default")
import numpy as np
def plot(i) -> None:
"""Create a simple plot."""
a = np.exp(np.linspace(-3, 5, 100))
fig = plt.figure()
ax = fig.add_subplot()
ax.set_xlabel("X Axis")
ax.set_ylabel("Y Axis")
ax.semilogy(a)
plt.savefig(f"./assets/{i}.png")
plt.close(fig)
plot(1)
plot(2)
plot(3)
plot(4)
plot(5)
plot(6)
plot(7)
plot(8)
plot(9)
plot(10)
# See `magick -list font` for all available fonts.
figs = [f"./assets/{i}.png" for i in range(1, 11)]
cosmoplots.combine(*figs).using(
font="JetBrainsMonoNL-NFM-Medium",
fontsize=30,
gravity="southeast",
pos=(100, 200),
color="green",
).in_grid(w=3, h=4).with_labels( # Specifying labels is optional
"one", "four", "three", "two", "eight", "six", "seven", "five", "nine", "ten"
).save("./assets/concat.png")
# Note that cosmoplots.combine() == cosmoplots.Combine().combine()
cosmoplots.combine().help()
# Or equivalently
cosmoplots.Combine().help()
Metadata
Release files for cosmoplots 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| cosmoplots-0.5.0.tar.gz | 14.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| cosmoplots-0.5.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 29.5 kB
Release files / cosmoplots-0.5.0.tar.gz
| Download URL | cosmoplots-0.5.0.tar.gz |
|---|---|
| Size | 14.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3842a0cd5ff187db924f7a3ad7516f4bab28fbc5cc549764112db95c6f00748b
|
|
BLAKE2b-256 checksum How to use checksums |
0cc500ef1c0fe7de456636effba91c7332fe5cfb5e141c64062599eef44bdcc7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.9.21
|
Release files / cosmoplots-0.5.0-py3-none-any.whl
| Download URL | cosmoplots-0.5.0-py3-none-any.whl |
|---|---|
| Size | 15.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
349b9b9b8de2923988bd415a147cbaf0d1cb339f644fdffcf593986e936d1e90
|
|
BLAKE2b-256 checksum How to use checksums |
fb256f7b81b49231fb642c33c57b5371054b96a0c09de32f05ac2172bf6415dc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.9.21
|
Security for cosmoplots 0.5.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.