mirror of
https://github.com/wassname/viz_torch_optim.git
synced 2026-08-06 13:40:52 +08:00
860 KiB
860 KiB
In [1]:
%pylab inlinePopulating the interactive namespace from numpy and matplotlib
In [2]:
import torch
from torch.autograd import VariableIn [3]:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.colors import LogNorm
from matplotlib import animation
from IPython.display import HTML
from collections import defaultdict
from itertools import zip_longest
from functools import partialIn [4]:
import datetime
ts = datetime.datetime.utcnow().strftime('%Y%m%d_%H-%M-%S')In [ ]:
In [15]:
"""modified from https://github.com/pytorch/pytorch/blob/master/test/test_optim.py"""
def test_f(f, df, constructor, steps=150, x0=[-4,-1], solution=[-2,0]):
state = {}
# start
params = Variable(torch.Tensor(x0), requires_grad=True)
optimizer = constructor([params])
solution = torch.Tensor(solution)
initial_dist = params.data.dist(solution)
def eval():
optimizer.zero_grad()
loss = f(params)
loss.backward()
# loss.backward() will give **slightly** different
# gradients, than drosenbtock, because of a different ordering
# of floating point operations. In most cases it doesn't matter,
# but some optimizers are so sensitive that they can temporarily
# diverge up to 1e-4, just to converge again. This makes the
# comparison more stable.
# params.grad.data.copy_(df(params.data))
return loss
data=[]
dist=[]
for i in range(steps):
optimizer.step(eval)
dist.append(params.data.dist(solution)) # loss
data.append(params.data.numpy().copy())
return np.array(data), np.array(dist)In [ ]:
azim=-95In [16]:
def to_tensor(x):
# TODO: I'm sure there's a proper way to do this
if isinstance(x, np.ndarray):
return torch.FloatTensor(x.astype(np.float32))
if isinstance(x, list):
return torch.FloatTensor(x)
elif isinstance(x, (float, int, numpy.generic)):
return torch.FloatTensor([float(x)])
else:
return x
# def from_tensor(x):
# x = getattr(x,'data',x)
# if hasattr(x, 'numpy'):
# x = x.numpy()
# return x
# to_tensor(1.0)
# to_tensor(1)
# to_tensor(np.array([1.0]))
# to_tensor([1.0])
# to_tensor(torch.rand((4,4)))
# to_tensor(Variable(torch.rand((4,4))))Out [16]:
1 [torch.FloatTensor of size 1]
In [17]:
class Problem(object):
def __init__(self, f, df, minima, x0, bounds=[[-5,5],[-5,5]], lr=1e-3, steps=3000):
"""
Problem setup
Params:
- f: function [x1,x2] => z
- df: derivative function ([x1,x2]=>[dx1,dx2])
- minima: where the function has a minima
- self: bounds
- x0: suggested start
- lr: suggested learning rate
- steps: suggested steps
"""
self.f = f
self.df = df
self.x0 = x0
self.bounds = bounds
self.minima = minima
self.lr = lr
self.steps = steps
self.xmin = bounds[0][0]
self.xmax = bounds[0][1]
self.ymin = bounds[1][0]
self.ymax = bounds[1][1]In [18]:
"""A valley"""
def madsen(tensor):
"""Madsen function (1981)."""
x1, x2 = tensor
x1 = to_tensor(x1)
x2 = to_tensor(x2)
r = x1**2 + x2**2 + x1 * x2 +\
torch.sin(x1) +\
torch.cos(x2) #+ np.abs(noise(x1,x2))
return r
def dmadsen(tensor):
x1, x2 = tensor
x1 = to_tensor(x1)
x2 = to_tensor(x2)
dx1=2. * x1 + x2 + np.cos(x1)
dx2 = 2. * x2 + x1 -np.sin(x2)
return torch.stack([dx1, dx2],1)[0]
madsen_problem= Problem(
f=madsen,
df=dmadsen,
minima=np.array([-0.39999999999999591, 0.20000000000000462]),
x0=[-3,-4],
steps=3000,
lr=1e-4
)
problem=madsen_problemIn [19]:
def schaffern4(tensor):
"""https://www.sfu.ca/~ssurjano/schaffer4.html"""
x1, x2 = tensor
x1 = to_tensor(x1)
x2 = to_tensor(x2)
r = 0.5 + (cos(sin(abs(x1**2-x2**2))) - 0.5)/(1+0.001*(x1**2+x2**2))**2
return r*100
def dschaffern4(tensor):
x1, x2 = tensor
x1 = to_tensor(x1)
x2 = to_tensor(x2)
dx1 = -2*(x1**2 - x2**2)*x1*cos(abs(-x1**2 + x2**2))*sin(sin(abs(-x1**2 + x2**2)))/((0.001*x1**2 + 0.001*x2**2 + 1.0)**2*abs(-x1**2 + x2**2)) - 0.004*x1*(cos(sin(abs(-x1**2 + x2**2))) - 0.5)/(0.001*x1**2 + 0.001*x2**2 + 1.0)**3
dx2 = 2*(x1**2 - x2**2)*x2*cos(abs(-x1**2 + x2**2))*sin(sin(abs(-x1**2 + x2**2)))/((0.001*x1**2 + 0.001*x2**2 + 1.0)**2*abs(-x1**2 + x2**2)) - 0.004*x2*(cos(sin(abs(-x1**2 + x2**2))) - 0.5)/(0.001*x1**2 + 0.001*x2**2 + 1.0)**3
return torch.stack([dx1, dx2], 1)[0]
schaffern4_problem = Problem(
f=schaffern4,
df=dschaffern4,
# minima=np.array([0, 1.25313]),
minima=np.array([100,100]),
bounds=[[-100, 100], [-100, 100]],
x0=[10, 5],
steps=3000,
lr=1e-1
)
problem = schaffern4_problemIn [20]:
"""Banana shaped"""
def rosenbrock(tensor):
x, y = tensor
x = to_tensor(x)
y = to_tensor(y)
return (1 - x) ** 2 + 100 * (y - x ** 2) ** 2
def drosenbrock(tensor):
x, y = tensor
x = to_tensor(x)
y = to_tensor(y)
dx = 400 * (x**2 -y) + 2 * x - 2
dy = -200 * x**2 + 200 * y
return torch.stack([dx,dy],1)[0]
rosenbrock_problem= Problem(
f=rosenbrock,
df=drosenbrock,
minima=np.array([1,1]),
x0=[-3,-4],
steps=3000,
lr=1e-4
)
problem=rosenbrock_problem
In [21]:
from torch import sin, cos, sqrt, abs, log
def eggholder(tensor):
"""https://www.sfu.ca/~ssurjano/camel6.html"""
x1, x2 = tensor
x1 = to_tensor(x1)
x2 = to_tensor(x2)
r = - (x2+47)*sin(sqrt(abs(x2+x1/2+47))) - x1*sin(sqrt(abs(x1-(x2+47))))
return r
def deggholder(tensor):
x1, x2 = tensor
x1 = to_tensor(x1)
x2 = to_tensor(x2)
dx1 = -1/8*(x1 + 2*x2 + 94)*(x2 + 47)*cos(sqrt(abs(1/2*x1 + x2 + 47)))/abs(1/2*x1 + x2 + 47)**(3/2) - 1/2*(x1 - x2 - 47)*x1*cos(sqrt(abs(-x1 + x2 + 47)))/abs(-x1 + x2 + 47)**(3/2) - sin(sqrt(abs(-x1 + x2 + 47)))
dx2 = -1/4*(x1 + 2*x2 + 94)*(x2 + 47)*cos(sqrt(abs(1/2*x1 + x2 + 47)))/abs(1/2*x1 + x2 + 47)**(3/2) + 1/2*(x1 - x2 - 47)*x1*cos(sqrt(abs(-x1 + x2 + 47)))/abs(-x1 + x2 + 47)**(3/2) - sin(sqrt(abs(1/2*x1 + x2 + 47)))
return torch.stack([dx1, dx2], 1)[0]
eggholder_problem = Problem(
f=eggholder,
df=deggholder,
minima=np.array([-512, -404.2319]),
bounds=[[-512, 512], [-512, 512]],
x0=[100,300],
steps=3000,
lr=1e-4
)
problem = eggholder_problemIn [22]:
def six_humped_camel_back(tensor):
"""https://www.sfu.ca/~ssurjano/camel6.html"""
x1, x2 = tensor
x1 = to_tensor(x1)
x2 = to_tensor(x2)
r = (4 - 2.1 * x1**2 + x1**4 / 3) * x1**2 + \
x1 * x2 +\
(-4 + 4 * x2**2) * x2**2
return r
def dsix_humped_camel_back(tensor):
x1, x2 = tensor
x1 = to_tensor(x1)
x2 = to_tensor(x2)
dx1 = -0.333333333333333 * (-4.00000000000000 * x1**3 + 12.6000000000000 * x1) * x1**2 - 0.666666666666667 * \
(-1.00000000000000 * x1**4 + 6.30000000000000 *
x1**2 - 12.0000000000000) * x1 + x2
dx2 = 8 * x2**3 + 8 * (x2**2 - 1) * x2 + x1
return torch.stack([dx1, dx2], 1)[0]
camel6_problem = Problem(
f=six_humped_camel_back,
df=dsix_humped_camel_back,
minima=np.array([0.0898,-0.7126]),
bounds=[[-3, 3], [-2, 2]],
x0=[1.3, -1.5],
steps=3000,
lr=1e-3
)
problem = camel6_problemIn [23]:
"""Valley"""
def beales(tensor):
"""Beales function, like a valley"""
x, y = tensor
x = to_tensor(x)
y = to_tensor(y)
# + noise(x,y)
return (1.5 - x + x * y)**2 + (2.25 - x + x * y**2)**2 + (2.625 - x + x * y**3)**2
def dbeales(tensor):
x, y = tensor
x = to_tensor(x)
y = to_tensor(y)
dx = 2 * (x * y**3 - x + 2.625) * (y**3 - 1) + 2 * (x * y**2 -
x + 2.25) * (y**2 - 1) + 2 * (x * y - x + 1.5) * (y - 1)
dy = 6 * (x * y**3 - x + 2.625) * x * y**2 + 4 * \
(x * y**2 - x + 2.25) * x * y + 2 * (x * y - x + 1.5) * x
return torch.stack([dx, dy], 1)[0]
beales_problem= Problem(
f=beales,
df=dbeales,
minima=np.array([3., 0.5]),
bounds=[[-4.5,4.5],[-4.5,4.5]],
x0=[1.4,1.7],
steps=6000,
lr=1e-3
)
problem=beales_problem
azim=-95In [26]:
# define boundaries
# xmin, xmax, xstep = -5, 5, .05
# ymin, ymax, ystep = -5, 5, .05
# x0 = np.array([3., 4.])
xmin = problem.xmin
xmax = problem.xmax
ymin = problem.ymin
ymax = problem.ymax
ystep = xstep= (xmax-xmin)/200.0
zeps = 1.1e-0 # we don't want the minima to be actual zero or we wont get any lines shown on a log scale
# and x, y, z
x, y = np.meshgrid(np.arange(xmin, xmax + xstep, xstep), np.arange(ymin, ymax + ystep, ystep))
z = problem.f([x, y]).numpy() # we shift everything up so the min is zero
logzmax=np.log(z.max()-z.min()+zeps)
z_min = problem.f(problem.minima).numpy()
z.min(), z.max(), z_min[0]Out [26]:
(0.00029144029, 181853.61, 0.0)
In [27]:
# reshape some vars
minima_ = problem.minima.reshape(-1, 1)
_x0 = np.array([problem.x0]).TIn [28]:
assert z.min()>=z_min[0], 'your minina is wrong'
assert (problem.df(problem.minima).numpy()<=1e-3).all(), 'gradient should be close to 0 at minima'In [29]:
ax = plt.gca()
cm=ax.contour(x, y, z - z_min[0] + zeps, levels=np.logspace(0, logzmax//2, 55), norm=LogNorm(), cmap=plt.cm.jet, alpha=0.15)
plt.colorbar(cm)
ax.plot(*minima_, 'r*', markersize=10)
ax.plot(*problem.x0, 'r+', markersize=10)
plt.title('debug: grid')
plt.show()In [31]:
import torch.optim as optim
lr = problem.lr
constructors = dict(
# need smaller lr's sometimes
SGD= lambda params: optim.SGD(params, lr=lr),
momentum = lambda params: optim.SGD(params, lr=lr, momentum=0.9),
nesterov = lambda params: optim.SGD(params, lr=lr, momentum=0.9, nesterov=True),
nesterov_decay = lambda params: optim.SGD(params, lr=lr, momentum=0.9, nesterov=True, weight_decay=1e-4),
# need larger lr's sometimes
Adadelta = lambda params: optim.Adadelta(params),
Adagrad = lambda params: optim.Adagrad(params, lr=lr),
#
Adamax = lambda params: optim.Adamax(params, lr=lr),
RMSprop = lambda params: optim.RMSprop(params, lr=lr),
Adam = lambda params: optim.Adam(params, lr=lr),
# lambda params: optim.Adam(params, lr=lr, weight_decay=1e-2),
# need to read about these, might not be comparable
# ASGD = lambda params: optim.ASGD(params, lr=lr),
# Rprop = lambda params: optim.Rprop(params, lr=lr),
# LBFGS = lambda params: optim.LBFGS(params),
)In [32]:
results = {}
distance = {}
for name, constructor in constructors.items():
data, dist = test_f(problem.f, problem.df, constructor, x0=problem.x0, steps=problem.steps)
results[name] = data
distance[name] = distIn [ ]:
In [35]:
# calc paths and elevation
methods = constructors.keys()
paths = np.array([path.T for path in results.values()]) # should be (2,N) each
zpaths = np.array([[problem.f(torch.FloatTensor(p)).numpy()-z_min[0] + zeps for p in path.T] for path in paths])
paths.shape, zpaths.shapeOut [35]:
((9, 2, 6000), (9, 6000, 1))
In [36]:
# DEBUG: check z's
for i, name in enumerate(results):
zmax = zpaths[i][np.isfinite(zpaths[i])].max()
print(name, zmax, np.isfinite(zmax).all())SGD 47.4099 True momentum 47.4099 True nesterov 25.8805 True nesterov_decay 25.8805 True Adadelta 95.8278 True Adagrad 96.548 True Adamax 96.548 True RMSprop 93.5872 True Adam 96.548 True
In [37]:
# clip zpaths
zmax = z.max()
zpaths[np.isfinite(zpaths)==False]=zmax
zpaths = np.clip(zpaths, 0, zmax)In [38]:
for i, name in enumerate(results):
plt.plot(np.abs(zpaths[i]), label=name)
plt.legend()
plt.title('loss (mae)')Out [38]:
<matplotlib.text.Text at 0x7f8c885b6e10>
In [ ]:
In [40]:
# static preview 2d to let you debug your steps and learning rate
ax = plt.gca()
for name in results:
plt.scatter(*results[name].T, label=name, s=1)
plt.legend()
plt.xlim(xmin,xmax)
plt.ylim(ymin,ymax)
cm=ax.contour(x, y, z - z_min[0] + zeps, levels=np.logspace(0, logzmax//2, 35), norm=LogNorm(), cmap=plt.cm.jet, alpha=0.15)
plt.colorbar(cm)
ax.plot(*minima_, 'r*', markersize=10)
ax.plot(*problem.x0, 'r+', markersize=10)
plt.title('debug: paths')
plt.show()In [ ]:
In [39]:
# static preview 3d
fig = plt.figure(figsize=(8, 5))
ax = plt.axes(projection='3d', elev=50, azim=azim)
ax.plot_surface(x, y, z, norm=LogNorm(), rstride=1, cstride=1, edgecolor='none', alpha=.25, cmap=plt.cm.jet)
ax.plot(*minima_, problem.f(minima_).numpy(), 'r*', markersize=10)
ax.plot(*_x0, problem.f(_x0).numpy(), 'r+', markersize=10)
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
ax.set_zlabel('$z$')
ax.set_xlim((xmin, xmax))
ax.set_ylim((ymin, ymax))
# anim = TrajectoryAnimation3D(*paths, zpaths=zpaths, labels=methods, ax=ax)
# quick plot to let you debug your steps and learning rate
ax = plt.gca()
for i, name in enumerate(results):
ax.scatter3D(*results[name].T, zpaths[i], label=name, s=1)
plt.legend()
plt.xlim(xmin,xmax)
plt.ylim(ymin,ymax)
ax.legend(loc='top right')Out [39]:
<matplotlib.legend.Legend at 0x7f8c888e6780>
In [41]:
class TrajectoryAnimation(animation.FuncAnimation):
def __init__(self, *paths, labels=[], fig=None, ax=None, frames=None,
interval=60, repeat_delay=5, blit=True, **kwargs):
if fig is None:
if ax is None:
fig, ax = plt.subplots()
else:
fig = ax.get_figure()
else:
if ax is None:
ax = fig.gca()
self.fig = fig
self.ax = ax
self.paths = paths
if frames is None:
frames = max(path.shape[1] for path in paths)
self.lines = [ax.plot([], [], label=label, lw=2)[0]
for _, label in zip_longest(paths, labels)]
self.points = [ax.plot([], [], 'o', color=line.get_color())[0]
for line in self.lines]
super(TrajectoryAnimation, self).__init__(fig, self.animate, init_func=self.init_anim,
frames=frames, interval=interval, blit=blit,
repeat_delay=repeat_delay, **kwargs)
def init_anim(self):
for line, point in zip(self.lines, self.points):
line.set_data([], [])
point.set_data([], [])
return self.lines + self.points
def animate(self, i):
for line, point, path in zip(self.lines, self.points, self.paths):
line.set_data(*path[::,:i])
point.set_data(*path[::,i-1:i])
return self.lines + self.pointsIn [42]:
class TrajectoryAnimation3D(animation.FuncAnimation):
def __init__(self, *paths, zpaths, labels=[], fig=None, ax=None, frames=None,
interval=60, repeat_delay=5, blit=True, **kwargs):
if fig is None:
if ax is None:
fig, ax = plt.subplots()
else:
fig = ax.get_figure()
else:
if ax is None:
ax = fig.gca()
self.fig = fig
self.ax = ax
self.paths = paths
self.zpaths = zpaths
if frames is None:
frames = max(path.shape[1] for path in paths)
self.lines = [ax.plot([], [], [], label=label, lw=2)[0]
for _, label in zip_longest(paths, labels)]
super(TrajectoryAnimation3D, self).__init__(fig, self.animate, init_func=self.init_anim,
frames=frames, interval=interval, blit=blit,
repeat_delay=repeat_delay, **kwargs)
def init_anim(self):
for line in self.lines:
line.set_data([], [])
line.set_3d_properties([])
return self.lines
def animate(self, i):
for line, path, zpath in zip(self.lines, self.paths, self.zpaths):
line.set_data(*path[::,:i])
line.set_3d_properties(zpath[:i])
return self.linesIn [84]:
fig, ax = plt.subplots(figsize=(10, 6))
ax.contour(x, y, z, levels=np.logspace(0, logzmax//2, 35), norm=LogNorm(), cmap=plt.cm.jet, alpha=0.5)
ax.plot(*minima_, 'r*', markersize=10)
ax.plot(*problem.x0, 'r+', markersize=10)
ax.set_title('{} function'.format(problem.f.__name__))
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
ax.set_xlim((xmin, xmax))
ax.set_ylim((ymin, ymax))
anim = TrajectoryAnimation(*paths, labels=methods, ax=ax)
ax.legend(loc='upper left')Out [84]:
<matplotlib.legend.Legend at 0x7f8c0c5ced30>
In [93]:
# anim.to_html5_video()
save_file = 'videos/{name:}_{ts:}.webm'.format(name=problem.f.__name__, ts=ts)
print(save_file)
anim.save(save_file, fps=1000, bitrate=1000, codec='vp9')
# display
html="""<video {options}>
<source type="video/webm" src="{video}">
Your browser does not support the video tag.
</video>""".format(options=' '.join(['controls', 'autoplay']), video=save_file)
print(html)
HTML(html)videos/beales_20171115_07-18-03.webm
[0;31m---------------------------------------------------------------------------[0m [0;31mKeyboardInterrupt[0m Traceback (most recent call last) [0;32m<ipython-input-93-1c93b3c569f0>[0m in [0;36m<module>[0;34m()[0m [1;32m 2[0m [0msave_file[0m [0;34m=[0m [0;34m'videos/{name:}_{ts:}.webm'[0m[0;34m.[0m[0mformat[0m[0;34m([0m[0mname[0m[0;34m=[0m[0mproblem[0m[0;34m.[0m[0mf[0m[0;34m.[0m[0m__name__[0m[0;34m,[0m [0mts[0m[0;34m=[0m[0mts[0m[0;34m)[0m[0;34m[0m[0m [1;32m 3[0m [0mprint[0m[0;34m([0m[0msave_file[0m[0;34m)[0m[0;34m[0m[0m [0;32m----> 4[0;31m [0manim[0m[0;34m.[0m[0msave[0m[0;34m([0m[0msave_file[0m[0;34m,[0m [0mfps[0m[0;34m=[0m[0;36m1000[0m[0;34m,[0m [0mbitrate[0m[0;34m=[0m[0;36m1000[0m[0;34m,[0m [0mcodec[0m[0;34m=[0m[0;34m'vp9'[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 5[0m [0;31m# display[0m[0;34m[0m[0;34m[0m[0m [1;32m 6[0m html="""<video {options}> [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/animation.py[0m in [0;36msave[0;34m(self, filename, writer, fps, dpi, codec, bitrate, extra_args, metadata, extra_anim, savefig_kwargs)[0m [1;32m 1061[0m [0;31m# TODO: See if turning off blit is really necessary[0m[0;34m[0m[0;34m[0m[0m [1;32m 1062[0m [0manim[0m[0;34m.[0m[0m_draw_next_frame[0m[0;34m([0m[0md[0m[0;34m,[0m [0mblit[0m[0;34m=[0m[0;32mFalse[0m[0;34m)[0m[0;34m[0m[0m [0;32m-> 1063[0;31m [0mwriter[0m[0;34m.[0m[0mgrab_frame[0m[0;34m([0m[0;34m**[0m[0msavefig_kwargs[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 1064[0m [0;34m[0m[0m [1;32m 1065[0m [0;31m# Reconnect signal for first draw if necessary[0m[0;34m[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/animation.py[0m in [0;36mgrab_frame[0;34m(self, **savefig_kwargs)[0m [1;32m 326[0m [0;31m# frame format and dpi.[0m[0;34m[0m[0;34m[0m[0m [1;32m 327[0m self.fig.savefig(self._frame_sink(), format=self.frame_format, [0;32m--> 328[0;31m dpi=self.dpi, **savefig_kwargs) [0m[1;32m 329[0m [0;32mexcept[0m [0;34m([0m[0mRuntimeError[0m[0;34m,[0m [0mIOError[0m[0;34m)[0m [0;32mas[0m [0me[0m[0;34m:[0m[0;34m[0m[0m [1;32m 330[0m [0mout[0m[0;34m,[0m [0merr[0m [0;34m=[0m [0mself[0m[0;34m.[0m[0m_proc[0m[0;34m.[0m[0mcommunicate[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/figure.py[0m in [0;36msavefig[0;34m(self, *args, **kwargs)[0m [1;32m 1571[0m [0mself[0m[0;34m.[0m[0mset_frameon[0m[0;34m([0m[0mframeon[0m[0;34m)[0m[0;34m[0m[0m [1;32m 1572[0m [0;34m[0m[0m [0;32m-> 1573[0;31m [0mself[0m[0;34m.[0m[0mcanvas[0m[0;34m.[0m[0mprint_figure[0m[0;34m([0m[0;34m*[0m[0margs[0m[0;34m,[0m [0;34m**[0m[0mkwargs[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 1574[0m [0;34m[0m[0m [1;32m 1575[0m [0;32mif[0m [0mframeon[0m[0;34m:[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/backend_bases.py[0m in [0;36mprint_figure[0;34m(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)[0m [1;32m 2250[0m [0morientation[0m[0;34m=[0m[0morientation[0m[0;34m,[0m[0;34m[0m[0m [1;32m 2251[0m [0mbbox_inches_restore[0m[0;34m=[0m[0m_bbox_inches_restore[0m[0;34m,[0m[0;34m[0m[0m [0;32m-> 2252[0;31m **kwargs) [0m[1;32m 2253[0m [0;32mfinally[0m[0;34m:[0m[0;34m[0m[0m [1;32m 2254[0m [0;32mif[0m [0mbbox_inches[0m [0;32mand[0m [0mrestore_bbox[0m[0;34m:[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py[0m in [0;36mprint_raw[0;34m(self, filename_or_obj, *args, **kwargs)[0m [1;32m 524[0m [0;34m[0m[0m [1;32m 525[0m [0;32mdef[0m [0mprint_raw[0m[0;34m([0m[0mself[0m[0;34m,[0m [0mfilename_or_obj[0m[0;34m,[0m [0;34m*[0m[0margs[0m[0;34m,[0m [0;34m**[0m[0mkwargs[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0m [0;32m--> 526[0;31m [0mFigureCanvasAgg[0m[0;34m.[0m[0mdraw[0m[0;34m([0m[0mself[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 527[0m [0mrenderer[0m [0;34m=[0m [0mself[0m[0;34m.[0m[0mget_renderer[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0m [1;32m 528[0m [0moriginal_dpi[0m [0;34m=[0m [0mrenderer[0m[0;34m.[0m[0mdpi[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py[0m in [0;36mdraw[0;34m(self)[0m [1;32m 462[0m [0;34m[0m[0m [1;32m 463[0m [0;32mtry[0m[0;34m:[0m[0;34m[0m[0m [0;32m--> 464[0;31m [0mself[0m[0;34m.[0m[0mfigure[0m[0;34m.[0m[0mdraw[0m[0;34m([0m[0mself[0m[0;34m.[0m[0mrenderer[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 465[0m [0;32mfinally[0m[0;34m:[0m[0;34m[0m[0m [1;32m 466[0m [0mRendererAgg[0m[0;34m.[0m[0mlock[0m[0;34m.[0m[0mrelease[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/artist.py[0m in [0;36mdraw_wrapper[0;34m(artist, renderer, *args, **kwargs)[0m [1;32m 61[0m [0;32mdef[0m [0mdraw_wrapper[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m,[0m [0;34m*[0m[0margs[0m[0;34m,[0m [0;34m**[0m[0mkwargs[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0m [1;32m 62[0m [0mbefore[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m)[0m[0;34m[0m[0m [0;32m---> 63[0;31m [0mdraw[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m,[0m [0;34m*[0m[0margs[0m[0;34m,[0m [0;34m**[0m[0mkwargs[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 64[0m [0mafter[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m)[0m[0;34m[0m[0m [1;32m 65[0m [0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/figure.py[0m in [0;36mdraw[0;34m(self, renderer)[0m [1;32m 1142[0m [0;34m[0m[0m [1;32m 1143[0m mimage._draw_list_compositing_images( [0;32m-> 1144[0;31m renderer, self, dsu, self.suppressComposite) [0m[1;32m 1145[0m [0;34m[0m[0m [1;32m 1146[0m [0mrenderer[0m[0;34m.[0m[0mclose_group[0m[0;34m([0m[0;34m'figure'[0m[0;34m)[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/image.py[0m in [0;36m_draw_list_compositing_images[0;34m(renderer, parent, dsu, suppress_composite)[0m [1;32m 137[0m [0;32mif[0m [0mnot_composite[0m [0;32mor[0m [0;32mnot[0m [0mhas_images[0m[0;34m:[0m[0;34m[0m[0m [1;32m 138[0m [0;32mfor[0m [0mzorder[0m[0;34m,[0m [0ma[0m [0;32min[0m [0mdsu[0m[0;34m:[0m[0;34m[0m[0m [0;32m--> 139[0;31m [0ma[0m[0;34m.[0m[0mdraw[0m[0;34m([0m[0mrenderer[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 140[0m [0;32melse[0m[0;34m:[0m[0;34m[0m[0m [1;32m 141[0m [0;31m# Composite any adjacent images together[0m[0;34m[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/mpl_toolkits/mplot3d/axes3d.py[0m in [0;36mdraw[0;34m(self, renderer)[0m [1;32m 291[0m [0;34m[0m[0m [1;32m 292[0m [0;31m# Then rest[0m[0;34m[0m[0;34m[0m[0m [0;32m--> 293[0;31m [0mAxes[0m[0;34m.[0m[0mdraw[0m[0;34m([0m[0mself[0m[0;34m,[0m [0mrenderer[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 294[0m [0;34m[0m[0m [1;32m 295[0m [0;32mdef[0m [0mget_axis_position[0m[0;34m([0m[0mself[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/artist.py[0m in [0;36mdraw_wrapper[0;34m(artist, renderer, *args, **kwargs)[0m [1;32m 61[0m [0;32mdef[0m [0mdraw_wrapper[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m,[0m [0;34m*[0m[0margs[0m[0;34m,[0m [0;34m**[0m[0mkwargs[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0m [1;32m 62[0m [0mbefore[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m)[0m[0;34m[0m[0m [0;32m---> 63[0;31m [0mdraw[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m,[0m [0;34m*[0m[0margs[0m[0;34m,[0m [0;34m**[0m[0mkwargs[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 64[0m [0mafter[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m)[0m[0;34m[0m[0m [1;32m 65[0m [0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/axes/_base.py[0m in [0;36mdraw[0;34m(self, renderer, inframe)[0m [1;32m 2424[0m [0mrenderer[0m[0;34m.[0m[0mstop_rasterizing[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0m [1;32m 2425[0m [0;34m[0m[0m [0;32m-> 2426[0;31m [0mmimage[0m[0;34m.[0m[0m_draw_list_compositing_images[0m[0;34m([0m[0mrenderer[0m[0;34m,[0m [0mself[0m[0;34m,[0m [0mdsu[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 2427[0m [0;34m[0m[0m [1;32m 2428[0m [0mrenderer[0m[0;34m.[0m[0mclose_group[0m[0;34m([0m[0;34m'axes'[0m[0;34m)[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/image.py[0m in [0;36m_draw_list_compositing_images[0;34m(renderer, parent, dsu, suppress_composite)[0m [1;32m 137[0m [0;32mif[0m [0mnot_composite[0m [0;32mor[0m [0;32mnot[0m [0mhas_images[0m[0;34m:[0m[0;34m[0m[0m [1;32m 138[0m [0;32mfor[0m [0mzorder[0m[0;34m,[0m [0ma[0m [0;32min[0m [0mdsu[0m[0;34m:[0m[0;34m[0m[0m [0;32m--> 139[0;31m [0ma[0m[0;34m.[0m[0mdraw[0m[0;34m([0m[0mrenderer[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 140[0m [0;32melse[0m[0;34m:[0m[0;34m[0m[0m [1;32m 141[0m [0;31m# Composite any adjacent images together[0m[0;34m[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/mpl_toolkits/mplot3d/art3d.py[0m in [0;36mdraw[0;34m(self, renderer)[0m [1;32m 705[0m [0;34m[0m[0m [1;32m 706[0m [0;32mdef[0m [0mdraw[0m[0;34m([0m[0mself[0m[0;34m,[0m [0mrenderer[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0m [0;32m--> 707[0;31m [0;32mreturn[0m [0mCollection[0m[0;34m.[0m[0mdraw[0m[0;34m([0m[0mself[0m[0;34m,[0m [0mrenderer[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 708[0m [0;34m[0m[0m [1;32m 709[0m [0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/artist.py[0m in [0;36mdraw_wrapper[0;34m(artist, renderer, *args, **kwargs)[0m [1;32m 61[0m [0;32mdef[0m [0mdraw_wrapper[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m,[0m [0;34m*[0m[0margs[0m[0;34m,[0m [0;34m**[0m[0mkwargs[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0m [1;32m 62[0m [0mbefore[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m)[0m[0;34m[0m[0m [0;32m---> 63[0;31m [0mdraw[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m,[0m [0;34m*[0m[0margs[0m[0;34m,[0m [0;34m**[0m[0mkwargs[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 64[0m [0mafter[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m)[0m[0;34m[0m[0m [1;32m 65[0m [0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/collections.py[0m in [0;36mdraw[0;34m(self, renderer)[0m [1;32m 352[0m [0mself[0m[0;34m.[0m[0m_linewidths[0m[0;34m,[0m [0mself[0m[0;34m.[0m[0m_linestyles[0m[0;34m,[0m[0;34m[0m[0m [1;32m 353[0m [0mself[0m[0;34m.[0m[0m_antialiaseds[0m[0;34m,[0m [0mself[0m[0;34m.[0m[0m_urls[0m[0;34m,[0m[0;34m[0m[0m [0;32m--> 354[0;31m self._offset_position) [0m[1;32m 355[0m [0;34m[0m[0m [1;32m 356[0m [0mgc[0m[0;34m.[0m[0mrestore[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py[0m in [0;36mdraw_path_collection[0;34m(self, *kl, **kw)[0m [1;32m 124[0m [0;34m[0m[0m [1;32m 125[0m [0;32mdef[0m [0mdraw_path_collection[0m[0;34m([0m[0mself[0m[0;34m,[0m [0;34m*[0m[0mkl[0m[0;34m,[0m [0;34m**[0m[0mkw[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0m [0;32m--> 126[0;31m [0;32mreturn[0m [0mself[0m[0;34m.[0m[0m_renderer[0m[0;34m.[0m[0mdraw_path_collection[0m[0;34m([0m[0;34m*[0m[0mkl[0m[0;34m,[0m [0;34m**[0m[0mkw[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 127[0m [0;34m[0m[0m [1;32m 128[0m [0;32mdef[0m [0m_update_methods[0m[0;34m([0m[0mself[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/path.py[0m in [0;36mvertices[0;34m(self)[0m [1;32m 219[0m [0mself[0m[0;34m.[0m[0m_has_nonfinite[0m [0;34m=[0m [0;32mnot[0m [0mnp[0m[0;34m.[0m[0misfinite[0m[0;34m([0m[0mself[0m[0;34m.[0m[0m_vertices[0m[0;34m)[0m[0;34m.[0m[0mall[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0m [1;32m 220[0m [0;34m[0m[0m [0;32m--> 221[0;31m [0;34m@[0m[0mproperty[0m[0;34m[0m[0m [0m[1;32m 222[0m [0;32mdef[0m [0mvertices[0m[0;34m([0m[0mself[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0m [1;32m 223[0m """ [0;31mKeyboardInterrupt[0m:
In [ ]:
# # or save as gif
# save_file2d = save_file.replace('.webm', '.gif')
# anim.save(save_file2d, fps=1000, bitrate=1000, codec='gif')In [ ]:
In [94]:
fig = plt.figure(figsize=(8, 5))
ax = plt.axes(projection='3d', elev=50, azim=azim)
ax.plot_surface(x, y, z, norm=LogNorm(), rstride=1, cstride=1, edgecolor='none', alpha=.5, cmap=plt.cm.jet)
ax.plot(*minima_, problem.f(minima_).numpy(), 'r*', markersize=10)
ax.plot(*_x0, problem.f(_x0).numpy(), 'r+', markersize=10)
ax.set_title('{} function'.format(problem.f.__name__))
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
ax.set_zlabel('$z$')
ax.set_xlim((xmin, xmax))
ax.set_ylim((ymin, ymax))
anim = TrajectoryAnimation3D(*paths, zpaths=zpaths[:,:,0], labels=methods, ax=ax)
ax.legend(loc='upper right')Out [94]:
<matplotlib.legend.Legend at 0x7f8c0dc707f0>
In [ ]:
In [ ]:
# anim.to_html5_video()
save_file3d = save_file.replace('.webm', '_3d.webm')
print(save_file3d)
anim.save(save_file3d, fps=1000, bitrate=1000, codec='vp9')
# display
html="""<video {options}>
<source type="video/webm" src="{video}">
Your browser does not support the video tag.
</video>""".format(options=' '.join(['controls', 'autoplay']), video=save_file3d)
print(html)
HTML(html)videos/beales_20171115_07-18-03_3d.webm
In [ ]:
# # or save as gif
# save_file3d = save_file.replace('.webm', '_3d.gif')
# anim.save(save_file3d, fps=1000, bitrate=1000, codec='gif')In [ ]:
In [ ]:
index = np.arange(1,problem.steps+1,1)[None,:]
index= np.array([index]*zpaths.shape[0])
loss_paths = np.concatenate([index, zpaths[:,None,:,0]], 1)
loss_paths.shapeIn [91]:
fig, ax = plt.subplots(figsize=(10, 6))
ax.set_xlabel('$step$')
ax.set_ylabel('$loss$')
ax.set_title('{} function'.format(problem.f.__name__))
ax.set_xlim((0, loss_paths[:,0,:].max()))
ax.set_ylim((0, loss_paths[:,1,:].max()))
anim = TrajectoryAnimation(*loss_paths, labels=methods, ax=ax)
ax.legend(loc='upper right')Out [91]:
<matplotlib.legend.Legend at 0x7f8c2262dbe0>
In [87]:
# anim.to_html5_video()
save_file_loss = save_file.replace('.webm', '_loss.webm')
print(save_file_loss)
anim.save(save_file_loss, fps=1000, bitrate=1000, codec='vp9')
# save_file_loss = save_file.replace('.webm', '_loss.gif')
# anim.save(save_file_loss, fps=1000, bitrate=1000, codec='gif')
# display
html="""<video {options}>
<source type="video/webm" src="{video}">
Your browser does not support the video tag.
</video>""".format(options=' '.join(['controls', 'autoplay']), video=save_file_loss)
print(html)
HTML(html)Out [87]:
videos/beales_20171115_07-18-03_loss.webm <video controls autoplay> <source type="video/webm" src="videos/beales_20171115_07-18-03_loss.webm"> Your browser does not support the video tag. </video>
In [88]:
# # or save as gif
# save_file_loss = save_file.replace('.webm', '_loss.gif')
# anim.save(save_file_loss, fps=1000, bitrate=1000, codec='gif')[0;31m---------------------------------------------------------------------------[0m [0;31mKeyboardInterrupt[0m Traceback (most recent call last) [0;32m<ipython-input-88-72db69b641d1>[0m in [0;36m<module>[0;34m()[0m [1;32m 1[0m [0;31m# or save as gif[0m[0;34m[0m[0;34m[0m[0m [1;32m 2[0m [0msave_file_loss[0m [0;34m=[0m [0msave_file[0m[0;34m.[0m[0mreplace[0m[0;34m([0m[0;34m'.webm'[0m[0;34m,[0m [0;34m'_loss.gif'[0m[0;34m)[0m[0;34m[0m[0m [0;32m----> 3[0;31m [0manim[0m[0;34m.[0m[0msave[0m[0;34m([0m[0msave_file_loss[0m[0;34m,[0m [0mfps[0m[0;34m=[0m[0;36m1000[0m[0;34m,[0m [0mbitrate[0m[0;34m=[0m[0;36m1000[0m[0;34m,[0m [0mcodec[0m[0;34m=[0m[0;34m'gif'[0m[0;34m)[0m[0;34m[0m[0m [0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/animation.py[0m in [0;36msave[0;34m(self, filename, writer, fps, dpi, codec, bitrate, extra_args, metadata, extra_anim, savefig_kwargs)[0m [1;32m 1060[0m [0;32mfor[0m [0manim[0m[0;34m,[0m [0md[0m [0;32min[0m [0mzip[0m[0;34m([0m[0mall_anim[0m[0;34m,[0m [0mdata[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0m [1;32m 1061[0m [0;31m# TODO: See if turning off blit is really necessary[0m[0;34m[0m[0;34m[0m[0m [0;32m-> 1062[0;31m [0manim[0m[0;34m.[0m[0m_draw_next_frame[0m[0;34m([0m[0md[0m[0;34m,[0m [0mblit[0m[0;34m=[0m[0;32mFalse[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 1063[0m [0mwriter[0m[0;34m.[0m[0mgrab_frame[0m[0;34m([0m[0;34m**[0m[0msavefig_kwargs[0m[0;34m)[0m[0;34m[0m[0m [1;32m 1064[0m [0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/animation.py[0m in [0;36m_draw_next_frame[0;34m(self, framedata, blit)[0m [1;32m 1098[0m [0mself[0m[0;34m.[0m[0m_pre_draw[0m[0;34m([0m[0mframedata[0m[0;34m,[0m [0mblit[0m[0;34m)[0m[0;34m[0m[0m [1;32m 1099[0m [0mself[0m[0;34m.[0m[0m_draw_frame[0m[0;34m([0m[0mframedata[0m[0;34m)[0m[0;34m[0m[0m [0;32m-> 1100[0;31m [0mself[0m[0;34m.[0m[0m_post_draw[0m[0;34m([0m[0mframedata[0m[0;34m,[0m [0mblit[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 1101[0m [0;34m[0m[0m [1;32m 1102[0m [0;32mdef[0m [0m_init_draw[0m[0;34m([0m[0mself[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/animation.py[0m in [0;36m_post_draw[0;34m(self, framedata, blit)[0m [1;32m 1123[0m [0mself[0m[0;34m.[0m[0m_blit_draw[0m[0;34m([0m[0mself[0m[0;34m.[0m[0m_drawn_artists[0m[0;34m,[0m [0mself[0m[0;34m.[0m[0m_blit_cache[0m[0;34m)[0m[0;34m[0m[0m [1;32m 1124[0m [0;32melse[0m[0;34m:[0m[0;34m[0m[0m [0;32m-> 1125[0;31m [0mself[0m[0;34m.[0m[0m_fig[0m[0;34m.[0m[0mcanvas[0m[0;34m.[0m[0mdraw_idle[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 1126[0m [0;34m[0m[0m [1;32m 1127[0m [0;31m# The rest of the code in this class is to facilitate easy blitting[0m[0;34m[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/backend_bases.py[0m in [0;36mdraw_idle[0;34m(self, *args, **kwargs)[0m [1;32m 2038[0m [0;32mif[0m [0;32mnot[0m [0mself[0m[0;34m.[0m[0m_is_idle_drawing[0m[0;34m:[0m[0;34m[0m[0m [1;32m 2039[0m [0;32mwith[0m [0mself[0m[0;34m.[0m[0m_idle_draw_cntx[0m[0;34m([0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0m [0;32m-> 2040[0;31m [0mself[0m[0;34m.[0m[0mdraw[0m[0;34m([0m[0;34m*[0m[0margs[0m[0;34m,[0m [0;34m**[0m[0mkwargs[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 2041[0m [0;34m[0m[0m [1;32m 2042[0m [0;32mdef[0m [0mdraw_cursor[0m[0;34m([0m[0mself[0m[0;34m,[0m [0mevent[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py[0m in [0;36mdraw[0;34m(self)[0m [1;32m 462[0m [0;34m[0m[0m [1;32m 463[0m [0;32mtry[0m[0;34m:[0m[0;34m[0m[0m [0;32m--> 464[0;31m [0mself[0m[0;34m.[0m[0mfigure[0m[0;34m.[0m[0mdraw[0m[0;34m([0m[0mself[0m[0;34m.[0m[0mrenderer[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 465[0m [0;32mfinally[0m[0;34m:[0m[0;34m[0m[0m [1;32m 466[0m [0mRendererAgg[0m[0;34m.[0m[0mlock[0m[0;34m.[0m[0mrelease[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/artist.py[0m in [0;36mdraw_wrapper[0;34m(artist, renderer, *args, **kwargs)[0m [1;32m 61[0m [0;32mdef[0m [0mdraw_wrapper[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m,[0m [0;34m*[0m[0margs[0m[0;34m,[0m [0;34m**[0m[0mkwargs[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0m [1;32m 62[0m [0mbefore[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m)[0m[0;34m[0m[0m [0;32m---> 63[0;31m [0mdraw[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m,[0m [0;34m*[0m[0margs[0m[0;34m,[0m [0;34m**[0m[0mkwargs[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 64[0m [0mafter[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m)[0m[0;34m[0m[0m [1;32m 65[0m [0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/figure.py[0m in [0;36mdraw[0;34m(self, renderer)[0m [1;32m 1142[0m [0;34m[0m[0m [1;32m 1143[0m mimage._draw_list_compositing_images( [0;32m-> 1144[0;31m renderer, self, dsu, self.suppressComposite) [0m[1;32m 1145[0m [0;34m[0m[0m [1;32m 1146[0m [0mrenderer[0m[0;34m.[0m[0mclose_group[0m[0;34m([0m[0;34m'figure'[0m[0;34m)[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/image.py[0m in [0;36m_draw_list_compositing_images[0;34m(renderer, parent, dsu, suppress_composite)[0m [1;32m 137[0m [0;32mif[0m [0mnot_composite[0m [0;32mor[0m [0;32mnot[0m [0mhas_images[0m[0;34m:[0m[0;34m[0m[0m [1;32m 138[0m [0;32mfor[0m [0mzorder[0m[0;34m,[0m [0ma[0m [0;32min[0m [0mdsu[0m[0;34m:[0m[0;34m[0m[0m [0;32m--> 139[0;31m [0ma[0m[0;34m.[0m[0mdraw[0m[0;34m([0m[0mrenderer[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 140[0m [0;32melse[0m[0;34m:[0m[0;34m[0m[0m [1;32m 141[0m [0;31m# Composite any adjacent images together[0m[0;34m[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/artist.py[0m in [0;36mdraw_wrapper[0;34m(artist, renderer, *args, **kwargs)[0m [1;32m 61[0m [0;32mdef[0m [0mdraw_wrapper[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m,[0m [0;34m*[0m[0margs[0m[0;34m,[0m [0;34m**[0m[0mkwargs[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0m [1;32m 62[0m [0mbefore[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m)[0m[0;34m[0m[0m [0;32m---> 63[0;31m [0mdraw[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m,[0m [0;34m*[0m[0margs[0m[0;34m,[0m [0;34m**[0m[0mkwargs[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 64[0m [0mafter[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m)[0m[0;34m[0m[0m [1;32m 65[0m [0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/axes/_base.py[0m in [0;36mdraw[0;34m(self, renderer, inframe)[0m [1;32m 2424[0m [0mrenderer[0m[0;34m.[0m[0mstop_rasterizing[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0m [1;32m 2425[0m [0;34m[0m[0m [0;32m-> 2426[0;31m [0mmimage[0m[0;34m.[0m[0m_draw_list_compositing_images[0m[0;34m([0m[0mrenderer[0m[0;34m,[0m [0mself[0m[0;34m,[0m [0mdsu[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 2427[0m [0;34m[0m[0m [1;32m 2428[0m [0mrenderer[0m[0;34m.[0m[0mclose_group[0m[0;34m([0m[0;34m'axes'[0m[0;34m)[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/image.py[0m in [0;36m_draw_list_compositing_images[0;34m(renderer, parent, dsu, suppress_composite)[0m [1;32m 137[0m [0;32mif[0m [0mnot_composite[0m [0;32mor[0m [0;32mnot[0m [0mhas_images[0m[0;34m:[0m[0;34m[0m[0m [1;32m 138[0m [0;32mfor[0m [0mzorder[0m[0;34m,[0m [0ma[0m [0;32min[0m [0mdsu[0m[0;34m:[0m[0;34m[0m[0m [0;32m--> 139[0;31m [0ma[0m[0;34m.[0m[0mdraw[0m[0;34m([0m[0mrenderer[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 140[0m [0;32melse[0m[0;34m:[0m[0;34m[0m[0m [1;32m 141[0m [0;31m# Composite any adjacent images together[0m[0;34m[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/artist.py[0m in [0;36mdraw_wrapper[0;34m(artist, renderer, *args, **kwargs)[0m [1;32m 61[0m [0;32mdef[0m [0mdraw_wrapper[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m,[0m [0;34m*[0m[0margs[0m[0;34m,[0m [0;34m**[0m[0mkwargs[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0m [1;32m 62[0m [0mbefore[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m)[0m[0;34m[0m[0m [0;32m---> 63[0;31m [0mdraw[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m,[0m [0;34m*[0m[0margs[0m[0;34m,[0m [0;34m**[0m[0mkwargs[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 64[0m [0mafter[0m[0;34m([0m[0martist[0m[0;34m,[0m [0mrenderer[0m[0;34m)[0m[0;34m[0m[0m [1;32m 65[0m [0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/axis.py[0m in [0;36mdraw[0;34m(self, renderer, *args, **kwargs)[0m [1;32m 1134[0m [0mrenderer[0m[0;34m.[0m[0mopen_group[0m[0;34m([0m[0m__name__[0m[0;34m)[0m[0;34m[0m[0m [1;32m 1135[0m [0;34m[0m[0m [0;32m-> 1136[0;31m [0mticks_to_draw[0m [0;34m=[0m [0mself[0m[0;34m.[0m[0m_update_ticks[0m[0;34m([0m[0mrenderer[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 1137[0m ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw, [1;32m 1138[0m renderer) [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/axis.py[0m in [0;36m_update_ticks[0;34m(self, renderer)[0m [1;32m 967[0m [0;34m[0m[0m [1;32m 968[0m [0minterval[0m [0;34m=[0m [0mself[0m[0;34m.[0m[0mget_view_interval[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0m [0;32m--> 969[0;31m [0mtick_tups[0m [0;34m=[0m [0;34m[[0m[0mt[0m [0;32mfor[0m [0mt[0m [0;32min[0m [0mself[0m[0;34m.[0m[0miter_ticks[0m[0;34m([0m[0;34m)[0m[0;34m][0m[0;34m[0m[0m [0m[1;32m 970[0m [0;32mif[0m [0mself[0m[0;34m.[0m[0m_smart_bounds[0m[0;34m:[0m[0;34m[0m[0m [1;32m 971[0m [0;31m# handle inverted limits[0m[0;34m[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/axis.py[0m in [0;36m<listcomp>[0;34m(.0)[0m [1;32m 967[0m [0;34m[0m[0m [1;32m 968[0m [0minterval[0m [0;34m=[0m [0mself[0m[0;34m.[0m[0mget_view_interval[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0m [0;32m--> 969[0;31m [0mtick_tups[0m [0;34m=[0m [0;34m[[0m[0mt[0m [0;32mfor[0m [0mt[0m [0;32min[0m [0mself[0m[0;34m.[0m[0miter_ticks[0m[0;34m([0m[0;34m)[0m[0;34m][0m[0;34m[0m[0m [0m[1;32m 970[0m [0;32mif[0m [0mself[0m[0;34m.[0m[0m_smart_bounds[0m[0;34m:[0m[0;34m[0m[0m [1;32m 971[0m [0;31m# handle inverted limits[0m[0;34m[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/axis.py[0m in [0;36miter_ticks[0;34m(self)[0m [1;32m 910[0m [0mIterate[0m [0mthrough[0m [0mall[0m [0mof[0m [0mthe[0m [0mmajor[0m [0;32mand[0m [0mminor[0m [0mticks[0m[0;34m.[0m[0;34m[0m[0m [1;32m 911[0m """ [0;32m--> 912[0;31m [0mmajorLocs[0m [0;34m=[0m [0mself[0m[0;34m.[0m[0mmajor[0m[0;34m.[0m[0mlocator[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 913[0m [0mmajorTicks[0m [0;34m=[0m [0mself[0m[0;34m.[0m[0mget_major_ticks[0m[0;34m([0m[0mlen[0m[0;34m([0m[0mmajorLocs[0m[0;34m)[0m[0;34m)[0m[0;34m[0m[0m [1;32m 914[0m [0mself[0m[0;34m.[0m[0mmajor[0m[0;34m.[0m[0mformatter[0m[0;34m.[0m[0mset_locs[0m[0;34m([0m[0mmajorLocs[0m[0;34m)[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/ticker.py[0m in [0;36m__call__[0;34m(self)[0m [1;32m 1784[0m [0;32mdef[0m [0m__call__[0m[0;34m([0m[0mself[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0m [1;32m 1785[0m [0mvmin[0m[0;34m,[0m [0mvmax[0m [0;34m=[0m [0mself[0m[0;34m.[0m[0maxis[0m[0;34m.[0m[0mget_view_interval[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0m [0;32m-> 1786[0;31m [0;32mreturn[0m [0mself[0m[0;34m.[0m[0mtick_values[0m[0;34m([0m[0mvmin[0m[0;34m,[0m [0mvmax[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 1787[0m [0;34m[0m[0m [1;32m 1788[0m [0;32mdef[0m [0mtick_values[0m[0;34m([0m[0mself[0m[0;34m,[0m [0mvmin[0m[0;34m,[0m [0mvmax[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/ticker.py[0m in [0;36mtick_values[0;34m(self, vmin, vmax)[0m [1;32m 1792[0m vmin, vmax = mtransforms.nonsingular( [1;32m 1793[0m vmin, vmax, expander=1e-13, tiny=1e-14) [0;32m-> 1794[0;31m [0mlocs[0m [0;34m=[0m [0mself[0m[0;34m.[0m[0m_raw_ticks[0m[0;34m([0m[0mvmin[0m[0;34m,[0m [0mvmax[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 1795[0m [0;34m[0m[0m [1;32m 1796[0m [0mprune[0m [0;34m=[0m [0mself[0m[0;34m.[0m[0m_prune[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/ticker.py[0m in [0;36m_raw_ticks[0;34m(self, vmin, vmax)[0m [1;32m 1734[0m [0;32mif[0m [0mself[0m[0;34m.[0m[0m_nbins[0m [0;34m==[0m [0;34m'auto'[0m[0;34m:[0m[0;34m[0m[0m [1;32m 1735[0m [0;32mif[0m [0mself[0m[0;34m.[0m[0maxis[0m [0;32mis[0m [0;32mnot[0m [0;32mNone[0m[0;34m:[0m[0;34m[0m[0m [0;32m-> 1736[0;31m nbins = max(min(self.axis.get_tick_space(), 9), [0m[1;32m 1737[0m max(1, self._min_n_ticks - 1)) [1;32m 1738[0m [0;32melse[0m[0;34m:[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/axis.py[0m in [0;36mget_tick_space[0;34m(self)[0m [1;32m 2018[0m [0mends[0m [0;34m=[0m [0mself[0m[0;34m.[0m[0maxes[0m[0;34m.[0m[0mtransAxes[0m[0;34m.[0m[0mtransform[0m[0;34m([0m[0;34m[[0m[0;34m[[0m[0;36m0[0m[0;34m,[0m [0;36m0[0m[0;34m][0m[0;34m,[0m [0;34m[[0m[0;36m1[0m[0;34m,[0m [0;36m0[0m[0;34m][0m[0;34m][0m[0;34m)[0m[0;34m[0m[0m [1;32m 2019[0m [0mlength[0m [0;34m=[0m [0;34m([0m[0;34m([0m[0mends[0m[0;34m[[0m[0;36m1[0m[0;34m][0m[0;34m[[0m[0;36m0[0m[0;34m][0m [0;34m-[0m [0mends[0m[0;34m[[0m[0;36m0[0m[0;34m][0m[0;34m[[0m[0;36m0[0m[0;34m][0m[0;34m)[0m [0;34m/[0m [0mself[0m[0;34m.[0m[0maxes[0m[0;34m.[0m[0mfigure[0m[0;34m.[0m[0mdpi[0m[0;34m)[0m [0;34m*[0m [0;36m72.0[0m[0;34m[0m[0m [0;32m-> 2020[0;31m [0mtick[0m [0;34m=[0m [0mself[0m[0;34m.[0m[0m_get_tick[0m[0;34m([0m[0;32mTrue[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 2021[0m [0;31m# There is a heuristic here that the aspect ratio of tick text[0m[0;34m[0m[0;34m[0m[0m [1;32m 2022[0m [0;31m# is no more than 3:1[0m[0;34m[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/axis.py[0m in [0;36m_get_tick[0;34m(self, major)[0m [1;32m 1727[0m [0;32melse[0m[0;34m:[0m[0;34m[0m[0m [1;32m 1728[0m [0mtick_kw[0m [0;34m=[0m [0mself[0m[0;34m.[0m[0m_minor_tick_kw[0m[0;34m[0m[0m [0;32m-> 1729[0;31m [0;32mreturn[0m [0mXTick[0m[0;34m([0m[0mself[0m[0;34m.[0m[0maxes[0m[0;34m,[0m [0;36m0[0m[0;34m,[0m [0;34m''[0m[0;34m,[0m [0mmajor[0m[0;34m=[0m[0mmajor[0m[0;34m,[0m [0;34m**[0m[0mtick_kw[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 1730[0m [0;34m[0m[0m [1;32m 1731[0m [0;32mdef[0m [0m_get_label[0m[0;34m([0m[0mself[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/axis.py[0m in [0;36m__init__[0;34m(self, axes, loc, label, size, width, color, tickdir, pad, labelsize, labelcolor, zorder, gridOn, tick1On, tick2On, label1On, label2On, major)[0m [1;32m 149[0m [0;34m[0m[0m [1;32m 150[0m [0mself[0m[0;34m.[0m[0mtick1line[0m [0;34m=[0m [0mself[0m[0;34m.[0m[0m_get_tick1line[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0m [0;32m--> 151[0;31m [0mself[0m[0;34m.[0m[0mtick2line[0m [0;34m=[0m [0mself[0m[0;34m.[0m[0m_get_tick2line[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 152[0m [0mself[0m[0;34m.[0m[0mgridline[0m [0;34m=[0m [0mself[0m[0;34m.[0m[0m_get_gridline[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0m [1;32m 153[0m [0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/axis.py[0m in [0;36m_get_tick2line[0;34m(self)[0m [1;32m 434[0m zorder=self._zorder) [1;32m 435[0m [0;34m[0m[0m [0;32m--> 436[0;31m [0ml[0m[0;34m.[0m[0mset_transform[0m[0;34m([0m[0mself[0m[0;34m.[0m[0maxes[0m[0;34m.[0m[0mget_xaxis_transform[0m[0;34m([0m[0mwhich[0m[0;34m=[0m[0;34m'tick2'[0m[0;34m)[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 437[0m [0mself[0m[0;34m.[0m[0m_set_artist_props[0m[0;34m([0m[0ml[0m[0;34m)[0m[0;34m[0m[0m [1;32m 438[0m [0;32mreturn[0m [0ml[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/axes/_base.py[0m in [0;36mget_xaxis_transform[0;34m(self, which)[0m [1;32m 707[0m [0;32melif[0m [0mwhich[0m [0;34m==[0m [0;34m'tick2'[0m[0;34m:[0m[0;34m[0m[0m [1;32m 708[0m [0;31m# for cartesian projection, this is top spine[0m[0;34m[0m[0;34m[0m[0m [0;32m--> 709[0;31m [0;32mreturn[0m [0mself[0m[0;34m.[0m[0mspines[0m[0;34m[[0m[0;34m'top'[0m[0;34m][0m[0;34m.[0m[0mget_spine_transform[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 710[0m [0;32melse[0m[0;34m:[0m[0;34m[0m[0m [1;32m 711[0m [0;32mraise[0m [0mValueError[0m[0;34m([0m[0;34m'unknown value for which'[0m[0;34m)[0m[0;34m[0m[0m [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/spines.py[0m in [0;36mget_spine_transform[0;34m(self)[0m [1;32m 425[0m [0mbase_transform[0m [0;34m=[0m [0mself[0m[0;34m.[0m[0maxes[0m[0;34m.[0m[0mget_yaxis_transform[0m[0;34m([0m[0mwhich[0m[0;34m=[0m[0;34m'grid'[0m[0;34m)[0m[0;34m[0m[0m [1;32m 426[0m [0;32melif[0m [0mself[0m[0;34m.[0m[0mspine_type[0m [0;32min[0m [0;34m[[0m[0;34m'top'[0m[0;34m,[0m [0;34m'bottom'[0m[0;34m][0m[0;34m:[0m[0;34m[0m[0m [0;32m--> 427[0;31m [0mbase_transform[0m [0;34m=[0m [0mself[0m[0;34m.[0m[0maxes[0m[0;34m.[0m[0mget_xaxis_transform[0m[0;34m([0m[0mwhich[0m[0;34m=[0m[0;34m'grid'[0m[0;34m)[0m[0;34m[0m[0m [0m[1;32m 428[0m [0;32melse[0m[0;34m:[0m[0;34m[0m[0m [1;32m 429[0m raise ValueError('unknown spine spine_type: %s' % [0;32m~/.pyenv/versions/3.6.0/envs/jupyter3/lib/python3.6/site-packages/matplotlib/artist.py[0m in [0;36maxes[0;34m(self)[0m [1;32m 240[0m [0mresides[0m [0;32min[0m[0;34m,[0m [0;32mor[0m [0;34m*[0m[0;32mNone[0m[0;34m*[0m[0;34m.[0m[0;34m[0m[0m [1;32m 241[0m """ [0;32m--> 242[0;31m [0;32mreturn[0m [0mself[0m[0;34m.[0m[0m_axes[0m[0;34m[0m[0m [0m[1;32m 243[0m [0;34m[0m[0m [1;32m 244[0m [0;34m@[0m[0maxes[0m[0;34m.[0m[0msetter[0m[0;34m[0m[0m [0;31mKeyboardInterrupt[0m:
In [ ]: