python Matplotlibシリーズチュートリアル(9)--ダイナミックマップの描画方法(リアルタイム株価マップ=トレンドマップのようなもの)
2765 ワード
この章では、Matplotlibのアニメーション機能について、リアルタイムのグラフをどのように描くかについて説明します.
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"C:\Windows\Fonts\simhei.ttf", size=14)
# style
style.use('fivethirtyeight')
# figure , , 。
# , 。 .
# , figure
# plt.figure(1) #
# plt.subplot(211) #
# plt.plot([1,2,3])
# plt.subplot(212) #
# plt.plot([4,5,6])
# plt.figure(2) #
# plt.plot([4,5,6]) # subplot(111)
# plt.figure(1) # figure 1 ; subplot(212)
# plt.subplot(211) # subplot(211) figure1
# plt.title('Easy as 1,2,3') # subplot 211
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
def animate(i):
graph_data = open('example.txt', 'r').read()
lines = graph_data.split('
')
xs = []
ys = []
for line in lines:
if len(line) > 1:
x, y = line.split(',')
xs.append(x)
ys.append(y)
ax1.clear()
ax1.plot(xs, ys)
ani = animation.FuncAnimation(fig, animate, interval = 1000)
plt.show()