Q&A
#達成したいこと
関数上を点が動くアニメーションを作成したいです.
その際,点の動くスピードを一定にする方法を模索しています.
##現状
python
1import numpy as np 2import matplotlib.pyplot as plt 3from matplotlib import animation 4 5 6plt.rcParams['figure.figsize'] = (6,5) 7plt.rcParams['font.family'] = 'Times New Roman' 8plt.rcParams['font.size'] = 16 9 10 11def axes_set_linewidth(axes, t=2, b=2, r=1, l=1): 12 axes.spines['top'].set_linewidth(t) 13 axes.spines['bottom'].set_linewidth(b) 14 axes.spines['right'].set_linewidth(r) 15 axes.spines['left'].set_linewidth(l) 16 17#変数設定,Nは(x軸の距離) 18N = 0.1 19x = np.arange(-2 * np.pi,2 * np.pi, N) 20y = np.sin(x) 21x1 = np.arange(-2 * np.pi,2 * np.pi,N) 22y1 = np.cos(x1)*2 23x2 = np.arange(-2 * np.pi,2 * np.pi,N) 24y2 = np.cos(x1)*0.5 25 26fig, ax = plt.subplots() 27 28ax.plot(x, y, color = 'blue', label = 'sin x') #sinカーブをplot 29ax.plot(x1, y1, color = 'red', label = 'cos x*2') #cosカーブをplot 30ax.plot(x2, y2, color = 'green', label = 'cos x*0.5') 31 32axes_set_linewidth(ax, t=0, r=0, b=2, l=2) 33ax.set_xlabel('x') 34ax.set_ylabel('y') 35ax.set_ylim(-1.2, 1.4) 36ax.legend(frameon = False) 37plt.subplots_adjust(top=0.9, bottom=0.2, right=0.9, left=0.2) 38 39ims = [] 40 41for i in range(len(x1)): 42 p = ax.plot(x[i], y[i], color = 'darkblue', marker = 'o', markersize = 10) 43 q= ax.plot(x1[i], y1[i], color = 'darkblue', marker = 'o', markersize = 10) 44 r= ax.plot(x2[i], y2[i], color = 'darkblue', marker = 'o', markersize = 10) 45 ims.append(p+q+r) 46 47 48ani = animation.ArtistAnimation(fig, ims, interval=100) 49ani.save('sincos2.gif', writer='imagemagick', dpi = 300)
#問題点
上記の場合,x軸でプロットの間隔を定めているため,アニメーション時の点の動く速さがバラバラになってしまいます.
一定の速度で関数上を点が動く方法をご教示いただけますと幸いです.
(上記の場合,振幅が大きい2cosx上の点が最も遅く右端に到達するようにしたいです.)
回答2件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。