Skip to content

Commit

Permalink
#1413 add example notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Mar 5, 2021
1 parent 2f7aa1c commit df4c398
Show file tree
Hide file tree
Showing 7 changed files with 519 additions and 14 deletions.
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
473 changes: 473 additions & 0 deletions examples/notebooks/customize-quick-plot.ipynb

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions examples/scripts/SPMe.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@

# plot
plot = pybamm.QuickPlot(
solution,
[solution] * 2,
[
"Negative particle concentration [mol.m-3]",
# "Negative particle concentration [mol.m-3]",
"Electrolyte concentration [mol.m-3]",
"Positive particle concentration [mol.m-3]",
# "Positive particle concentration [mol.m-3]",
"Current [A]",
"Negative electrode potential [V]",
"Electrolyte potential [V]",
Expand All @@ -47,4 +47,7 @@
spatial_unit="um",
variable_limits="tight",
)
plot.dynamic_plot()
plot.plot(0) # dynamic_plot()
import matplotlib.pyplot as plt

plt.show()
2 changes: 1 addition & 1 deletion pybamm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def version(formatted=False):

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

plt.style.use(default_plot_style)
Expand Down
1 change: 0 additions & 1 deletion pybamm/plotting/plot_voltage_components.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#
# Method for plotting voltage components
#
import pybamm
import numpy as np


Expand Down
30 changes: 23 additions & 7 deletions pybamm/plotting/quick_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ def ax_max(data):
return 1.04 * data_max


def split_long_string(title, max_words=4):
def split_long_string(title, max_words=None):
"""Get title in a nice format"""
max_words = max_words or pybamm.settings.max_words_in_line
words = title.split()
# Don't split if fits on one line, don't split just for units
if len(words) <= max_words or words[max_words].startswith("["):
Expand Down Expand Up @@ -440,7 +441,7 @@ def reset_axis(self):
): # pragma: no cover
raise ValueError(f"Axis limits cannot be NaN for variables '{key}'")

def plot(self, t):
def plot(self, t, dynamic=False):
"""Produces a quick plot with the internal states at time t.
Parameters
Expand Down Expand Up @@ -598,11 +599,26 @@ def plot(self, t):
)

# Set global legend
if len(solution_handles) > 0:
self.fig.legend(solution_handles, self.labels, loc="lower right")
if len(self.labels) > 1:
fig_legend = self.fig.legend(
solution_handles, self.labels, loc="lower right"
)
# Get the position of the top of the legend in relative figure units
# There may be a better way ...
legend_top_inches = fig_legend.get_window_extent(
renderer=self.fig.canvas.get_renderer()
).get_points()[1, 1]
fig_height_inches = (self.fig.get_size_inches() * self.fig.dpi)[1]
legend_top = legend_top_inches / fig_height_inches
else:
legend_top = 0

# Fix layout
bottom = 0.05 + 0.03 * max((len(self.labels) - 2), 0)
if dynamic:
slider_top = 0.05
else:
slider_top = 0
bottom = max(legend_top, slider_top)
self.gridspec.tight_layout(self.fig, rect=[0, bottom, 1, 1])

def dynamic_plot(self, testing=False, step=None):
Expand All @@ -623,7 +639,7 @@ def dynamic_plot(self, testing=False, step=None):

step = step or self.max_t / 100
widgets.interact(
self.plot,
lambda t: self.plot(t, dynamic=False),
t=widgets.FloatSlider(min=0, max=self.max_t, step=step, value=0),
continuous_update=False,
)
Expand All @@ -632,7 +648,7 @@ def dynamic_plot(self, testing=False, step=None):
from matplotlib.widgets import Slider

# create an initial plot at time 0
self.plot(0)
self.plot(0, dynamic=True)

axcolor = "lightgoldenrodyellow"
ax_slider = plt.axes([0.315, 0.02, 0.37, 0.03], facecolor=axcolor)
Expand Down
1 change: 1 addition & 0 deletions pybamm/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Settings(object):
_max_smoothing = "exact"
_heaviside_smoothing = "exact"
_abs_smoothing = "exact"
max_words_in_line = 4

@property
def debug_mode(self):
Expand Down

0 comments on commit df4c398

Please sign in to comment.