I had the opportunity to use matplotlib at work, so I will summarize the usage I learned.
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4, 5])
plt.show()
The following graph is displayed. If there is only one piece of information to pass to the argument, it seems that the coordinates of the argument will be on the y-axis.
line1 = [1, 2, 3, 4, 5]
line2 = [5, 4, 3, 2, 1]
plt.plot(line1, color='red')
plt.plot(line2, color='green')
plt.show()
You can draw one line with plt.plot ()
.
Specify color
to change the line color
You can also animate to draw the line.
from matplotlib.animation import ArtistAnimation
#Or
from matplotlib.animation import FuncAnimation
There are two classes for implementing animation.
ArtistAnimation
Animation of a graph in which the value increases by 1
fig = plt.figure()
artist = []
line = []
for value in range(1, 10):
line.append(value)
im = plt.plot(line, color='red', marker='o')
artist.append(im)
anim = ArtistAnimation(fig, artist, interval=300)
plt.show()
FuncAnimation
Implementation content is the same as Artist Animation
Note that FuncAnimation defaults to repeat
when creating an object, and if the initialization function is not implemented, it will be an unintended animation after the second week.
fig = plt.figure()
line = []
def init():
global line
print("Implement initialization process")
plt.gca().cla()
line = []
def draw(i):
line.append(i)
im = plt.plot(line, color='red', marker='o')
anim = FuncAnimation(fig, func=draw, frames=10, init_func=init, interval=100)
plt.show()
Recommended Posts