1

I want to create animation using matplotlib.

But there is one problem related to the beauty of visualization. The next value appears abruptly after the previous one. There is no smooth transition. Such animation is good for own use, but it does not look good in a public demonstration.

Example from official documentation

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
np.random.seed(19680801)

HIST_BINS = np.linspace(-4, 4, 100)
data = np.random.randn(1000)
n, _ = np.histogram(data, HIST_BINS)


def prepare_animation(bar_container):

    def animate(frame_number):

        # simulate new data coming in
        data = np.random.randn(1000)
        n, _ = np.histogram(data, HIST_BINS)
        for count, rect in zip(n, bar_container.patches):
            rect.set_height(count)
        return bar_container.patches
    return animate


fig, ax = plt.subplots()
_, _, bar_container = ax.hist(data, HIST_BINS, lw=1,
                              ec="yellow", fc="green", alpha=0.5)
ax.set_ylim(top=55)
ani = animation.FuncAnimation(fig, prepare_animation(bar_container), 50,
                              repeat=False, blit=True)
plt.show()

enter image description here

Can matplotlib provide a smooth transition from one value to another? Here's an example using the flet library.

GIF with animation made by flet library

Can this be done in matplotlib?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Igor K.
  • 813
  • 6
  • 17
  • What are you trying to animate? Is it a bar graph similar to the first example or something else entirely? There are some tricks to make animations more visually appealing but we would need to know what you are animating. – trent May 09 '23 at 14:07
  • do you mean slower change of values ? – pippo1980 May 15 '23 at 16:21
  • https://stackoverflow.com/questions/22010586/matplotlib-animation-duration – pippo1980 May 15 '23 at 16:22

0 Answers0