与geopandas的动画网格计数

发布于 2025-02-03 09:41:29 字数 2073 浏览 1 评论 0原文

我正在尝试将以下代码合并到动画中。下面的数据是一个可行的示例。我正在与时间戳一起工作日期和时间。

我目前将数量绘制为热图。我希望使用时间戳列随着时间的推移显示此计数的变化。

gdf_area = gpd.read_file('https://raw.githubusercontent.com/openpolis/geojson-italy/master/geojson/limits_IT_municipalities.geojson')#.loc[:, ["geometry"]]

BOXES = 50
a, b, c, d = gdf_area.total_bounds

gdf_grid = gpd.GeoDataFrame(
    geometry=[
        shapely.geometry.box(minx, miny, maxx, maxy)
        for minx, maxx in zip(np.linspace(a, c, BOXES), np.linspace(a, c, BOXES)[1:])
        for miny, maxy in zip(np.linspace(b, d, BOXES), np.linspace(b, d, BOXES)[1:])
    ],
    crs="epsg:4326",
)

gdf_grid.insert(0, 'timestamp', range(0, 0 + len(gdf_grid)))

# total area for the grid
xmin, ymin, xmax, ymax = gdf_grid.total_bounds

# how many cells across and down
n_cells = 30
cell_size = (xmax-xmin)/n_cells

# create the cells in a loop
grid_cells = []
for x0 in np.arange(xmin, xmax+cell_size, cell_size ):
        for y0 in np.arange(ymin, ymax+cell_size, cell_size):
            # bounds
            x1 = x0-cell_size
            y1 = y0+cell_size
            grid_cells.append(shapely.geometry.box(x0, y0, x1, y1))
        
cell = gpd.GeoDataFrame(grid_cells, columns = ['geometry'], crs = 'EPSG:4326')

merged = gpd.sjoin(gdf_grid, cell, how='left', predicate='within')

# make a simple count variable that we can sum
merged['gridCount'] = 1

# Compute stats per grid cell -- aggregate fires to grid cells with dissolve
dissolve = merged.dissolve(by = 'index_right', aggfunc = 'count')

# put this into cell
cell.loc[dissolve.index, 'gridCount'] = dissolve['gridCount'].values

fig, ax = plt.subplots()

im = cell.plot(column = 'gridCount', 
               cmap = 'viridis_r', 
               #vmax = 1000,
               alpha = 0.5,
               edgecolor = 'grey')

def animate(data):

    im.set_data(data)

    return im

anim = animation.FuncAnimation(fig, animate, frames = 10,
                               interval=50)

输出:

AttributeError: 'AxesSubplot' object has no attribute 'set_data'

I'm trying to incorporate the following code into an animation. The data below is a workable example. I'm working with has both date and time as the timestamp.

I'm currently plotting count as a heat map. I'm hoping to use the timestamp column to display the variation of this count over time.

gdf_area = gpd.read_file('https://raw.githubusercontent.com/openpolis/geojson-italy/master/geojson/limits_IT_municipalities.geojson')#.loc[:, ["geometry"]]

BOXES = 50
a, b, c, d = gdf_area.total_bounds

gdf_grid = gpd.GeoDataFrame(
    geometry=[
        shapely.geometry.box(minx, miny, maxx, maxy)
        for minx, maxx in zip(np.linspace(a, c, BOXES), np.linspace(a, c, BOXES)[1:])
        for miny, maxy in zip(np.linspace(b, d, BOXES), np.linspace(b, d, BOXES)[1:])
    ],
    crs="epsg:4326",
)

gdf_grid.insert(0, 'timestamp', range(0, 0 + len(gdf_grid)))

# total area for the grid
xmin, ymin, xmax, ymax = gdf_grid.total_bounds

# how many cells across and down
n_cells = 30
cell_size = (xmax-xmin)/n_cells

# create the cells in a loop
grid_cells = []
for x0 in np.arange(xmin, xmax+cell_size, cell_size ):
        for y0 in np.arange(ymin, ymax+cell_size, cell_size):
            # bounds
            x1 = x0-cell_size
            y1 = y0+cell_size
            grid_cells.append(shapely.geometry.box(x0, y0, x1, y1))
        
cell = gpd.GeoDataFrame(grid_cells, columns = ['geometry'], crs = 'EPSG:4326')

merged = gpd.sjoin(gdf_grid, cell, how='left', predicate='within')

# make a simple count variable that we can sum
merged['gridCount'] = 1

# Compute stats per grid cell -- aggregate fires to grid cells with dissolve
dissolve = merged.dissolve(by = 'index_right', aggfunc = 'count')

# put this into cell
cell.loc[dissolve.index, 'gridCount'] = dissolve['gridCount'].values

fig, ax = plt.subplots()

im = cell.plot(column = 'gridCount', 
               cmap = 'viridis_r', 
               #vmax = 1000,
               alpha = 0.5,
               edgecolor = 'grey')

def animate(data):

    im.set_data(data)

    return im

anim = animation.FuncAnimation(fig, animate, frames = 10,
                               interval=50)

Output:

AttributeError: 'AxesSubplot' object has no attribute 'set_data'

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

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

发布评论

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

评论(1

感性不性感 2025-02-10 09:41:29

我不确定时间戳,但是对于动画功能,您可以使用cell.plot()使用ax axe 参数设置为ax ax 。以下创建了一个动画GIF,其随机生成GridCount值。您可以根据data Animate 函数的参数生成gridCount值生成GridCount值。

from random import randint

import geopandas as gpd
import matplotlib.pyplot as plt
from matplotlib import animation
import numpy as np
import shapely


gdf_area = gpd.read_file('https://raw.githubusercontent.com/openpolis/geojson-italy/master/geojson/limits_IT_municipalities.geojson')

BOXES = 50
a, b, c, d = gdf_area.total_bounds

gdf_grid = gpd.GeoDataFrame(
    geometry=[
        shapely.geometry.box(minx, miny, maxx, maxy)
        for minx, maxx in zip(np.linspace(a, c, BOXES), np.linspace(a, c, BOXES)[1:])
        for miny, maxy in zip(np.linspace(b, d, BOXES), np.linspace(b, d, BOXES)[1:])
    ],
    crs="epsg:4326",
)

# total area for the grid
xmin, ymin, xmax, ymax = gdf_grid.total_bounds

# how many cells across and down
n_cells = 30
cell_size = (xmax-xmin)/n_cells

# create the cells in a loop
grid_cells = []
for x0 in np.arange(xmin, xmax+cell_size, cell_size ):
        for y0 in np.arange(ymin, ymax+cell_size, cell_size):
            # bounds
            x1 = x0-cell_size
            y1 = y0+cell_size
            grid_cells.append(shapely.geometry.box(x0, y0, x1, y1))
cell = gpd.GeoDataFrame(grid_cells, columns = ['geometry'], crs = 'EPSG:4326')


fig = plt.figure()
ax = plt.axes()

def animate(data):
    cell['gridCount'] = [randint(1,10) for _ in range(0, len(cell))] 
    im = cell.plot(
        'gridCount', 
        cmap = 'viridis_r',         
        alpha = 0.5,
        edgecolor = 'grey',
        ax=ax,
    )
    return im

anim = animation.FuncAnimation(fig, animate,  frames = 10, interval=50)
anim.save('map.gif', writer='imagemagick')

输出:

I am not sure about the timestamp, but for the animate function you could call cell.plot() with the ax parameter set to your ax. The following created a animated gif with randomly generated gridCount values. You can adapt this code to generate the gridCount values based on the data parameter of the animate function.

from random import randint

import geopandas as gpd
import matplotlib.pyplot as plt
from matplotlib import animation
import numpy as np
import shapely


gdf_area = gpd.read_file('https://raw.githubusercontent.com/openpolis/geojson-italy/master/geojson/limits_IT_municipalities.geojson')

BOXES = 50
a, b, c, d = gdf_area.total_bounds

gdf_grid = gpd.GeoDataFrame(
    geometry=[
        shapely.geometry.box(minx, miny, maxx, maxy)
        for minx, maxx in zip(np.linspace(a, c, BOXES), np.linspace(a, c, BOXES)[1:])
        for miny, maxy in zip(np.linspace(b, d, BOXES), np.linspace(b, d, BOXES)[1:])
    ],
    crs="epsg:4326",
)

# total area for the grid
xmin, ymin, xmax, ymax = gdf_grid.total_bounds

# how many cells across and down
n_cells = 30
cell_size = (xmax-xmin)/n_cells

# create the cells in a loop
grid_cells = []
for x0 in np.arange(xmin, xmax+cell_size, cell_size ):
        for y0 in np.arange(ymin, ymax+cell_size, cell_size):
            # bounds
            x1 = x0-cell_size
            y1 = y0+cell_size
            grid_cells.append(shapely.geometry.box(x0, y0, x1, y1))
cell = gpd.GeoDataFrame(grid_cells, columns = ['geometry'], crs = 'EPSG:4326')


fig = plt.figure()
ax = plt.axes()

def animate(data):
    cell['gridCount'] = [randint(1,10) for _ in range(0, len(cell))] 
    im = cell.plot(
        'gridCount', 
        cmap = 'viridis_r',         
        alpha = 0.5,
        edgecolor = 'grey',
        ax=ax,
    )
    return im

anim = animation.FuncAnimation(fig, animate,  frames = 10, interval=50)
anim.save('map.gif', writer='imagemagick')

Output:

random map animation

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