It is a Python 2D, 3D plotting library which draw high quality figures.
matplotlib
URL: http://matplotlib.org/
Summary:
This article is written about matplotlib.
I show you how to create an animation using the scatter.
In scatter, you can update the coordinates of the points by using the set_offsets method.
I show example python script.
This script is plotting three points in xy coordinates.
#!/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) 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()Flow of a process.
- Import the required method.
- Define a callback function.
- Instantiate the Figure Object.
- Sets the value of the X and Y.
- Instantiate the Subplot Object.
- Sets a grid and a limits of the X and Y.
- Instantiate the Scatter Object.
- Sets a Alpha Channel of the points.
- Instantiate the FuncAnimation Object.
- Start the Animation.
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) return scat,Note:
In instantiate scatter, value of X and Y is set [x1, x2, ..., xN], [y1, y2, ..., yN].
But in set_offsets method, you set as follows.
set_offsets(([x1, y1], [x2, y2], ...., [xN, yN]))
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)x:X-axis values.
[x1, x2, ..., xN]
y:Y-axis values.
[y1, y2, ..., yN]
c:Color
It changes according to the value of the X axis.
set_offsets(([x1, y1], [x2, y2], ...., [xN, yN]))
scat.set_alpha(0.8)
anim = animation.FuncAnimation(fig, _update_plot, fargs = (fig, scat), frames = 100, interval = 100)fig: Figure Object
_update_plot: Callback Function
fargs: Value to be passed to the callback function
frams: Number of frames
interval: Call interval of the callback function
plt.show()
Thanks you for read my page.