[尝试与matplotlib的动画模块一起使用,以创建网格变化的动画。使用的库。
import numpy as np
import geopandas as gpd
from shapely.geometry import Polygon
import matplotlib.animation as animation
import matplotlib.pyplot as plt
使用geopandas创建网格(可以使用grid.plot(edgecolor = 'white')
命令看到网格的完整图像:
xmin,ymin,xmax,ymax = -2, -2, 2, 2
length = 0.1
wide = 0.1
columns = list(np.arange(int(np.floor(xmin)), int(np.ceil(xmax)), wide))
rows = list(np.arange(int(np.floor(ymin)), int(np.ceil(ymax)), length))
rows.reverse()
grid = gpd.GeoDataFrame(columns = ['geometry'])
for row in rows:
for column in columns:
grid.loc[len(grid)]=Polygon([(column, row), (column + wide, row), (column + wide, row - length), (column, row - length)])
动画每个多边形外观的功能:
def update_grid_next(curr):
ax = plt.gca()
ax.axis([-2, 2, -2.1, 1.9])
ax.annotate('n = %s'%curr, [-2, -2])
ax.plot(*grid.geometry[curr].exterior.xy, color = 'b')
ax.fill(*grid.geometry[curr].exterior.xy, alpha=0.5, fc='r', ec='none')
当我运行以下代码时,在Jupyter Notebook中以我期望的方式显示动画(或多或少):
%matplotlib
fig = plt.figure()
a = animation.FuncAnimation(fig, update_grid_next, interval=50, frames = range(75))
在绘图上一个接一个的多边形。但是,当我尝试使用a.save(r'C:\Users\fasbo\k.mp4')
保存视频时,matplotlib会创建static图像,并记录/捕获的帧数惊人。
我怀疑您可能需要在init_func
中使用FuncAnimation
参数,该参数用于绘制不需要更新的内容。