Tip

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.

Attention

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

[8]:
%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])
    sims.append(sim)

[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

[9]:
pybamm.dynamic_plot(sims)
[9]:
<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

[10]:
import matplotlib.pyplot as plt

plt.style.available
[10]:
['Solarize_Light2',
 '_classic_test_patch',
 '_mpl-gallery',
 '_mpl-gallery-nogrid',
 'bmh',
 'classic',
 'dark_background',
 'fast',
 'fivethirtyeight',
 'ggplot',
 'grayscale',
 'seaborn-v0_8',
 'seaborn-v0_8-bright',
 'seaborn-v0_8-colorblind',
 'seaborn-v0_8-dark',
 'seaborn-v0_8-dark-palette',
 'seaborn-v0_8-darkgrid',
 'seaborn-v0_8-deep',
 'seaborn-v0_8-muted',
 'seaborn-v0_8-notebook',
 'seaborn-v0_8-paper',
 'seaborn-v0_8-pastel',
 'seaborn-v0_8-poster',
 'seaborn-v0_8-talk',
 'seaborn-v0_8-ticks',
 'seaborn-v0_8-white',
 'seaborn-v0_8-whitegrid',
 'tableau-colorblind10']

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

[11]:
plt.style.use("ggplot")
pybamm.settings.max_words_in_line = 3
pybamm.dynamic_plot(sims)
[11]:
<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

[12]:
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.dynamic_plot(sims)
[12]:
<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.

[13]:
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()
    ax.set_title("")
    ax.set_ylabel(title)

# Remove old legend and add a new one in the bottom
leg = plot.fig.get_children()[-1]
leg.set_visible(False)
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])
../../../../_images/source_examples_notebooks_plotting_customize-quick-plot_17_0.png

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

References#

The relevant papers for this notebook are:

[14]:
pybamm.print_citations()
[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.