Tips and tricks¶
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from cycler import cycler
from mxlpy import plot
Renaming plots arguments¶
The easiest way to rename plot arguments, e.g. for LaTeX
display, is to work on the pandas.DataFrame
directly.
This conveniently offers a rename
method, to which you can supply a dictionary of desired names.
x = np.linspace(0, np.pi, 100)
data = pd.DataFrame(
{
"x1": np.sin(x),
"x2": np.sin(x * 2),
"x3": np.sin(x * 4),
}
)
fig, ax = plot.lines(
data.rename(
columns={
"x1": r"$x_{1}$",
"x2": r"$x_{2}$",
"x3": r"$x_{3}$",
}
)
)
ax.set(xlabel="time / a.u.", ylabel="concentration / a.u.")
plt.show()
Custom plot styling¶
To change the style of plot elements, we recommend using plot.context
, which is a convenience wrapper around plt.rc_context
, see the matplotlib documentation.
That way, our plotting routines can easily be re-used with completely different styling options.
We opted for this way of styling plots so that you can use new features introduced by matplotlib
immediately and don't have to wait for us to support every single update an every single plotting function.
x = np.linspace(0, np.pi, 100)
data = pd.DataFrame(
{
"x1": np.sin(x),
"x2": np.sin(x * 2),
"x3": np.sin(x * 4),
}
)
with plot.context(
colors=["r", "g", "b"],
linewidth=2,
):
fig, ax = plot.lines(data)
ax.set(xlabel="time", ylabel="amplitude")
ax.legend()
plot.show()
Advanced plot styling¶
In case the our convenience arguments are not enough, you can use the entirety of the the matplotlib rc customizing arguments using the rc
keyword argument of plot.context
.
Here, you have to spell out the exact name that matplotlib
expects, e.g. axes.prop_cycle
instead of just colors
in case that was everything you wanted to change.
Hint: watch out for the difference between
cycler + cycler
andcycler * cycler
.
x = np.linspace(0, np.pi, 100)
data = pd.DataFrame(
{
"x1": np.sin(x),
"x2": np.sin(x * 2),
"x3": np.sin(x * 4),
}
)
with plot.context(
rc={"axes.prop_cycle": cycler(color=["r", "b"]) + cycler(linestyle=["-", "--"])},
):
plot.lines(data, ax=plot.one_axes(figsize=(3, 3))[1])
plot.show()
with plot.context(
rc={"axes.prop_cycle": cycler(color=["r", "b"]) * cycler(linestyle=["-", "--"])},
):
plot.lines(data, ax=plot.one_axes(figsize=(3, 3))[1])
plot.show()