Re: [問題] 請教色塊顏色隨時間變化的動畫圖

作者: nicha115 (cha)   2019-07-08 22:08:10
我目前大致上是寫出可以用的了
可是現在有一個問題
就是跑的速度很慢.....
想請問是我coding太差,還是matplotlib的動畫本來就會這麼慢
代碼如下:
# 先創fig
fig,ax = plt.subplots()
# 六個足壓圓心
plantar_c =
np.array([(0.5,0.8),(0.2,0.75),(0.375,0.3),(0.85,0.8),(1.15,0.75),(0.975,0.3)])
# 先隨機生成data
data = np.random.rand(600).reshape(100,6)
data = data
def init():
for n in plantar_c:
circle = mpatches.Circle(n, 0.05, color = (0.9,0.9,0.9))
ax.add_patch(circle)
ax.axis('equal')
ax.axis('off')
return ax
def animate(i):
data_i = data.iloc[i,:]
for n in range(6):
c = data_i[n]
center = plantar_c[n]
circle = mpatches.Circle(center, 0.05, color=(1-c,1-c,1-c))
ax.add_patch(circle)
left = (plantar_c[0] * data_i[0] + plantar_c[1] * data_i[1] +
plantar_c[2] * data_i[2]) / (data_i[0]+data_i[1]+data_i[2])
right = (plantar_c[3] * data_i[3] + plantar_c[4] * data_i[4] +
plantar_c[5] * data_i[5]) / (data_i[3]+data_i[4]+data_i[5])
ax.scatter(*zip(left,right), c='lightgray')
ax.axis('equal')
ax.axis('off')
return ax
ani = animation.FuncAnimation(fig, animate, frames=len(data),
init_func=init, blit=False, interval=100)
ani.save(f'{filename}_ani.mp4', fps=15)
※ 引述《Hsins (迅雷不及掩耳盜鈴)》之銘言:
: ※ 引述《nicha115 (cha)》之銘言:
: : 謝謝H大的解說,
: : 我完成了一半
: : 但我現在卡在我要如何將array數據轉換成顏色?
: : 例如我生成一個np.random.randn(60).reshape(10,6)
: : 然後我希望每個數據依其數字大小對應一個顏色(例如從白色到深藍色)
: : 這樣要如何做呢?
: : 謝謝~~
: https://matplotlib.org/2.1.1/api/_as_gen/matplotlib.pyplot.plot.html
: In addition, you can specify colors in many weird and wonderful ways,
: including full names ('green'), hex strings ('#008000'), RGB or RGBA tuples
: ((0,1,0,1)) or grayscale intensities as a string ('0.8'). Of these, the
: string specifications can be used in place of a fmt group, but the tuple
: forms can be used only as kwargs.
: 從文件你可以知道要使用色彩可以直接給定色彩名稱,或者是使用 HEX 字串
: 或者是使用 RGB 色碼。有兩種方式你可以考慮:
: 1. 去查一下 RGB 怎麼處理顏色的深淺,讓你的數據依比例轉換成 0 - 256
: 設置為變數在 RGB 傳入
: 2. 如果數據不多,你可以一個數據對應一個顏色,透過 dict 寫成 key-value pair
: 傳入,不過這很糞喇,建議第一種
作者: nicha115 (cha)   2019-07-08 22:09:00
跑一個600比更新的資料約10分鐘
作者: GeoMeTric ( )   2019-07-08 23:08:00
把舊的 circle patch 從 axes 從移除(remove method)。
作者: nicha115 (cha)   2019-07-08 23:35:00
謝謝~~

Links booklink

Contact Us: admin [ a t ] ucptt.com