r/matplotlib • • Jul 28 '20

Reverse animation order?

I'm building an animation where the y values should be increasing (exponentially) as the x values decrease. the FuncAnimation always plots from small x to big x values (even if I flip the axis). Anyone solved this before?

1 Upvotes

5 comments sorted by

View all comments

Show parent comment

2

u/jmichaelparty Jul 29 '20

Yeah this is pretty much how I had to do it. I'm kind of an idiot for missing how this was working at first. Here's my code and it worked:

import matplotlib.pyplot as plt
from matplotlib import animation
from matplotlib.animation import FuncAnimation

Writer = animation.writers['ffmpeg']
writer = Writer(fps=60, metadata=dict(artist="Me"), bitrate=1800)

x_data = []
y_data = []

fig, ax = plt.subplots()

ax.set_xlim(0,1)
ax.set_ylim(1,10000000000)

ax.set_xlabel('decreasing diameter (m)')
ax.set_ylabel('Surface Area to Volume Ratio')

ax.set_yscale('log')

line, = ax.plot(0,0, color='blue', lw=4)

def animation_frame(i):
x_data.append(i)
y_data.append(6/i)

line.set_xdata(x_data)
line.set_ydata(y_data)

return line,

def input_array(start, stop, divisor):
arr = []
arr.append(start)

currentValue = start / divisor
while currentValue > stop:
arr.append(currentValue)
currentValue = currentValue/divisor

return arr

anim = FuncAnimation(fig, func=animation_frame, frames= input_array(1,0.000000001,1.05), interval= 10)

anim.save('animation.mp4', writer = writer)

plt.show()

2

u/anionthefuture Jul 30 '20

import matplotlib.pyplot as pltfrom matplotlib import animationfrom matplotlib.animation import FuncAnimation

Writer = animation.writers['ffmpeg']writer = Writer(fps=60, metadata=dict(artist="Me"), bitrate=1800)

x_data = []y_data = []

fig, ax = plt.subplots()

ax.set_xlim(0,1)ax.set_ylim(1,10000000000)

ax.set_xlabel('decreasing diameter (m)')ax.set_ylabel('Surface Area to Volume Ratio')

ax.set_yscale('log')

line, = ax.plot(0,0, color='blue', lw=4)

def animation_frame(i):x_data.append(i)y_data.append(6/i)

line.set_xdata(x_data)line.set_ydata(y_data)

return line,

def input_array(start, stop, divisor):arr = []arr.append(start)

currentValue = start / divisorwhile currentValue > stop:arr.append(currentValue)currentValue = currentValue/divisor

return arr

anim = FuncAnimation(fig, func=animation_frame, frames= input_array(1,0.000000001,1.05), interval= 10)

anim.save('animation.mp4', writer = writer)

plt.show()

Nice - glad you got it working!