Skip to main content

A collection of tools for use in the Institute of Chemical Engineering at Ulm University.

Project description

Package ICIW-Plots

Installation

Install the package by running the following command:

pip install iciw-plots

or

pip install iciw-plots -U

Usage

Although I show the usage of the package in the context of a Jupyter notebook, the package can be used in any Python environment. Also, the style has to be used only once, at the beginning of the plotting file or notebook. In this document I write it in nearly every box just to show th use. This is not necessary!

Use of the default style

The default style defines convenient presets for the following settings:

  • Font size
  • Font family
  • Line width
  • Marker size
  • Color palette
  • Grid style
  • Legend style
import matplotlib.pyplot as plt
import numpy as np

plt.style.use("ICIWstyle")

x = np.linspace(0, 2 * np.pi, 100)

for i in range(7):
    plt.plot(x, np.sin(x + (i * (4 * np.pi / 7))), label=f"Line {i}")

plt.legend()
plt.show()

png

In addition to the default style, the package also provides different styles to be loaded additionally. These can be used to

  • enable rendering of texts in LaTeX format by using ICIWlatex
  • disable the background when exporting the plot by using ICIWnobg

The latex style automatically imports the LaTex packages amsmath, amssymb, siunitx and mchem enabling the use of all latex math commands, SI unit rendering and chemical formulas.

import matplotlib.pyplot as plt
plt.style.use(["ICIWstyle", "ICIWlatex"])

x = np.linspace(0, 2 * np.pi, 100)

for i in range(7):
    plt.plot(x, np.sin(x + (i * (4 * np.pi / 7))))


plt.xlabel(r"$t$ / \unit{\second}")
plt.ylabel(r"$c$ / \unit{\mole\per\metre\cubed}")
plt.legend(
    [
        r"\ce{A}",
        r"\ce{B}",
        r"\ce{AB}",
        r"\ce{AB2}",
        r"\ce{A2Be}",
        r"\ce{C2+}",
    ]
)

plt.show()

png

Sizes

The package provides default size options for the following publishers:

  • ACS
  • Elsevier

To make conversion between default matplotlib units (inches) and european units (cm) easier, the package provides the following conversion factors:

  • cm2inch
  • mm2inch
from ICIW_Plots.figures import Elsevier_Sizes, ACS_Sizes
from ICIW_Plots import cm2inch

print(
    f"Width for Elsevier default single column wide plots:\n{Elsevier_Sizes.single_column}"
)
print(
    f"Width for Elsevier default double column wide plots:\n{Elsevier_Sizes.double_column}"
)
print(
    f"width for Elsevier defaultone and a half column wide plots:\n{Elsevier_Sizes.threehalf_column}"
)

print(f"Width for ACS default single column wide plots:\n{ACS_Sizes.single_column}")
print(f"Width for ACS default double column wide plots:\n{ACS_Sizes.double_column}")

fig = plt.figure(figsize=(Elsevier_Sizes.single_column["in"], 5 * cm2inch))
ax = fig.add_axes([0, 0, 1, 1])
plt.show()
Width for Elsevier default single column wide plots:
{'mm': 90, 'in': 3.54, 'cm': 9}
Width for Elsevier default double column wide plots:
{'mm': 190, 'in': 7.48, 'cm': 19}
width for Elsevier defaultone and a half column wide plots:
{'mm': 140, 'in': 5.51, 'cm': 14}
Width for ACS default single column wide plots:
{'mm': 82.55, 'in': 3.25, 'cm': 8.255}
Width for ACS default double column wide plots:
{'mm': 177.8, 'in': 7, 'cm': 17.78}

png

More Sizes and Axes

Oftentimes, I like to create square axis with a fixed width in the middle of a figure with fixed width. matplotlib makes this hard for the user. ICIW-Plots provides functions make_square_ax and make_rect_ax that do this for you.

plt.style.use("ICIWstyle")
from ICIW_Plots.figures import Elsevier_Sizes
from ICIW_Plots import make_square_ax

fig = plt.figure(figsize=(Elsevier_Sizes.single_column["in"], 7 * cm2inch))
ax = make_square_ax(
    fig,
    ax_width=5 * cm2inch,
)
ax.plot(x, np.sin(x))
plt.show()
c:\ProgramData\Anaconda3\envs\ML3\Lib\site-packages\ICIW_Plots\layout.py:74: UserWarning: Unscientific behavior. No xlabel provided.
  warnings.warn("Unscientific behavior. No xlabel provided.")
c:\ProgramData\Anaconda3\envs\ML3\Lib\site-packages\ICIW_Plots\layout.py:79: UserWarning: Unscientific behavior. No ylabel provided.
  warnings.warn("Unscientific behavior. No ylabel provided.")

png

As you can see, you even get a warning when you misbehave. Both functions take some arguments you can inspect via the mouseover in your IDE. Here is just an example of what you can do although it is unreasonable to do so:

plt.style.use("ICIWstyle")
from ICIW_Plots.figures import Elsevier_Sizes
from ICIW_Plots import make_square_ax

fig = plt.figure(figsize=(Elsevier_Sizes.single_column["in"], 7 * cm2inch))
ax = make_square_ax(
    fig,
    ax_width=5 * cm2inch,
    # left_h=0.2,  # These arguments control the spacing of the axis
    # bottom_v=0.2, # not supplying them wil place the axes in the middle of the figure
    xlabel=r"$t$ / \unit{\second}",
    ylabel=r"$U$ / \unit{\volt}",
    title="This is a title",
    xscale="log",
)
ax.plot(x, np.sin(x))
plt.show()

png

import matplotlib.pyplot as plt

plt.style.use("ICIWstyle")

from ICIW_Plots.figures import Elsevier_Sizes

from ICIW_Plots import make_rect_ax


fig = plt.figure(figsize=(Elsevier_Sizes.double_column["in"], 7 * cm2inch))

ax = make_rect_ax(
    fig,
    ax_width=7.3 * cm2inch,
    ax_height=5 * cm2inch,
    # left_h=0.2,  # These arguments control the spacing of the axis
    # bottom_v=0.2, # not supplying them wil place the axes in the middle of the figure
    xlabel=r"$t$ / \unit{\second}",
    ylabel=r"$U$ / \unit{\volt}",
    title="This is a title",
    xscale="log",

)
ax.plot(x, np.sin(x))
plt.show()

png

In jupyter notebooks the output appears cut to the "appropriate" size. In a python script, you will see the full figure with all the sizes and positions spaced correctly.

Colors

ICIW-Plots defines the university colors.

import matplotlib.pyplot as plt
import ICIW_Plots.colors as ICIWcolors
plt.style.use("ICIWstyle")

fig, ax = plt.subplots()

ax.plot(x, np.sin(x), color=ICIWcolors.CRIMSON)
ax.plot(x, np.cos(x), color=ICIWcolors.CERULEAN)
ax.plot(x, np.log(x + 0.1), color=ICIWcolors.KELLYGREEN)
ax.plot(x, np.tanh(x), color=ICIWcolors.FLAME)
ax.plot(x, np.arcsinh(x), color=ICIWcolors.DRAB)

plt.legend(["crimson", "cerulean", "kellygreen", "flame", "drab"])

plt.show()

png

All colors are available as colorbars as well. Here is just an example for the cerulean colorbar:

import matplotlib.pyplot as plt
import ICIW_Plots.colors as ICIWcolors

plt.style.use("ICIWstyle")

N = 100
x = np.linspace(-3.0, 3.0, N)
y = np.linspace(-2.0, 2.0, N)

X, Y = np.meshgrid(x, y)
Z1 = -(X**2) - Y**2
Z2 = -((X * 10) ** 2) - (Y * 10) ** 2
z = Z1 + 50 * Z2

fig, ax = plt.subplots()

cs = ax.contourf(X, Y, z, cmap=ICIWcolors.cerulean_cm, levels=10)
cbar = fig.colorbar(cs)

plt.show()

png

Cyclers

The package defines some functionality to do your own cyclers. Supported are:

  • color cyclers from colormaps
    • all default matplotlib colormaps by name
    • all custom colormaps from ICIW-Plots by reference
  • line style cyclers
    • all default linestyles by abbreviation (-,--,.-,:)
    • every custom linestyle by a dash tuple (e.g., (0,(3,10,1,15)))
  • marker cyclers
    • all predefined markers by abbreviation (o,s,^,v,and so on)
    • every custom marker by a marker reference

Custom color cyclers take a colormap and sample num_plots points from them equidistantly spaced. start and stop are used to prevent very light or very dark colors from being used. The cycler is then added as the axes prop_cycle.

import matplotlib.pyplot as plt
import ICIW_Plots.cyclers as ICIW_cyclers

fig, ax = plt.subplots()
x = np.linspace(-2 * np.pi, 2 * np.pi)
my_green_cycler = ICIW_cyclers.ICIW_colormap_cycler("Greens", 7, start=0.2)
ax.set_prop_cycle(my_green_cycler)
for i in range(7):
    ax.plot(x, np.sin(x + (i * (4 * np.pi / 7))))
plt.show()

png

import matplotlib.pyplot as plt
import ICIW_Plots.cyclers as ICIW_cyclers

fig, ax = plt.subplots()

my_blue_cycler = ICIW_cyclers.ICIW_colormap_cycler(
    ICIWcolors.cerulean_cm,
    7,
    start=0.1,
)
ax.set_prop_cycle(my_blue_cycler)
for i in range(7):
    ax.plot(x, np.sin(x + (i * (4 * np.pi / 7))))
plt.show()

png

custom linestyle cyclers take a list of linestyles and a number of plots to cycle through. The cycler is then added as the axes prop_cycle.

my_linestyle_cycler = ICIW_cyclers.ICIW_linestyle_cycler(3)

fig, ax = plt.subplots()

ax.set_prop_cycle(my_linestyle_cycler)
for j in range(3):
    ax.plot(x, x + j * 5)

png

Note, that all lines have the same color, since matplotlib by default cycles through its default cycler containing the colors. By overwriting the default cycler by our linestyle cycler, all lines will have the same color.

We can combine different cyclers together by either

  • inner product (pairwise combinations)
  • outer product (unique combinations)
fig, axs = plt.subplots(1, 2)

custom_c_cycler = ICIW_cyclers.ICIW_colormap_cycler("Greens", 3, start=0.5)
custom_l_cycler = ICIW_cyclers.ICIW_linestyle_cycler(3)

axs[0].set_title("Inner Product")
# this combination gives 3 different combinations of color and line style
# linestyle 1 and color 1, linestyle 2 and color 2, linestyle 3 and color 3
axs[0].set_prop_cycle(custom_c_cycler + custom_l_cycler)
for i in range(3):
    axs[0].plot(x, np.sin(x + (i * (4 * np.pi / 5))))

axs[1].set_title("Outer Product")
# this combination gives 9 different combinations of color and line style
# linestyle 1 and color 1, linestyle 2 and color 1, linestyle 3 and color 1 and so on
axs[1].set_prop_cycle(custom_c_cycler * custom_l_cycler)
for i in range(3):
    for j in range(3):
        axs[1].plot(x, i * x + j * 5)

png

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

iciw_plots-1.0.28.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

ICIW_Plots-1.0.28-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file iciw_plots-1.0.28.tar.gz.

File metadata

  • Download URL: iciw_plots-1.0.28.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/7.0.1 pkginfo/1.9.6 requests/2.31.0 requests-toolbelt/1.0.0 tqdm/4.66.2 CPython/3.10.4

File hashes

Hashes for iciw_plots-1.0.28.tar.gz
Algorithm Hash digest
SHA256 e2e49d367c08729f86db760a538fdce73a3853f067946de0fb817f652ee61c08
MD5 2ca63eb6788ca82f016cb32396258433
BLAKE2b-256 138935ef37e358c8c6e6ca48aa69d7d27d2eb876e73149f71254750d47c6b6f9

See more details on using hashes here.

File details

Details for the file ICIW_Plots-1.0.28-py3-none-any.whl.

File metadata

  • Download URL: ICIW_Plots-1.0.28-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/7.0.1 pkginfo/1.9.6 requests/2.31.0 requests-toolbelt/1.0.0 tqdm/4.66.2 CPython/3.10.4

File hashes

Hashes for ICIW_Plots-1.0.28-py3-none-any.whl
Algorithm Hash digest
SHA256 858115cf8da3d30be206713552136dc27e01171acb40fe8aa1534cad98bdd415
MD5 9995e9b4371d1abb37ac480226e0f0e3
BLAKE2b-256 2ca1455e1aa08587443efcbe4c10fb863045fdb575bc07280fee5ca2824e8be0

See more details on using hashes here.

Supported by

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