Hi. I write about matplotlib today.
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.
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
- Define a callback function.
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]))
- Instantiate the Figure Object.
fig = plt.figure()
- Sets the value of the X and Y.
x = [0, 50, 100]
y = [0, 0, 0]
- Instantiate the Subplot Object.
ax = fig.add_subplot(111)
- Sets a grid and a limits of the X and Y.
ax.grid(True, linestyle = '-', color = '0.75')
ax.set_xlim([-50, 200])
ax.set_ylim([-50, 200])
- Instantiate the Scatter Object.
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]))
- Sets a Alpha Channel of the points.
scat.set_alpha(0.8)
- Instantiate the FuncAnimation Object.
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
- Start the Animation.
plt.show()
Movie:
Thanks you for read my page.