首页 > 解决方案 > 使用 matplotlib.animation 扭曲刻度标签

问题描述

随着 matplotlib 的更新版本,我在动画图上得到了扭曲的标签。有其他人得到这个吗?难道我做错了什么?

最小工作示例:

from netCDF4 import Dataset
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython import display

print(matplotlib.__version__)

# Make a random array of data to plot

r = np.random.rand(10,100,100)

fig, ax = plt.subplots(1,1,figsize=(10,10))

# Initiate Plot

plot = ax.pcolormesh(range(0,100), 
                    range(0,100), 
                    r[0], 
                    cmap='RdBu')

cb = fig.colorbar(plot)
cb.set_label('Temperature')

def animate(frame):
    
    C = r[frame][:-1,:-1]
    
    plot.set_array(C)

ani = animation.FuncAnimation(fig,
                              animate,
                             frames= range(0,r.shape[0]),
                             )    

video = ani.to_html5_video()
html = display.HTML(video)
display.display(html)
plt.close()

我的标签似乎被放置了很多次(尽管即使动画只有两帧,这个错误仍然存​​在),它们看起来像这样:

在此处输入图像描述

如果我注释掉下面的所有行,则不会发生此行为,ax.pcolormesh...这会创建看起来不错的静态图。

我在 matplotlib 3.3.2

标签: pythonmatplotlibmatplotlib-animation

解决方案


推荐阅读