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