我正在使用matplotlib.Basemap
和matplotlib.Animation
绘制位置数据。最多有point_number
个点,逐渐引入点,然后通过在数据中存在或不存在来删除。
即
day 1
--------
1 some-lon some-lat
2 some-lon some-lat
day 2
--------
1 some-lon some-lat
2 some-lon some-lat
3 some-lon some-lat
day 3
--------
2 some-lon some-lat
一切都很好,直到点数开始减少(如第3天),此时绘制的点数(等于第3天不再存在的点数)在地图上保持冻结状态。
这似乎是由于init()
例行程序无法正常工作。当我将每个点设置为等于空对象时,我似乎无法弄清楚它为什么会这样做。
init()函数:
def init():
for pt in pts_dots:
pt.set_data([], [])
return pts_dots
animation()函数:
def animate(i):
# filter out daily data from pandas dataframe
lons = data.lons[data.daynr==dates[i]].values
lats = data.lats[data.daynr==dates[i]].values
for z in range(len(lons)):
x, y = m(lons[z],lats[z])
pts_dots[z].set_data(x, y)
# attempting to "empty" objects where values are absent
for z in range(point_number-len(lons)):
pts_dots[z].set_data([],[])
return pts_dots
动画电话:
pts_dots = [[] for i in range(point_number)] # Animation point global list
for i in range(point_number):
pts_dots[i] = m.plot(x,y, 'ro', markersize=5)[0]
anim = animation.FuncAnimation(plt.gcf(), animate,
init_func=init, frames=f_num,
interval=animate_range, blit=True)
知道这里可能会发生什么吗?谢谢!
数据摘要:
+------+-------+----------+-------------+-------------+
| | daynr | point_id | lons | lats |
+------+-------+----------+-------------+-------------+
| 171 | 7.0 | 2 | -4.45366398 | 60.72650532 |
| 241 | 8.0 | 2 | -4.00647587 | 61.01281524 |
| 242 | 8.0 | 3 | -4.45183701 | 60.71743553 |
| 243 | 8.0 | 5 | -7.92443966 | 61.34647079 |
| 325 | 9.0 | 2 | -3.55301 | 61.30035716 |
| 326 | 9.0 | 3 | -3.99834681 | 61.00068353 |
| 327 | 9.0 | 4 | -4.44165093 | 60.72803087 |
| 328 | 9.0 | 5 | -7.41926307 | 61.61805212 |
| 412 | 10.0 | 1 | -7.90065375 | 61.34230534 |
| 413 | 10.0 | 2 | -3.09927833 | 61.5869605 |
| 414 | 10.0 | 3 | -3.54755914 | 61.28483512 |
| 415 | 10.0 | 4 | -3.99194122 | 61.01493663 |
| 416 | 10.0 | 5 | -5.82052964 | 62.12547394 |
| 504 | 11.0 | 1 | -7.40200065 | 61.61242493 |
| 505 | 11.0 | 2 | -2.63697802 | 61.87711064 |
| 506 | 11.0 | 3 | -3.08461278 | 61.57107991 |
| 507 | 11.0 | 4 | -3.53868532 | 61.2991389 |
| 508 | 11.0 | 5 | -5.30487041 | 62.3976168 |
| 597 | 12.0 | 1 | -6.60641348 | 62.26026119 |
| 598 | 12.0 | 2 | -2.17446179 | 62.16981877 |
| 599 | 12.0 | 3 | -2.62998793 | 61.86510687 |
| 600 | 12.0 | 4 | -3.08032268 | 61.58919812 |
| 601 | 12.0 | 5 | -4.79651341 | 62.67039254 |
| 694 | 13.0 | 1 | -6.08703929 | 62.53172291 |
| 695 | 13.0 | 2 | -1.7127866 | 62.46318164 |
| 696 | 13.0 | 3 | -2.17614685 | 62.15537983 |
| 697 | 13.0 | 4 | -2.62316597 | 61.88067335 |
| 698 | 13.0 | 5 | -4.28827737 | 62.94939414 |
| 794 | 14.0 | 1 | -5.56028452 | 62.79949887 |
| 795 | 14.0 | 2 | -1.24521666 | 62.75546024 |
| 796 | 14.0 | 3 | -1.71300992 | 62.44833943 |
| 797 | 14.0 | 4 | -2.16482898 | 62.17056137 |
| 798 | 14.0 | 5 | -3.77037748 | 63.2290821 |
| 894 | 15.0 | 1 | -5.71432399 | 62.44689183 |
| 895 | 15.0 | 2 | -0.77802644 | 63.0510241 |
| 896 | 15.0 | 3 | -1.25380314 | 62.74107774 |
| 897 | 15.0 | 4 | -1.7003722 | 62.46102404 |
| 898 | 15.0 | 5 | -3.25720983 | 63.5068827 |
| 994 | 16.0 | 1 | -5.59137769 | 62.08532967 |
| 995 | 16.0 | 2 | -0.30805077 | 63.34665516 |
| 996 | 16.0 | 3 | -0.78319223 | 63.03575662 |
| 997 | 16.0 | 4 | -1.23957285 | 62.75271172 |
| 998 | 16.0 | 5 | -2.75050842 | 63.78709738 |
| 1094 | 17.0 | 1 | -5.71709194 | 61.72990759 |
| 1095 | 17.0 | 2 | 0.15062701 | 63.64276531 |
| 1096 | 17.0 | 3 | -1.42640622 | 62.8293313 |
| 1097 | 17.0 | 4 | -0.77191719 | 63.04631626 |
| 1098 | 17.0 | 5 | -2.23675898 | 64.07140725 |
| 1194 | 18.0 | 1 | -5.84315356 | 61.37380764 |
| 1195 | 18.0 | 2 | -0.37369111 | 63.37029853 |
| 1196 | 18.0 | 3 | -1.90305652 | 62.54377504 |
| 1197 | 18.0 | 4 | -0.30628721 | 63.33942171 |
| 1198 | 18.0 | 5 | -1.7196634 | 64.355484 |
| 1294 | 19.0 | 1 | -5.2628807 | 61.1454648 |
| 1295 | 19.0 | 2 | -0.92225023 | 63.11307572 |
| 1296 | 19.0 | 3 | -2.32185799 | 62.24064625 |
| 1297 | 19.0 | 4 | 0.16220025 | 63.63782642 |
| 1298 | 19.0 | 5 | -2.27735206 | 64.08689711 |
| 1394 | 20.0 | 1 | -6.00380815 | 61.17190429 |
| 1395 | 20.0 | 2 | -1.57348486 | 62.90934753 |
| 1396 | 20.0 | 3 | -2.9061956 | 62.00581576 |
| 1397 | 20.0 | 4 | 0.63065135 | 63.9345848 |
| 1398 | 20.0 | 5 | -2.49747739 | 63.7426637 |
| 1493 | 21.0 | 1 | -6.75167242 | 61.22029862 |
| 1494 | 21.0 | 2 | -1.97949208 | 62.59848653 |
| 1495 | 21.0 | 3 | -3.56641468 | 61.82230198 |
| 1496 | 21.0 | 4 | 0.0176544 | 63.69946287 |
| 1497 | 21.0 | 5 | -2.85047365 | 63.41953451 |
| 1587 | 22.0 | 1 | -7.49038571 | 61.2681404 |
| 1588 | 22.0 | 2 | -2.56974239 | 62.35995893 |
| 1589 | 22.0 | 3 | -4.03747684 | 61.54029225 |
| 1590 | 22.0 | 4 | -0.56825407 | 63.45703496 |
| 1591 | 22.0 | 5 | -3.10192883 | 63.0782619 |
| 1679 | 23.0 | 1 | -6.78479647 | 61.14594969 |
| 1680 | 23.0 | 2 | -3.15971365 | 62.12655957 |
| 1681 | 23.0 | 3 | -4.77551934 | 61.48315198 |
| 1682 | 23.0 | 4 | -1.20257268 | 63.23739608 |
| 1683 | 23.0 | 5 | -3.35273464 | 62.73608292 |
| 1766 | 24.0 | 1 | -7.48894785 | 61.26483922 |
| 1767 | 24.0 | 2 | -3.61670316 | 61.83964718 |
| 1768 | 24.0 | 3 | -5.44135019 | 61.31067048 |
| 1769 | 24.0 | 4 | -1.68497622 | 62.95139694 |
| 1770 | 24.0 | 5 | -3.60826477 | 62.39478414 |
| 1851 | 25.0 | 2 | -4.1577626 | 61.58987649 |
| 1852 | 25.0 | 3 | -6.09732902 | 61.13961171 |
| 1853 | 25.0 | 4 | -2.36817354 | 62.77207285 |
| 1854 | 25.0 | 5 | -4.09030225 | 62.10817621 |
| 1934 | 26.0 | 1 | -6.78250548 | 61.14562163 |
| 1935 | 26.0 | 2 | -4.26932018 | 61.23393004 |
| 1936 | 26.0 | 3 | -5.46586982 | 60.95023225 |
| 1937 | 26.0 | 4 | -2.903176 | 62.50884564 |
| 1938 | 26.0 | 5 | -4.57302758 | 61.82364897 |
| 2018 | 27.0 | 1 | -7.15642824 | 61.2098756 |
| 2019 | 27.0 | 2 | -4.38717574 | 60.87630252 |
| 2020 | 27.0 | 3 | -4.77991484 | 60.80961114 |
| 2021 | 27.0 | 4 | -3.4821231 | 62.26664477 |
| 2022 | 27.0 | 5 | -4.51805801 | 61.46261821 |
| 2101 | 28.0 | 1 | -6.45087653 | 61.08953278 |
| 2102 | 28.0 | 2 | -5.10962837 | 60.95808701 |
| 2103 | 28.0 | 3 | -5.47168995 | 60.92719014 |
| 2104 | 28.0 | 4 | -3.85846896 | 61.95312546 |
| 2105 | 28.0 | 5 | -4.31627368 | 61.11404727 |
| 2181 | 29.0 | 1 | -7.15367019 | 61.20996396 |
| 2182 | 29.0 | 2 | -4.4685451 | 60.77766143 |
| 2183 | 29.0 | 3 | -5.57426471 | 60.93922674 |
| 2184 | 29.0 | 4 | -4.3348975 | 61.66888486 |
| 2185 | 29.0 | 5 | -4.96446093 | 60.93709383 |
| 2254 | 30.0 | 2 | -4.42456883 | 60.73181243 |
| 2255 | 30.0 | 4 | -4.22187879 | 61.30985271 |
| 2256 | 30.0 | 5 | -5.6922579 | 60.98634563 |
| 2322 | 31.0 | 4 | -4.96950637 | 61.314032 |
| 2323 | 31.0 | 5 | -6.39554038 | 61.09783538 |
| 2390 | 32.0 | 3 | -6.24088918 | 61.0563245 |
| 2391 | 32.0 | 4 | -4.55267037 | 61.01582872 |
| 2392 | 32.0 | 5 | -6.35170419 | 61.07596667 |
| 2454 | 33.0 | 4 | -5.27982039 | 60.95963728 |
| 2455 | 33.0 | 5 | -7.05496009 | 61.19298776 |
| 2514 | 34.0 | 1 | -6.39671804 | 61.08225977 |
| 2515 | 34.0 | 4 | -6.00036916 | 61.04043063 |
| 2516 | 34.0 | 5 | -6.34544218 | 61.07435974 |
| 2571 | 35.0 | 4 | -6.3522571 | 61.07221175 |
| 2572 | 35.0 | 5 | -6.35287665 | 61.07582546 |
| 2659 | 37.0 | 5 | -5.96382779 | 61.00587669 |
| 2758 | 42.0 | 2 | -5.09121351 | 60.85182831 |
| 2891 | 58.0 | 4 | -5.65987894 | 60.95238083 |
| 2895 | 59.0 | 4 | -5.96656035 | 61.0058948 |
| 2898 | 64.0 | 4 | -6.67680629 | 61.12872095 |
+------+-------+----------+-------------+-------------+