Matplotlib热图动画非常小

时间:2019-01-21 23:12:02

标签: python matplotlib heatmap

我正在尝试在matplotlib中制作热图动画,这在几个小时内首次起作用,但是该图非常小,如下所示。Very small graph

我正在使用的代码如下:

import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import xarray

Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)

x = a[var]['lat'].values # Formatted as follows: [-32,-33,-34,-35,-36...]
y = a[var]['lon'].values # Formatted as follows: [150.2, 151.2, 153.2...]
z = np.array(a[var].values) # [[[24,22,18,23,24,24,25]
                            #   [18,25,23,22,21,19,26]
                            #   ...
                            #   [12,28,19,22,25,26,19]]
                            #  ...
                            #  [[8,4,16,12,22,24,23,8]
                            #   ...
                            #   [12,28,14,7,8,13,6,12]]]
fig = plt.figure()
ax = fig.add_subplot(len(x),len(y),1)
def plot(i):
    data = z[i]
    heatmap = ax.pcolor(x, y, data)
ani = animation.FuncAnimation(fig, plot, interval=1, frames=20)
ani.save('im.mp4', writer=writer)

我还应该提到尝试调用plt.subplots(x,y)导致控制台冻结,并且5分钟后它仍然没有运行。

如果有人有想法或建议,将不胜感激。

1 个答案:

答案 0 :(得分:0)

ax = fig.add_subplot(len(x),len(y),1)行应为ax = fig.add_subplot(111)。此更改后,代码将起作用。