I am trying to animate a list of coordinates over a contextily map. I get "AttributeError: 'NoneType' object has no attribute 'canvas'.
I've tried so many different ways to animate over a map: osmnx (gets distorted/coordinates do not line up), tilemapbase, etc... I am so close to getting what I need with the code below!
import pandas as pd import geopandas as gpd import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation import contextily as cx
#Reads the Excel doc
df = pd.read_excel('PATH_TO_MY_EXCEL_DOC', sheet_name='SHEET_NAME_OF_MY_DOC')
#Creates a list of Lat Long, Long, and Lat
df['Points'] = list(zip(df.Latitude,df.Longitude))
Longs = list(df.Longitude)
Lats = list(df.Latitude)
#This is the list of Coordinates
Coords = df['Points']
print(Coords)
#turns the dataframe into a geodataframe
gdf = gpd.GeoDataFrame(
df, geometry=gpd.points_from_xy(df.Longitude, df.Latitude),crs='EPSG:4326')
#Define the axis
ax = gdf.plot(figsize=(10,10), alpha =0.5)
#Use contextily to create the basemap with the same projection as the geodataframe
fig = cx.add_basemap(ax, crs=gdf.crs.to_string())
#animation definition
def animate(i):
X_points= Longs[0]
Y_points= Lats[0]
Longs.remove(Longs[0])
Lats.remove(Lats[0])
ax.scatter(X_points, Y_points, zorder=1, alpha= 0.5, c='b', s=10)
#make the animation
ani = FuncAnimation(fig, animate, 265, interval=200, repeat = True)
#shows the final plot
plt.show()
If I plt.show() right after the code:
cx.add_basemap(ax, crs=gdf.crs.to_string())
it returns the exact map I need, but adding the animation gives me that error. Any tips to overcome this hurdle?