与geopandas的动画网格计数
我正在尝试将以下代码合并到动画中。下面的数据是一个可行的示例。我正在与时间戳一起工作日期和时间。
我目前将数量绘制为热图。我希望使用时间戳列随着时间的推移显示此计数的变化。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定时间戳,但是对于动画功能,您可以使用
cell.plot()
使用ax
axe 参数设置为ax ax
。以下创建了一个动画GIF,其随机生成GridCount
值。您可以根据data
Animate 函数的参数生成gridCount
值生成GridCount
值。输出:
I am not sure about the timestamp, but for the animate function you could call
cell.plot()
with theax
parameter set to yourax
. The following created a animated gif with randomly generatedgridCount
values. You can adapt this code to generate thegridCount
values based on thedata
parameter of theanimate
function.Output: