An interactive online version of this notebook is available, which can be accessed via Open this notebook in Google Colab

Alternatively, you may download this notebook and run it offline.


You are viewing this notebook on the latest version of the documentation, where these notebooks may not be compatible with the stable release of PyBaMM since they can contain features that are not yet released. We recommend viewing these notebooks from the stable version of the documentation. To install the latest version of PyBaMM that is compatible with the latest notebooks, build PyBaMM from source.

Customizing QuickPlot#

This notebook shows how to customize PyBaMM’s QuickPlot, using matplotlib’s style sheets and rcParams

First we define and solve the models

%pip install "pybamm[plot,cite]" -q    # install PyBaMM if it is not installed
import pybamm

models = [pybamm.lithium_ion.SPM(), pybamm.lithium_ion.SPMe(), pybamm.lithium_ion.DFN()]
sims = []
for model in models:
    sim = pybamm.Simulation(model)
    sim.solve([0, 3600])

[notice] A new release of pip is available: 23.0.1 -> 23.1.2
[notice] To update, run: pip install --upgrade pip
Note: you may need to restart the kernel to use updated packages.

Call the default plots

<pybamm.plotting.quick_plot.QuickPlot at 0x28810b850>

Using style sheets#

The easiest way to customize style is to use one of matplotlib’s available style sheets

import matplotlib.pyplot as plt


For example we can use the ggplot style from R. In this case, the title fonts are quite large, so we reduce the number of words in a title before a line break

pybamm.settings.max_words_in_line = 3
<pybamm.plotting.quick_plot.QuickPlot at 0x289590050>

Another good set of style sheets for scientific plots is available by pip installing the SciencePlots package

Further customization using rcParams#

Sometimes we want further customization of a style, without needing to edit the style sheets. For example, we can update the font sizes and plot again.

To change the line colors, we use cycler

import matplotlib as mpl
from cycler import cycler

mpl.rcParams["axes.labelsize"] = 12
mpl.rcParams["axes.titlesize"] = 12
mpl.rcParams["xtick.labelsize"] = 12
mpl.rcParams["ytick.labelsize"] = 12
mpl.rcParams["legend.fontsize"] = 12
mpl.rcParams["axes.prop_cycle"] = cycler("color", ["k", "g", "c"])
<pybamm.plotting.quick_plot.QuickPlot at 0x282c16290>

Very fine customization#

Some customization of the QuickPlot object is possible by passing arguments - see the docs for details

We can also further control the plot by calling plot.fig after the figure has been created, and editing the matplotlib objects. For example, here we move the titles to the ylabel, and move the legend.

pybamm.settings.max_words_in_line = 4

plot = pybamm.QuickPlot(sims, figsize=(14, 7))
plot.plot(0.5)  # time in hours

# Move title to ylabel
for ax in plot.fig.axes:
    title = ax.get_title()

# Remove old legend and add a new one in the bottom
leg = plot.fig.get_children()[-1]
plot.fig.legend(plot.labels, loc="lower center", ncol=len(plot.labels), fontsize=11)

# Adjust layout
plot.gridspec.tight_layout(plot.fig, rect=[0, 0.04, 1, 1])

The figure can then be saved using plot.fig.savefig


The relevant papers for this notebook are:

[1] Joel A. E. Andersson, Joris Gillis, Greg Horn, James B. Rawlings, and Moritz Diehl. CasADi – A software framework for nonlinear optimization and optimal control. Mathematical Programming Computation, 11(1):1–36, 2019. doi:10.1007/s12532-018-0139-4.
[2] Marc Doyle, Thomas F. Fuller, and John Newman. Modeling of galvanostatic charge and discharge of the lithium/polymer/insertion cell. Journal of the Electrochemical society, 140(6):1526–1533, 1993. doi:10.1149/1.2221597.
[3] Charles R. Harris, K. Jarrod Millman, Stéfan J. van der Walt, Ralf Gommers, Pauli Virtanen, David Cournapeau, Eric Wieser, Julian Taylor, Sebastian Berg, Nathaniel J. Smith, and others. Array programming with NumPy. Nature, 585(7825):357–362, 2020. doi:10.1038/s41586-020-2649-2.
[4] Scott G. Marquis, Valentin Sulzer, Robert Timms, Colin P. Please, and S. Jon Chapman. An asymptotic derivation of a single particle model with electrolyte. Journal of The Electrochemical Society, 166(15):A3693–A3706, 2019. doi:10.1149/2.0341915jes.
[5] Valentin Sulzer, Scott G. Marquis, Robert Timms, Martin Robinson, and S. Jon Chapman. Python Battery Mathematical Modelling (PyBaMM). Journal of Open Research Software, 9(1):14, 2021. doi:10.5334/jors.309.