Skip to main content

Routines to get a sane default configuration for production quality plots.

Project description

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

Usage

Set your rcparams before plotting in your code, for example:

import cosmoplots
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"])

change_log_axis_base

import matplotlib.pyplot as plt
import numpy as np
import cosmoplots
import matplotlib as mpl

# Setup
mpl.style.use("cosmoplots.default")
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 cosmoplots
import matplotlib as mpl
import matplotlib.pyplot as plt
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 mpl.style.context("default"):
    plot()
    # plt.savefig("assets/matplotlib.png")
    plt.show()

# Cosmoplots ------------------------------------------------------------------------- #
with mpl.style.context("cosmoplots.default"):
    plot()
    # plt.savefig("assets/cosmoplots.png")
    plt.show()
matplotlib cosmoplots
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

axes_size = cosmoplots.set_rcparams_dynamo(plt.rcParams, num_cols=1, ls="thin")


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 = fig.add_axes(axes_size)
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
colors colors

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cosmoplots-0.3.1.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cosmoplots-0.3.1-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file cosmoplots-0.3.1.tar.gz.

File metadata

  • Download URL: cosmoplots-0.3.1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for cosmoplots-0.3.1.tar.gz
Algorithm Hash digest
SHA256 901894cf0e6fbe5a893d6849708b5b5089383fc2c563c2bc8e422293af4684a2
MD5 058993715622cde6364901b8cbd04a34
BLAKE2b-256 b7428b9229577cc68c08d2e2e7e5d0119689ae3d610fc64d6a39efdd2807580d

See more details on using hashes here.

File details

Details for the file cosmoplots-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: cosmoplots-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for cosmoplots-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 49c34ffdf0c04a46c180dfe385d2deeaeeae92f0f1f5df02cac7f6de24256c30
MD5 735d3e0d67e7af65e0d99baf57d1814d
BLAKE2b-256 949f90ee09a97cc4b7340b7e3717eaf8365be52b1bfa57530c9ebc64c8485f04

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page