Я пытаюсь анимировать график рассеяния на карте-карте в блокноте jupyter, используя FuncAnimation от matplotlib. Когда я запускаю код, сообщений об ошибках нет, но анимация не отображается. Это мой код:
%matplotlib notebook
from matplotlib import animation
# Sets up the map plot with coastlines from cartopy and the PlateCarree projection
# The PlateCarree projection is neither equal area nor conformal, thus it is mostly used for thematic mapping
fig=plt.figure(figsize=(9,5))
cmap=matplotlib.cm.RdBu_r
norm=matplotlib.colors.Normalize(vmin=0, vmax=50)
ax=plt.axes(projection=ccrs.PlateCarree(),extent=[-180, 180, -90, 90])
# Set labels and ticks for x- and y-axis
ax.set_xticks([-180, -120, -60, 0, 60, 120, 180], crs=ccrs.PlateCarree())
ax.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
plt.xlabel('Longitude [deg]')
plt.ylabel('Latitude [deg]')
plot = plt.scatter([], [], c=[], s=40, norm=norm, cmap=cmap, edgecolor="k")
def init():
plot.set_offsets([])
plot.set_array([])
return plot,
def animate(i):
data = np.hstack((nphi[i], nthe[i]))
plot.set_offsets(data)
plot.set_array(mag[i])
return plot,
# Sets up colourbar and set a label
cbar=plt.colorbar()
cbar.set_label('Magnitude of SV [nT/yr$^2$]')
anim = animation.FuncAnimation(fig, animate, init_func=init, interval=800, blit=True)
Переменные "nphi", "nthe" и "mag" - это массивы с несколькими подмассивами, поэтому все, что я хочу, это иметь анимацию, которая строит график рассеяния с новый набор данных из этих массивов для координат и цветов в каждом l oop. Что я делаю не так?
Минимальный воспроизводимый пример:
# Setting up script by importing libraries and functions as well as using
magic
# Magic line of code that Jupyter Notebook needs to display interactive
figures nicely when using matplotlib
%matplotlib notebook
# Time is used to display execution time of functions
import time
# Numpy is used for most array and math operations
import numpy as np
# The function loadmat is imported from scipy to load .mat data files
from scipy.io import loadmat
# Cartopy is used for projections of world map
import cartopy.crs as ccrs
# Matplotlib.pyplot is used for most plots
import matplotlib.pyplot as plt
# Matplotlib.cm and Matplotlib.colors is used for colourbars
import matplotlib.cm
import matplotlib.colors
from matplotlib import animation
nphi = [np.random.randint(1,180,30),np.random.randint(1,180,30),np.random.randint(1,180,30),np.random.randint(1,180,30),np.random.randint(1,180,30)]
nthe = [np.random.randint(1,90,30),np.random.randint(1,90,30),np.random.randint(1,90,30),np.random.randint(1,90,30),np.random.randint(1,90,30)]
mag = [np.random.randint(1,50,30),np.random.randint(1,50,30),np.random.randint(1,50,30),np.random.randint(1,50,30),np.random.randint(1,50,30)]
# Sets up the map plot with coastlines from cartopy and the PlateCarree projection
# The PlateCarree projection is neither equal area nor conformal, thus it is
mostly used for thematic mapping
fig=plt.figure(figsize=(9,5))
cmap=matplotlib.cm.RdBu_r
norm=matplotlib.colors.Normalize(vmin=0, vmax=50)
ax=plt.axes(projection=ccrs.PlateCarree(),extent=[-180, 180, -90, 90])
# Set labels and ticks for x- and y-axis
ax.set_xticks([-180, -120, -60, 0, 60, 120, 180], crs=ccrs.PlateCarree())
ax.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
plt.xlabel('Longitude [deg]')
plt.ylabel('Latitude [deg]')
plot = plt.scatter([], [], c=[], s=40, norm=norm, cmap=cmap, edgecolor="k")
def init():
plot.set_offsets([])
plot.set_array([])
return plot,
def animate(i):
data = np.hstack((nphi[i], nthe[i]))
plot.set_offsets(data)
plot.set_array(mag[i])
return plot,
# Sets up colourbar and set a label
cbar=plt.colorbar()
cbar.set_label('Magnitude of SV [nT/yr$^2$]')
anim = animation.FuncAnimation(fig, animate, init_func=init, interval=800, blit=True)