やりたいこと
matplotlibを使ってアニメーションを描画したいです。
静的なものはおおまかにできていますが、動的なものができません。
ランダムで長さ1のタテ線またはヨコ線を表示するアニメーション描画がしたいです。
左が完成イメージで、右が現状です。
書いたのに反映されてない部分もあり、めちゃくちゃになります。
エラーは出ず、動き続けます。
プログラム
Python3
1 2 3from matplotlib.figure import Figure 4import numpy as np 5import matplotlib.pyplot as plt 6import matplotlib.animation as animation 7import random 8 9fig = plt.figure(figsize=(5, 5)) 10ax = plt.gca() 11 12y_symbol = ['A','B','C','D','E'] 13 14for i in range(5): 15 for j in range(5): 16 plt.text(i + 0.5, j + 0.5, y_symbol[i] + str(j+1) , size=14, ha='center',color='gray') 17 18plt.text(0.5,0.8,'START→', size=9, ha='center',color='gray') 19 20# 描画範囲の設定と目盛りを消す設定 21ax.set_xlim(0,5) 22ax.set_ylim(5,0) # y軸を反転させる 23# x軸の目盛設定 24ax.set_xticks([0,1, 2, 3, 4, 5]) 25# y軸の目盛設定 26ax.set_yticks([0,1,2,3,4,5]) 27#目盛を丈上部に配置 28ax.xaxis.tick_top() 29#グリッドを表示 30ax.grid() 31 32plt.tick_params(axis='both', which='both', bottom='off', top='off', 33 labelbottom='off', right='off', left='off', labelleft='off') 34 35# A0に緑丸を描く 36line, = ax.plot([0.5], [0.5], marker="o", color='greenyellow', markersize=50) 37line2, = ax.plot([2.5], [2.5], marker="o", color='greenyellow', markersize=50) 38 39xy = [] 40XY = [] 41#animateに呼び出される 42def SelectData(t) : 43 #V_wall、H_wallについて 44 #[[[x,y], [X,Y]],,,],,,]となっていて 45 #点[x,y]と[X,Y]を結ぶと長さ1の直線になる 46 V_wall = [[[[i,y],[i,y+1]]for y in range(6)] for i in range(6)] #タテ線 47 H_wall = [[[[x,i],[x+1,i] ]for x in range(6)] for i in range(6)] #ヨコ線 48 49 i = random.randint(0,5) 50 j = random.randint(0,5) 51 if t == 1 : 52 update_xy = V_wall[i][j][0] 53 update_XY = V_wall[i][j][1] 54 else : 55 update_xy = H_wall[i][j][0] 56 update_XY = H_wall[i][j][1] 57 return update_xy,update_XY 58 59#animation.FuncAnimationに呼び出される 60def animate(data,x,y): 61 t=random.randint(0,1) 62 plt.cla() 63 update_xy,update_XY = SelectData(t) 64 print(update_xy ," " ,update_XY) #数値確認用 65 xy.append(update_xy) 66 XY.append(update_XY) 67 plt.plot(xy, XY, color='red', linewidth=2) 68 69ani = animation.FuncAnimation(fig, animate, fargs = (xy,XY), interval=1000) 70plt.show() 71
宜しくお願いします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/08/11 06:14