【发布时间】:2020-04-15 09:44:37
【问题描述】:
我正在尝试通过在带有子图的图形上使用 matplotlib.animation 来创建视频。其中一个子图使用 cartopy 显示地图。
我写的代码是:
import pandas as pd
import numpy as np
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import cartopy.feature as cfeature
import matplotlib as mpl
import matplotlib.animation as animation
from IPython.display import HTML
data = pd.DataFrame({'data': np.sin(np.arange(0, 2, 0.2))}, index=pd.date_range(start='1/1/2018', periods=10))
extent = [-16, 10, 36, 48]
#create the figure
fig = plt.figure(figsize=(7,12))
#add subplots
ax = plt.subplot(211, projection=ccrs.PlateCarree())
ax_t = plt.subplot(212)
ax.coastlines(resolution='50m')
ax.add_feature(cfeature.BORDERS.with_scale('50m'))
ax.set_extent(extent, ccrs.PlateCarree())
sm = plt.cm.ScalarMappable(cmap=mpl.cm.Reds,norm=plt.Normalize(0,1))
sm._A = []
cb = plt.colorbar(sm, ax=ax, fraction=0.022, pad=0.02)
cb.set_label('Values')
color = (1.0, 1.0, 1.0)
hpoint = ax.plot(-3, 40, color=color, linewidth=2, marker='o', markersize=30, transform=ccrs.PlateCarree() )
#ax_t.plot(meanRMS)
data.plot(ax=ax_t)
ax_t.margins(x=0, tight=True)
ax_t.grid(True)
box1 = ax.get_position()
box2 = ax_t.get_position()
deltaheigh = box2.height * 0.3
ax_t.set_position([box1.x0, box1.y0 - deltaheigh*1.1 , box1.width , deltaheigh])
这段代码给出了我想要和期望的输出:
在创建视频动画时:
frames = 1
iter_data = data.iterrows()
ndays = data.shape[0]
def makemap(n):
if n % frames == 0:
time, val = next(iter_data)
color = (1.0, 1.0-val[0], 1.0-val[0])
hpoint[0].set_color(color)
ax.set_title(time.strftime("%d %b %Y"), fontsize=20)
ani = animation.FuncAnimation(fig, makemap, frames=frames*(ndays-2), interval=100)
HTML(ani.to_html5_video())
我得到了一个不同形状/格式的视频(更大的白色画布,图像甚至没有居中): enter image description here
如何解决这个意外结果?
【问题讨论】:
标签: python matplotlib animation subplot cartopy