Skip to content

Instantly share code, notes, and snippets.

@ysimillides
Created April 29, 2018 09:27
Show Gist options
  • Save ysimillides/832281f876574b1ef0da953b22c87af6 to your computer and use it in GitHub Desktop.
Save ysimillides/832281f876574b1ef0da953b22c87af6 to your computer and use it in GitHub Desktop.
Plot a 2d time-domain problem in pandas
#Let solution be our solution array
#with x,y and z being the first, second, and third index respectively.
import pandas as pd
x_pd= solution[0]
y_pd = solution[1]
z_pd = solution[2]
#We assume we already have the array for t, (this can also be created if needed, based on the dims of the previous iterates.
#We create a DataFrame object to store, and display our results if needed
df = pd.DataFrame({"time": t , "x": x_pd.flatten(), "y": y_pd.flatten(), "z": z_pd.flatten()})
#we define the number of iterates we wish to display
#as all animations, we create an updating function
#that updates our graph at each timestep, k
def animate(k):
data = df[df['time']==k]
graph.set_data (data.x, data.y)
graph.set_3d_properties(data.z)
title.set_text('INSERT TITLE HERE'.format(k))
return title, graph,
#feel free to change these to be more suitable to your specific problem
fig = plt.figure(figsize=(12,12))
ax = fig.add_subplot(111, projection='3d')
title = ax.set_title('Starting value')
#we also plot our initial iterate, to compare with the time_based solution
data = df[df['time']==0]
#we create the graph
graph, = ax.plot(data.x, data.y, data.z, linestyle="", marker="o")
#we animate it
animate = matplotlib.animation.FuncAnimation(fig, animate, 60)
#we display it
HTML(animate.to_html5_video())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment