From ca63f1d8ac5dbd51cde2c9e2df65ebe12e038caf Mon Sep 17 00:00:00 2001
From: Joseph Weston <joseph@weston.cloud>
Date: Mon, 13 May 2019 13:23:05 +0200
Subject: [PATCH] add matplotlib boilerplate to start of all tutorials

Currently we just set the default figure size. Matplotlib is
annoying when it comes to setting these default parameters, and we
have to ensure that we set any 'rcParams' in a separate Jupyter cell
and *after* we import pyplot.
---
 doc/source/pre/whatsnew/1.3.rst              |  3 +++
 doc/source/tutorial/boilerplate.py           |  6 ++++++
 doc/source/tutorial/discretize.rst           |  3 +++
 doc/source/tutorial/faq.rst                  |  3 +++
 doc/source/tutorial/first_steps.rst          | 13 ++++++++++++
 doc/source/tutorial/graphene.rst             |  3 +++
 doc/source/tutorial/kpm.rst                  |  7 +++++--
 doc/source/tutorial/operators.rst            |  3 +++
 doc/source/tutorial/plotting.rst             |  6 ++++++
 doc/source/tutorial/spectrum.rst             |  6 ++++++
 doc/source/tutorial/spin_potential_shape.rst | 21 ++++++++++++++++++++
 doc/source/tutorial/superconductors.rst      |  3 +++
 12 files changed, 75 insertions(+), 2 deletions(-)
 create mode 100644 doc/source/tutorial/boilerplate.py

diff --git a/doc/source/pre/whatsnew/1.3.rst b/doc/source/pre/whatsnew/1.3.rst
index 0ba00ce8..8336e99d 100644
--- a/doc/source/pre/whatsnew/1.3.rst
+++ b/doc/source/pre/whatsnew/1.3.rst
@@ -52,6 +52,9 @@ a quantum anomalous spin Hall phase:
     import kwant
     import kwant.continuum
 
+.. jupyter-execute:: ../../tutorial/boilerplate.py
+    :hide-code:
+
 .. jupyter-execute::
 
     def make_model(a):
diff --git a/doc/source/tutorial/boilerplate.py b/doc/source/tutorial/boilerplate.py
new file mode 100644
index 00000000..db2fba37
--- /dev/null
+++ b/doc/source/tutorial/boilerplate.py
@@ -0,0 +1,6 @@
+import matplotlib
+import matplotlib.pyplot
+from IPython.display import set_matplotlib_formats
+
+matplotlib.rcParams['figure.figsize'] = matplotlib.pyplot.figaspect(1) * 2
+set_matplotlib_formats('svg')
diff --git a/doc/source/tutorial/discretize.rst b/doc/source/tutorial/discretize.rst
index 27bab68f..31c4b3bb 100644
--- a/doc/source/tutorial/discretize.rst
+++ b/doc/source/tutorial/discretize.rst
@@ -42,6 +42,9 @@ continuum models and for discretizing them into tight-binding models.
 
     import kwant
 
+.. jupyter-execute:: boilerplate.py
+    :hide-code:
+
 .. _tutorial_discretizer_introduction:
 
 Discretizing by hand
diff --git a/doc/source/tutorial/faq.rst b/doc/source/tutorial/faq.rst
index 42afab79..ec52448d 100644
--- a/doc/source/tutorial/faq.rst
+++ b/doc/source/tutorial/faq.rst
@@ -14,6 +14,9 @@ into Kwant's structure.
     import matplotlib
     from matplotlib import pyplot as plt
 
+.. jupyter-execute:: boilerplate.py
+    :hide-code:
+
 What is a system, and what is a builder?
 ========================================
 A Kwant system represents a particular tight-binding model. It contains a graph
diff --git a/doc/source/tutorial/first_steps.rst b/doc/source/tutorial/first_steps.rst
index bfb60e0c..f509aaa0 100644
--- a/doc/source/tutorial/first_steps.rst
+++ b/doc/source/tutorial/first_steps.rst
@@ -88,6 +88,9 @@ In order to use Kwant, we need to import it:
     # For plotting
     from matplotlib import pyplot
 
+.. jupyter-execute:: boilerplate.py
+    :hide-code:
+
 .. jupyter-execute::
 
     import kwant
@@ -452,6 +455,11 @@ file and defining the a square lattice and empty scattering region.
     # For plotting
     from matplotlib import pyplot
 
+.. jupyter-execute:: boilerplate.py
+    :hide-code:
+
+.. jupyter-execute::
+
     a = 1
     t = 1.0
     W, L = 10, 30
@@ -661,6 +669,11 @@ return a Kwant ``Builder``:
     from matplotlib import pyplot
     import kwant
 
+.. jupyter-execute:: boilerplate.py
+    :hide-code:
+
+.. jupyter-execute::
+
     def make_system(L, W, a=1, t=1.0):
         lat = kwant.lattice.square(a)
 
diff --git a/doc/source/tutorial/graphene.rst b/doc/source/tutorial/graphene.rst
index 6c271d88..6eaa93d6 100644
--- a/doc/source/tutorial/graphene.rst
+++ b/doc/source/tutorial/graphene.rst
@@ -47,6 +47,9 @@ explicitly here to show how to define a new lattice:
 
     sin_30, cos_30 = (1 / 2, sqrt(3) / 2)
 
+.. jupyter-execute:: boilerplate.py
+    :hide-code:
+
 .. jupyter-execute::
 
     graphene = kwant.lattice.general([(1, 0), (sin_30, cos_30)],
diff --git a/doc/source/tutorial/kpm.rst b/doc/source/tutorial/kpm.rst
index 767423f3..7e123258 100644
--- a/doc/source/tutorial/kpm.rst
+++ b/doc/source/tutorial/kpm.rst
@@ -39,6 +39,9 @@ KPM method `kwant.kpm`, that is based on the algorithms presented in Ref. [1]_.
     import scipy
     from matplotlib import pyplot
 
+.. jupyter-execute:: boilerplate.py
+    :hide-code:
+
 Introduction
 ************
 
@@ -151,7 +154,7 @@ We start by importing kwant and defining our system.
 
     # Plot several density of states curves on the same axes.
     def plot_dos(labels_to_data):
-        pyplot.figure(figsize=(5,4))
+        pyplot.figure()
         for label, (x, y) in labels_to_data:
             pyplot.plot(x, y.real, label=label, linewidth=2)
         pyplot.legend(loc=2, framealpha=0.5)
@@ -162,7 +165,7 @@ We start by importing kwant and defining our system.
 
     # Plot fill density of states plus curves on the same axes.
     def plot_dos_and_curves(dos, labels_to_data):
-        pyplot.figure(figsize=(5,4))
+        pyplot.figure()
         pyplot.fill_between(dos[0], dos[1], label="DoS [a.u.]",
                          alpha=0.5, color='gray')
         for label, (x, y) in labels_to_data:
diff --git a/doc/source/tutorial/operators.rst b/doc/source/tutorial/operators.rst
index c10b7780..1646ae86 100644
--- a/doc/source/tutorial/operators.rst
+++ b/doc/source/tutorial/operators.rst
@@ -51,6 +51,9 @@ texture.
     # letters denote spinor indices
     sigma = np.rollaxis(np.array([sigma_x, sigma_y, sigma_z]), 1)
 
+.. jupyter-execute:: boilerplate.py
+    :hide-code:
+
 Introduction
 ------------
 Our starting point will be the following spinful tight-binding model on
diff --git a/doc/source/tutorial/plotting.rst b/doc/source/tutorial/plotting.rst
index 0b84f555..ea04094a 100644
--- a/doc/source/tutorial/plotting.rst
+++ b/doc/source/tutorial/plotting.rst
@@ -35,6 +35,9 @@ these options can be used to achieve various very different objectives.
 
     import kwant
 
+.. jupyter-execute:: boilerplate.py
+    :hide-code:
+
 We begin by first considering a circular graphene quantum dot (similar to what
 has been used in parts of the tutorial :ref:`tutorial-graphene`.)  In contrast
 to previous examples, we will also use hoppings beyond next-nearest neighbors:
@@ -216,6 +219,9 @@ visible. The hoppings are also plotted in order to show the underlying lattice.
 
     import kwant
 
+.. jupyter-execute:: boilerplate.py
+    :hide-code:
+
 Zincblende is a very common crystal structure of semiconductors. It is a
 face-centered cubic crystal with two inequivalent atoms in the unit cell
 (i.e. two different types of atoms, unlike diamond which has the same crystal
diff --git a/doc/source/tutorial/spectrum.rst b/doc/source/tutorial/spectrum.rst
index 1eeef135..b2cbc51d 100644
--- a/doc/source/tutorial/spectrum.rst
+++ b/doc/source/tutorial/spectrum.rst
@@ -30,6 +30,9 @@ Band structure calculations
     # For plotting
     from matplotlib import pyplot
 
+.. jupyter-execute:: boilerplate.py
+    :hide-code:
+
 When doing transport simulations, one also often needs to know the band
 structure of the leads, i.e. the energies of the propagating plane waves in the
 leads as a function of momentum. This band structure contains information about
@@ -137,6 +140,9 @@ Closed systems
     from matplotlib import pyplot
     import kwant
 
+.. jupyter-execute:: boilerplate.py
+    :hide-code:
+
 Although Kwant is (currently) mainly aimed towards transport problems, it
 can also easily be used to compute properties of closed systems -- after
 all, a closed system is nothing more than a scattering region without leads!
diff --git a/doc/source/tutorial/spin_potential_shape.rst b/doc/source/tutorial/spin_potential_shape.rst
index 9107f701..770ba4a5 100644
--- a/doc/source/tutorial/spin_potential_shape.rst
+++ b/doc/source/tutorial/spin_potential_shape.rst
@@ -68,6 +68,9 @@ for small arrays.)
     # For plotting
     from matplotlib import pyplot
 
+.. jupyter-execute:: boilerplate.py
+    :hide-code:
+
 .. jupyter-execute::
 
     # For matrix support
@@ -242,6 +245,9 @@ Spatially dependent values through functions
     # For plotting
     from matplotlib import pyplot
 
+.. jupyter-execute:: boilerplate.py
+    :hide-code:
+
 Up to now, all examples had position-independent matrix-elements
 (and thus translational invariance along the wire, which
 was the origin of the conductance steps). Now, we consider the
@@ -403,6 +409,9 @@ Nontrivial shapes
     # For plotting
     from matplotlib import pyplot
 
+.. jupyter-execute:: boilerplate.py
+    :hide-code:
+
 Up to now, we only dealt with simple wire geometries. Now we turn to the case
 of a more complex geometry, namely transport through a quantum ring
 that is pierced by a magnetic flux :math:`\Phi`:
@@ -594,6 +603,12 @@ period of one flux quantum.
         import kwant
         from matplotlib import pyplot
 
+    .. jupyter-execute:: boilerplate.py
+        :hide-code:
+
+    .. jupyter-execute::
+        :hide-code:
+
         a = 1
         t = 1.0
         W = 10
@@ -643,6 +658,12 @@ period of one flux quantum.
         import kwant
         from matplotlib import pyplot
 
+    .. jupyter-execute:: boilerplate.py
+        :hide-code:
+
+    .. jupyter-execute::
+        :hide-code:
+
         a = 1
         t = 1.0
         W = 10
diff --git a/doc/source/tutorial/superconductors.rst b/doc/source/tutorial/superconductors.rst
index 56f5a849..78f004d3 100644
--- a/doc/source/tutorial/superconductors.rst
+++ b/doc/source/tutorial/superconductors.rst
@@ -35,6 +35,9 @@ Superconductors: orbital degrees of freedom, conservation laws and symmetries
     tau_y = tinyarray.array([[0, -1j], [1j, 0]])
     tau_z = tinyarray.array([[1, 0], [0, -1]])
 
+.. jupyter-execute:: boilerplate.py
+    :hide-code:
+
 This example deals with superconductivity on the level of the
 Bogoliubov-de Gennes (BdG) equation. In this framework, the Hamiltonian
 is given as
-- 
GitLab