Last active
July 26, 2018 10:37
-
-
Save pelson/618a5f4ca003e56f06d43815b21848f6 to your computer and use it in GitHub Desktop.
Line animation, featuring pirates. Answer to https://stackoverflow.com/questions/51482082/animate-a-point-moving-along-path-between-two-points
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cartopy.crs as ccrs | |
import matplotlib.animation as animation | |
import matplotlib.pyplot as plt | |
ax = plt.axes(projection=ccrs.Robinson()) | |
ax.stock_img() | |
ny_lon, ny_lat = -75, 43 | |
delhi_lon, delhi_lat = 77.23, 28.61 | |
[line] = plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat], | |
color='blue', linewidth=2, marker='o', | |
transform=ccrs.Geodetic(), | |
) | |
t_path = line._get_transformed_path() | |
path_in_data_coords, _ = t_path.get_transformed_path_and_affine() | |
# Draw the point that we want to animate. | |
[point] = plt.plot(ny_lon, ny_lat, marker='o', transform=ax.projection) | |
def animate_point(i): | |
verts = path_in_data_coords.vertices | |
i = i % verts.shape[0] | |
# Set the coordinates of the line to the coordinate of the path. | |
point.set_data(verts[i, 0], verts[i, 1]) | |
ani = animation.FuncAnimation( | |
ax.figure, animate_point, | |
frames= path_in_data_coords.vertices.shape[0], | |
interval=125, repeat=True) | |
ani.save('point_ani.gif', writer='imagemagick') | |
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment