[How to use matplotlib] 2D Animation in XY plane using Scatter

preview_player
Показать описание
I will explain the basic usage of Scatter 2D Animation in XY plane

Рекомендации по теме
Комментарии
Автор

Here's the code. It didn't work in Jupyter (stuck on frame 0), but for grins, I just pasted it into the command line and it works just fine. Note, I'm using Python3, but there's only one line change, and it's commented out for Python2 folks:


#!/usr/bin/env/python
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np

def _update_plot(i, fig, scat):
scat.set_offsets(([0, i], [50, i], [100, i]))
#print('Frames: %d', %i) # For < Python3
print('Frames: {}'.format(i)) # For Python3

return scat

fig = plt.figure()

x = [0, 50, 100]
y = [0, 0, 0]

ax = fig.add_subplot(111)
ax.grid(True, linestyle = '-', color = '0.75')
ax.set_xlim([-50, 200])
ax.set_ylim([-50, 200])

scat = plt.scatter(x, y, c = x)
scat.set_alpha(0.8)

anim = animation.FuncAnimation(fig, _update_plot, fargs = (fig, scat),
frames = 100, interval = 100)

plt.show()

kikijewell
Автор

Btw, this is a super fast, awesome tutorial to get up and running! Thank you!

kikijewell
Автор

in the _update function, what does "return scat" do? Is it necessary?

jamintg
Автор

This is more convoluted then Java's swing etc. Why must everything in python be overcomplicated magic bs?

cherubinth