如何制作动画(或动画gif),从许多地理图中

发布于 2025-02-13 21:35:47 字数 2013 浏览 0 评论 0原文

我有一个GeodataFrame(“ MHG”),其中索引为月份(即“ 2019-01-01”,“ 2019-02-01”,...),GDF具有一列,是某些几何形状区域(即多边形(...)),最后是另一个与该月相对应的种群的列。

可以通过以下方式创建示例数据(带有两个月的ONYL):

import geopandas as gpd 
data = [['2019-01-01', 'POLYGON(123...)', 1000], ['2019-01-01', 'POLYGON(456...)', 1500], ['2019-01-01', 'POLYGON(789...)', 1400], ['2019-02-01', 'POLYGON(123...)', 1100], ['2019-02-01', 'POLYGON(456...)', 1600], ['2019-02-01', 'POLYGON(789...)', 1300]]
mhg = gpd.GeoDataFrame(data, columns=['month','geometry', 'population'])
mhg.set_index('month')

我可以通过以下方式创建一个居住在每个区域(所有时期)的用户的多色绘图:

mhg.plot(column='population',cmap='jet')

我可以进行相同的操作,但是按月进行过滤,使用:

mhg.loc['2019-01-01'].plot(column='population',cmap='jet')

我想:通过使用这种伪代码,获得某种“动画”或动画gif,我可以看到人口的时间演变:

for all the plots in
    mhg.loc['2019-01-01'].plot(column='population',cmap='jet')
    mhg.loc['2019-02-01'].plot(column='population',cmap='jet')
    mhg.loc['2019-03-01'].plot(column='population',cmap='jet')
    ...
then merge all plots into 1 animated gif

但是我不知道该怎么做:几个月的数量可以超过数百个,我不要怎么如何制作for循环,我什至不知道如何开始...

编辑

:我尝试了以下内容/en/c089c549df4d4a6d815c/“ rel =” nofollow noreferrer“> https://linuxtut.com/en/c089c549df4d4a6d815c/ ):

months = np.sort(np.unique(mhg.month.values))
from matplotlib.animation import FuncAnimation
from matplotlib.animation import PillowWriter
fig, ax = plt.subplots()
ims = []
def update_fig(month):
    if len(ims) > 0:
        ims[0].remove()
        del ims[0]
    geos = mhg['geometry'].values
    users = mhg[(mhg.month==month)].population
    apl = gpd.plotting.plot_polygon_collection(ax, geos, population, True, cmap='jet')
    ims.append(apl)
    ax.set_title('month = ' + str(month))
    return ims
anim = FuncAnimation(fig, update_fig, interval=1000, repeat_delay=3000, frames=months)
plt.show()

但是我得到了一个用户浏览:动画被删除而没有渲染任何内容...

所以我再次被固定了。

I have a Geodataframe ("mhg") in which the index are months (i.e. "2019-01-01", "2019-02-01", ...), and the GDF have a column that is the geometry of certain regions (i.e. POLYGON(...)), and finally another column that is the population corresponding to that geometry at that month.

sample data (with onyl two months) could be created by:

import geopandas as gpd 
data = [['2019-01-01', 'POLYGON(123...)', 1000], ['2019-01-01', 'POLYGON(456...)', 1500], ['2019-01-01', 'POLYGON(789...)', 1400], ['2019-02-01', 'POLYGON(123...)', 1100], ['2019-02-01', 'POLYGON(456...)', 1600], ['2019-02-01', 'POLYGON(789...)', 1300]]
mhg = gpd.GeoDataFrame(data, columns=['month','geometry', 'population'])
mhg.set_index('month')

I can make a multicolor plot of the users living in each region (all periods) with:

mhg.plot(column='population',cmap='jet')

and I can make the same, but filtering by month, using:

mhg.loc['2019-01-01'].plot(column='population',cmap='jet')

I would like to get some kind of ""animation" or animated gif where I can see the temporal evolution of the population, by using this kind of pseudocode:

for all the plots in
    mhg.loc['2019-01-01'].plot(column='population',cmap='jet')
    mhg.loc['2019-02-01'].plot(column='population',cmap='jet')
    mhg.loc['2019-03-01'].plot(column='population',cmap='jet')
    ...
then merge all plots into 1 animated gif

But I dont' know how to do it: the number of months can be up to hundreds, I don't how how to make the for loop, and I don't know even how to start...

Any suggestions?

EDIT: I tried the following (following https://linuxtut.com/en/c089c549df4d4a6d815c/):

months = np.sort(np.unique(mhg.month.values))
from matplotlib.animation import FuncAnimation
from matplotlib.animation import PillowWriter
fig, ax = plt.subplots()
ims = []
def update_fig(month):
    if len(ims) > 0:
        ims[0].remove()
        del ims[0]
    geos = mhg['geometry'].values
    users = mhg[(mhg.month==month)].population
    apl = gpd.plotting.plot_polygon_collection(ax, geos, population, True, cmap='jet')
    ims.append(apl)
    ax.set_title('month = ' + str(month))
    return ims
anim = FuncAnimation(fig, update_fig, interval=1000, repeat_delay=3000, frames=months)
plt.show()

But I got a UserWarning: animation was deleted without rendering anything...

So I am stuck again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

窗影残 2025-02-20 21:35:47

我设法这样做:

mhg = mhg.reset_index()
groups = mhg.groupby('month')
for month, grp in groups:                      
    grp.plot(column='users',cmap='jet',legend=True,figsize=(10, 10),norm=matplotlib.colors.LogNorm(vmin=mhg.users.min(), vmax=mhg.users.max()))
    plt.title({month})
    plt.xlim([-20, 5])
    plt.ylim([25, 45])
    plt.savefig("plot{month}.png".format(month=month), facecolor='white')
    plt.close()

然后我加入了所有png的 convert> convert (ImageMagick工具):

convert -delay 50 -loop 0 *.png aniamtion.gif

I managed to do it this way:

mhg = mhg.reset_index()
groups = mhg.groupby('month')
for month, grp in groups:                      
    grp.plot(column='users',cmap='jet',legend=True,figsize=(10, 10),norm=matplotlib.colors.LogNorm(vmin=mhg.users.min(), vmax=mhg.users.max()))
    plt.title({month})
    plt.xlim([-20, 5])
    plt.ylim([25, 45])
    plt.savefig("plot{month}.png".format(month=month), facecolor='white')
    plt.close()

And then I joined all the png's with convert (imagemagick tool):

convert -delay 50 -loop 0 *.png aniamtion.gif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文