- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to plot Exponentially Decaying Function using FuncAnimation in Matplotlib
Let us assume that we want to animate a nature of function which is exponentially decaying like y = a(b)^x where b = growth factor and a = initial value.
An exponentially decay function would look like this,
However, for now, we want to animate and plot the exponentially decaying tan function.
First import the libraries,
import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation
Define the axes,
fig, a = plt.subplots()
Plotting a blank figure with axes,
xdata, ydata = ,  line, = ax.plot(xdata, ydata)
Set the limit of the grids,
ax.set_xlim(0, 10) ax.set_ylim(-3.0, 3.0) ax.grid()
Define the function to generate the data which will be used in place of number of frames,
def frame_generator(i=0): while i < 50: i += 0.1 yield x, np.tan(2*np.pi*x) * np.exp(-x/5.)
Define function to plot the animation,
def animate(data): x, y = data xdata.append(x) ydata.append(y) xmin, xmax = ax.get_xlim() if x >= xmax: ax.set_xlim(xmin, 2*xmax) ax.figure.canvas.draw() line.set_data(xdata, ydata) return line
Activate the animation,
ani = FuncAnimation(fig, animate, frame_gen, blit=True, interval=2, repeat=False)
Plot the graph,
- How to exponentially scale the Y axis with matplotlib?
- How to plot a multivariate function in Python Matplotlib?
- How to plot collections.Counter histogram using Matplotlib?
- How to plot vectors in Python using Matplotlib?
- How to plot MFCC in Python using Matplotlib?
- How to plot a wav file using Matplotlib?
- How to plot 3D graphs using Python Matplotlib?
- How to plot events on time using Matplotlib?
- How to plot 2d FEM results using matplotlib?
- How to get a Gantt plot using matplotlib?
- How to Plot Cluster using Clustermaps class in Matplotlib
- How to plot an array in Python using Matplotlib?
- Plot scatter points using plot method in Matplotlib
- How to plot a function defined with def in Python? (Matplotlib)
- How to plot multiple histograms on same plot with Seaborn using Matplotlib?