in distractors/n_body_problem.py [0:0]
def animate(self, file_name=None, frames=1000, pixel_length=None, tight_format=True):
"""
Animation function for visual debugging.
"""
if self.num_dimensions is not 2:
raise NotImplementedError
if pixel_length is None:
fig = plt.figure()
else:
# matplotlib can't render if pixel_length is too small, so just run in the background id pixels specified
import matplotlib
matplotlib.use('Agg')
my_dpi = 96 # find your screen's dpi here: https://www.infobyip.com/detectmonitordpi.php
fig = plt.figure(facecolor='lightslategray', figsize=(pixel_length/my_dpi, pixel_length/my_dpi), dpi=my_dpi)
ax = fig.add_subplot(1, 1, 1)
plt.axis('off')
if tight_format:
plt.subplots_adjust(left=0, right=1, top=1, bottom=0, wspace=None, hspace=None)
body_colors = np.random.uniform(size=self.num_bodies)
def render(_):
self.step()
x = self.body_positions[:, 0]
y = self.body_positions[:, 1]
ax.clear()
# if tight_format:
# plt.subplots_adjust(left=0., right=1., top=1., bottom=0.)
ax.scatter(x, y, marker='o', c=body_colors, cmap='viridis')
# ax.set_title(self.__class__.__name__ + "\n(temperature inside box: {:.1f})".format(self.temperature))
ax.set_xlim(self.MIN_POS, self.MAX_POS)
ax.set_ylim(self.MIN_POS, self.MAX_POS)
ax.set_xticks([])
ax.set_yticks([])
ax.set_aspect('equal')
# ax.axis('off')
ax.set_facecolor('black')
if tight_format:
ax.margins(x=0., y=0.)
interval_milliseconds = 1000 * self.dt
anim = animation.FuncAnimation(fig, render, frames=frames, interval=interval_milliseconds)
plt.pause(1)
if file_name is None:
file_name = self.__class__.__name__.lower() + '.gif'
file_name = 'images/' + file_name
print('Saving file {} ...'.format(file_name))
anim.save(file_name, writer='imagemagick')
plt.close(fig)