I would like to know animated below plot, and I have the following code, however nothing is shown in the output. Please help me debug.
See the image intended for animation
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.animation import FuncAnimation, PillowWriter
from IPython.display import Image
%matplotlib inline
# Function to filter and get the first 200 rows
def filter_and_slice_df0(filtered, filter_value):
return df0_merged_tilt[df0_merged_tilt['TWINS1/2_LAD1/2_ID'] == filter_value].reset_index(drop=True).head(200)
# Filter the data for each specific value
filtered_df0_11 = filter_and_slice_df0(df0_merged_tilt, 11.0)
filtered_df0_12 = filter_and_slice_df0(df0_merged_tilt, 12.0)
filtered_df0_21 = filter_and_slice_df0(df0_merged_tilt, 21.0)
filtered_df0_22 = filter_and_slice_df0(df0_merged_tilt, 22.0)
# Create the figure and axis
fig, ax = plt.subplots(figsize=(18, 6))
# Initialize the plot
def init():
ax.clear()
ax.set_title('6/12/2008 Plot of Intensity vs. Row Number (First 200 LOS #)', fontweight="bold")
ax.set_xlabel('LOS #', fontweight="bold")
ax.set_ylabel('Intensity(R)', fontweight="bold")
ax.grid(True)
return ax,
# Update function for animation
def update(frame):
ax.clear()
ax.plot(filtered_df0_11.index[:frame], filtered_df0_11['I'][:frame], label='ID=11')
ax.plot(filtered_df0_12.index[:frame], filtered_df0_12['I'][:frame], label='ID=12')
ax.plot(filtered_df0_21.index[:frame], filtered_df0_21['I'][:frame], label='ID=21')
ax.plot(filtered_df0_22.index[:frame], filtered_df0_22['I'][:frame], label='ID=22')
ax.legend(loc="upper left", ncol=4)
ax.set_title('6/12/2008 Plot of Intensity vs. Row Number (First 200 LOS #)', fontweight="bold")
ax.set_xlabel('LOS #', fontweight="bold")
ax.set_ylabel('Intensity(R)', fontweight="bold")
ax.grid(True)
return ax,
# Create the animation
ani = FuncAnimation(fig, update, frames=200, init_func=init, blit=True)
# Save the animation
writer = PillowWriter(fps=5)
gif_path = "/home/jovyan/Heliophysics_Internship/animated_plot.gif"
ani.save(gif_path, writer=writer)
# Display the animation
Image("/home/jovyan/Heliophysics_Internship/animated_plot.gif")
I have tried the code sevaral times including add in
%matplotlib inline
However I am really new to matplotlib animation, so any feedback will be greatly appreciated.
New contributor
Alison is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1