Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 1413 plotting #1419

Merged
merged 13 commits into from
Mar 8, 2021
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Features

- Added `plot_voltage_components` to easily plot the component overpotentials that make up the voltage ([#1419](https://github.com/pybamm-team/PyBaMM/pull/1419))
- Made `QuickPlot` more customizable and added an example ([#1419](https://github.com/pybamm-team/PyBaMM/pull/1419))
- `Solution` objects can now be created by stepping *different* models ([#1408](https://github.com/pybamm-team/PyBaMM/pull/1408))
- Added support for Python 3.9 and dropped support for Python 3.6. Python 3.6 may still work but is now untested ([#1370](https://github.com/pybamm-team/PyBaMM/pull/1370))
- Added the electrolyte overpotential and Ohmic losses for full conductivity, including surface form ([#1350](https://github.com/pybamm-team/PyBaMM/pull/1350))
Expand Down
15 changes: 14 additions & 1 deletion examples/notebooks/compare-particle-diffusion-models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,20 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
"version": "3.8.8"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
}
},
"nbformat": 4,
Expand Down
441 changes: 441 additions & 0 deletions examples/notebooks/customize-quick-plot.ipynb

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions pybamm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,15 @@ def version(formatted=False):
from .plotting.quick_plot import QuickPlot, close_plots
from .plotting.plot import plot
from .plotting.plot2D import plot2D
from .plotting.plot_voltage_components import plot_voltage_components
from .plotting.dynamic_plot import dynamic_plot

# Define the plot-style string and set the default plotting style (can be overwritten
# in a specific script)
default_plot_style = os.path.join(root_dir(), "pybamm/plotting/pybamm.mplstyle")
import matplotlib.pyplot as plt

plt.style.use(default_plot_style)
#
# Simulation
#
Expand Down
2 changes: 2 additions & 0 deletions pybamm/plotting/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def plot(x, y, xlabel=None, ylabel=None, title=None, testing=False, **kwargs):
The label for the y axis
testing : bool, optional
Whether to actually make the plot (turned off for unit tests)
kwargs
Keyword arguments, passed to plt.plot

"""
import matplotlib.pyplot as plt
Expand Down
80 changes: 80 additions & 0 deletions pybamm/plotting/plot_voltage_components.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#
# Method for plotting voltage components
#
import numpy as np


def plot_voltage_components(
solution, ax=None, show_legend=True, testing=False, **kwargs_fill
):
"""
Generate a plot showing the component overpotentials that make up the voltage

Parameters
----------
solution : :class:`pybamm.Solution`
Solution object from which to extract voltage components
ax : matplotlib Axis, optional
The axis on which to put the plot. If None, a new figure and axis is created.
show_legend : bool, optional
Whether to display the legend. Default is True
testing : bool, optional
Whether to actually make the plot (turned off for unit tests)
kwargs_fill
Keyword arguments, passed to ax.fill_between

"""
import matplotlib.pyplot as plt

# Set a default value for alpha, the opacity
kwargs_fill = {"alpha": 0.6, **kwargs_fill}

if ax is not None:
testing = True
else:
_, ax = plt.subplots()

overpotentials = [
"X-averaged battery reaction overpotential [V]",
"X-averaged battery concentration overpotential [V]",
"X-averaged battery electrolyte ohmic losses [V]",
"X-averaged battery solid phase ohmic losses [V]",
]

# Plot
# Initialise
time = solution["Time [h]"].entries
initial_ocv = solution["X-averaged battery open circuit voltage [V]"](0)
ocv = solution["X-averaged battery open circuit voltage [V]"].entries
ax.fill_between(time, ocv, initial_ocv, **kwargs_fill)
top = ocv
# Plot components
for overpotential in overpotentials:
bottom = top + solution[overpotential].entries
ax.fill_between(time, bottom, top, **kwargs_fill)
top = bottom
V = solution["Battery voltage [V]"].entries
ax.plot(time, V, "k--")
if show_legend:
labels = [
"Voltage",
"Open-circuit voltage",
"Reaction overpotential",
"Concentration overpotential",
"Ohmic electrolyte overpotential",
"Ohmic electrode overpotential",
]
leg = ax.legend(labels, loc="lower left", frameon=True)
leg.get_frame().set_edgecolor("k")

# Labels
ax.set_xlim([time[0], time[-1]])
ax.set_xlabel("Time [h]")

y_min, y_max = 0.98 * np.nanmin(V), 1.02 * np.nanmax(initial_ocv)
ax.set_ylim([y_min, y_max])

if not testing: # pragma: no cover
plt.show()

return ax
22 changes: 22 additions & 0 deletions pybamm/plotting/pybamm.mplstyle
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Plot style based on seaborn-paper
axes.labelsize: 11
axes.titlesize: 12
xtick.labelsize: 9
ytick.labelsize: 9
legend.fontsize: 9

axes.prop_cycle: cycler('color', ["r", "b", "k", "g", "m", "c"])
grid.linewidth: 0.8
lines.linewidth: 1.8
patch.linewidth: 0.24

xtick.major.width: 0.8
ytick.major.width: 0.8
xtick.minor.width: 0.4
ytick.minor.width: 0.4

xtick.major.pad: 5.6
ytick.major.pad: 5.6

image.cmap: coolwarm
pcolor.shading: gouraud
Loading