Skip to content

Commit

Permalink
make examples consistent and contained to a single episode; fix #3
Browse files Browse the repository at this point in the history
  • Loading branch information
edbennett committed Jun 11, 2020
1 parent 48679b3 commit 399d1a8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 32 deletions.
27 changes: 11 additions & 16 deletions _episodes/02-writing-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ If we wanted to plot a variety of quadratic functions, with a
consistent set of styles, we could define a class that does this:

~~~
import matplotlib.pyplot
import numpy
from matplotlib.pyplot import subplots
from numpy import linspace
class QuadraticPlotter:
color = 'red'
Expand All @@ -37,11 +37,10 @@ class QuadraticPlotter:
The line is plotted in the colour specified by color, and with width
linewidth.'''
fig, ax = matplotlib.pyplot.subplots()
x = numpy.linspace(-10, 10, 1000)
fig, ax = subplots()
x = linspace(-10, 10, 1000)
ax.plot(x, a * x ** 2 + b * x + c,
color=self.color, linewidth=self.linewidth)
fig.show()
~~~
{: .language-python}

Expand Down Expand Up @@ -165,11 +164,10 @@ Line width of blue plotter is 5
>> The line is plotted in the colour specified by color, and with width
>> linewidth.'''
>>
>> fig, ax = matplotlib.pyplot.subplots()
>> x = numpy.linspace(self.x_min, self.x_max, 1000)
>> fig, ax = subplots()
>> x = linspace(self.x_min, self.x_max, 1000)
>> ax.plot(x, a * x ** 2 + b * x + c,
>> color=self.color, linewidth=self.linewidth)
>> fig.show()
>>
>> narrow_plot = QuadraticPlotter()
>> wide_plot = QuadraticPlotter()
Expand All @@ -192,8 +190,7 @@ Line width of blue plotter is 5
>
> ~~~
> from scipy.odr import ODR, Model, RealData
> from matplotlib.pyplot import subplots, show
> from numpy import linspace
> from matplotlib.pyplot import show
>
> def linear(params, x):
> return params[0] * x + params[1]
Expand Down Expand Up @@ -290,11 +287,10 @@ class QuadraticPlotter:
The line is plotted in the colour specified by color, and with width
linewidth.'''

fig, ax = matplotlib.pyplot.subplots()
x = numpy.linspace(-10, 10, 1000)
fig, ax = subplots()
x = linspace(-10, 10, 1000)
ax.plot(x, a * x ** 2 + b * x + c,
color=self.color, linewidth=self.linewidth)
fig.show()

pink_plotter = QuadraticPlotter(color='magenta', linewidth=3)
pink_plotter.plot(0, 1, 0)
Expand Down Expand Up @@ -327,11 +323,10 @@ usable, rather than deferring these errors to a long way down the line.
>> The line is plotted in the colour specified by color, and with width
>> linewidth.'''
>>
>> fig, ax = matplotlib.pyplot.subplots()
>> x = numpy.linspace(self.x_min, self.x_max, 1000)
>> fig, ax = subplots()
>> x = linspace(self.x_min, self.x_max, 1000)
>> ax.plot(x, a * x ** 2 + b * x + c,
>> color=self.color, linewidth=self.linewidth)
>> fig.show()
>>
>> narrow_plot = QuadraticPlotter()
>> wide_plot = QuadraticPlotter(x_min=-5, x_max=50)
Expand Down
14 changes: 8 additions & 6 deletions _episodes/03-inheritance.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ increasingly complex and build up functionality in layers.
>> ## Solution
>>
>> ~~~
>> from numpy import linspace
>> from matplotlib.pyplot import subplots
>> from matplotlib.colors import is_color_like
>>
>> class PolynomialPlotter:
>> def __init__(self, color='red', linewidth=1, x_min=-10, x_max=10):
>> assert is_color_like(color)
Expand All @@ -349,11 +353,10 @@ increasingly complex and build up functionality in layers.
>> plot the polynomial f(x) = ax^n + bx^(n-1) + cx^(n-2) + ... .
>> The line is plotted in the colour specified by color, and with width
>> linewidth.'''
>> fig, ax = matplotlib.pyplot.subplots()
>> x = numpy.linspace(self.x_min, self.x_max, 1000)
>> fig, ax = subplots()
>> x = linspace(self.x_min, self.x_max, 1000)
>> ax.plot(x, self.polynomial(x, coefficients),
>> color=self.color, linewidth=self.linewidth)
>> fig.show()
>>
>> class QuadraticPlotter(PolynomialPlotter):
>> def plot(self, a, b, c):
Expand Down Expand Up @@ -383,10 +386,9 @@ increasingly complex and build up functionality in layers.
>> '''Plot a function of a single argument.
>> The line is plotted in the colour specified by color, and with width
>> linewidth.'''
>> fig, ax = matplotlib.pyplot.subplots()
>> x = numpy.linspace(self.x_min, self.x_max, 1000)
>> fig, ax = subplots()
>> x = linspace(self.x_min, self.x_max, 1000)
>> ax.plot(x, function(x), color=self.color, linewidth=self.linewidth)
>> fig.show()
>>
>>
>> class PolynomialPlotter(FunctionPlotter):
Expand Down
9 changes: 6 additions & 3 deletions _episodes/04-decorators.md
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,10 @@ Modified perimeter: 21
>> ## Solution
>>
>> ~~~
>> from numpy import linspace
>> from matplotlib.pyplot import subplots
>> from matplotlib.colors import is_color_like
>>
>> class FunctionPlotter:
>> def __init__(self, color='red', linewidth=1, x_min=-10, x_max=10):
>> self.color = color
Expand All @@ -537,10 +541,9 @@ Modified perimeter: 21
>> '''Plot a function of a single argument.
>> The line is plotted in the colour specified by color, and with width
>> linewidth.'''
>> fig, ax = matplotlib.pyplot.subplots()
>> x = numpy.linspace(self.x_min, self.x_max, 1000)
>> fig, ax = subplots()
>> x = linspace(self.x_min, self.x_max, 1000)
>> ax.plot(x, function(x), color=self._color, linewidth=self.linewidth)
>> fig.show()
>> ~~~
>> {: .language-python}
> {: .solution}
Expand Down
11 changes: 7 additions & 4 deletions _episodes/05-dunder.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ class to be called like functions. For example, returning to the
`FunctionPlotter` example:
~~~
from numpy import linspace, sin
from matplotlib.colors import is_color_like
from matplotlib.pyplot import subplots

class FunctionPlotter:
def __init__(self, color='red', linewidth=1, x_min=-10, x_max=10):
self.color = color
Expand All @@ -312,16 +316,15 @@ class FunctionPlotter:
'''Plot a function of a single argument.
The line is plotted in the colour specified by color, and with width
linewidth.'''
fig, ax = matplotlib.pyplot.subplots()
x = numpy.linspace(self.x_min, self.x_max, 1000)
fig, ax = subplots()
x = linspace(self.x_min, self.x_max, 1000)
ax.plot(x, function(x), color=self._color, linewidth=self.linewidth)
fig.show()

def __call__(self, *args, **kwargs):
return self.plot(*args, **kwargs)

plotter = FunctionPlotter()
plotter(numpy.sin)
plotter(sin)
~~~
{: .language-python}
Expand Down
5 changes: 2 additions & 3 deletions _episodes/06-duck.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fractal.

~~~
%matplotlib inline
from numpy import linspace, newaxis, pi
from numpy import angle, linspace, newaxis, pi
from matplotlib.pyplot import colorbar, subplots
def complex_linspace(lower, upper, num_real, num_imag):
Expand All @@ -113,14 +113,13 @@ initial_z = complex_linspace(z_min, z_max, 1000, 1000)
results = newton(test_polynomial, test_derivative, initial_z, 20)
fig, ax = subplots()
image = ax.imshow(numpy.angle(results), vmin=-3, vmax=3,
image = ax.imshow(angle(results), vmin=-3, vmax=3,
extent=(z_min.real, z_max.real, z_min.imag, z_max.imag))
cbar = colorbar(image, ax=ax, ticks=(-2*pi/3, 0, 2*pi/3))
cbar.set_label(r'$\arg(z_n)$')
cbar.ax.set_yticklabels((r'$-\frac{2\pi}{3}$', '0', r'$\frac{2\pi}{3}$'))
ax.set_xlabel(r'$\operatorname{Re}(z_0)$')
ax.set_ylabel(r'$\operatorname{Im}(z_0)$')
fig.show()
~~~
{: .language-python}

Expand Down

0 comments on commit 399d1a8

Please sign in to comment.