A collection of tools for use in the Institute of Chemical Engineering at Ulm University.
Project description
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()
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()
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}
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.")
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()
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()
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()
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()
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))
)
- all default linestyles by abbreviation (
- marker cyclers
- all predefined markers by abbreviation (
o
,s
,^
,v
,and so on) - every custom marker by a marker reference
- all predefined markers by abbreviation (
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()
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()
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)
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)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file iciw_plots-1.0.38.tar.gz
.
File metadata
- Download URL: iciw_plots-1.0.38.tar.gz
- Upload date:
- Size: 25.8 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a18789c95becf0460073259fa199ca2835d8dee9a1362d40e43832b1ad53be3 |
|
MD5 | aa19e98248f301e425ce1f5f1cfe020d |
|
BLAKE2b-256 | 232877710d519f9b93fa0d94c947f3b6ba9d9fff80c0f037530efd51a1eb3be9 |
File details
Details for the file ICIW_Plots-1.0.38-py3-none-any.whl
.
File metadata
- Download URL: ICIW_Plots-1.0.38-py3-none-any.whl
- Upload date:
- Size: 28.3 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e3cb8e15e832fa5842e2e2766edd6d3ea151c179262ebaa851bb9a880a357d0 |
|
MD5 | 61f910b9205ce41aa8506ca510d18c56 |
|
BLAKE2b-256 | 5946733834600e147f6dd81f1891ebd9708b2ed7a019046346716e90c4736e6e |