0

I am trying to animate a scatter plot on a cartopy map in jupyter notebook using FuncAnimation from matplotlib. When i run the code there are no error messages, but the animation does not show up. This is my code:

%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)

The variables "nphi", "nthe", and "mag" are all arrays with several subarrays, so all i want is to have an animation that plots a scatter plot with a new set of data from those arrays for coordinates and colours in each loop. What am i doing wrong?

Minimal reproducible example:

# 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)

0 Answers0