Skip to content

Instantly share code, notes, and snippets.

@hartikainen
Created July 22, 2019 04:57
Show Gist options
  • Save hartikainen/28880433e5e55c94f814af8fc15b9114 to your computer and use it in GitHub Desktop.
Save hartikainen/28880433e5e55c94f814af8fc15b9114 to your computer and use it in GitHub Desktop.
Distance website figures
import numpy as np
import matplotlib
matplotlib.use('TKAgg')
from matplotlib import pyplot as plt
from matplotlib import animation
from .data import WALLS, TRAJECTORIES
# First set up the figure, the axis, and the plot element we want to animate
figure = plt.figure(figsize=(18, 6))
axis = plt.axes(xlim=(-4.2, 4.2), ylim=(-4.2, 4.2))
axis.set_aspect('equal', 'box')
trajectory_lines = [
axis.plot([], [], lw=2, marker='o', markersize=5)[0]
for trajectory in TRAJECTORIES
]
# line = axis.plot([], [], lw=2)[0]
wall_lines = [
axis.plot(*zip(*wall), lw=8, color='black')[0]
for wall in WALLS
]
# initialization function: plot the background of each frame
def initialize_animation():
return (*trajectory_lines, *wall_lines)
# animation function. This is called sequentially
def animate(i):
step_budget = i
for j, (trajectory, trajectory_line) in enumerate(zip(
TRAJECTORIES, trajectory_lines)):
visible_data = trajectory[:min(step_budget, len(trajectory))]
x, y = (zip(*visible_data)
if visible_data
else ((), ()))
trajectory_line.set_data(x, y)
step_budget -= len(visible_data)
return (*trajectory_lines, *wall_lines)
# call the animator. blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(
figure,
animate,
init_func=initialize_animation,
frames=sum(map(len, TRAJECTORIES)) + 6,
interval=1000,
blit=False)
plt.axis('off')
figure.tight_layout()
# anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
anim.save('animation.gif', fps=10, writer='imagemagick')
# plt.show()
import numpy as np
import matplotlib
matplotlib.use('TKAgg')
from matplotlib import pyplot as plt
from matplotlib import animation
from .data import WALLS, TRAJECTORIES
# First set up the figure, the axis, and the plot element we want to animate
# figure = plt.figure()
figure, axes = plt.subplots(1, 2)
for axis in axes:
axis.set_aspect('equal', 'box')
axis.set_axis_off()
axis.set_xlim((-4.2, 4.2))
axis.set_ylim((-4.2, 4.2))
trajectory_lines = [
axes[0].plot(*zip(*trajectory), lw=2, marker='o', markersize=5)[0]
for trajectory in TRAJECTORIES
]
# line = axis.plot([], [], lw=2)[0]
wall_lines = [
axis.plot(*zip(*wall), lw=8, color='black')[0]
for axis in axes
for wall in WALLS
]
axes[1].plot(*TRAJECTORIES[0][-1], marker='*', markersize=25)
figure.tight_layout()
# anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
# anim.save('animation.gif', fps=10, writer='imagemagick')
plt.savefig('choose_goal.pdf')
WALLS = (
((-4, 4), (-4, -4)),
((-4, -4), (4, -4)),
((4, -4), (4, 4)),
((4, 4), (-2, 4)),
((0, 4), (0, 0)),
((-4, 2), (-2, 2)),
((-2, 2), (-2, -2)),
((-2, -2), (2, -2)),
((2, -2), (2, 2)),
)
TRAJECTORIES = (
(
(3.0, -3.0),
(3.0, -2.1),
(2.8, -1.4),
(3.1, -0.5),
(2.45, 0.3),
(2.3, 0.98),
(2.65, 1.8),
(2.6, 2.1),
(2.2, 2.4),
(1.9, 2.44),
(1.4, 1.7),
(1.34, 1.4),
(1.54, 1.2),
(1.6, 0.9),
(1.55, 0.3)
),
(
(3, -3),
(1.9, -2.9),
(1.3, -3.0),
(0.6, -3.0),
(-0.7, -3.0),
(-1.4, -3.2),
(-2.3, -3.1),
(-3.3, -2.9),
(-3.9, -1.4),
(-3.7, -0.4),
(-3.3, -0.2),
(-3, -0.5),
(-2.85, -1),
(-2.8, -1.55),
(-2.5, -1.89)
),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment