0

I am using code from geekforgeek and copying it to jupyternotebook.

from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation, writers
import numpy as np

fig = plt.figure(figsize = (7,5))
axes = fig.add_subplot(1,1,1)
axes.set_ylim(0, 300)
palette = ['blue', 'red', 'green',
        'darkorange', 'maroon', 'black']

y1, y2, y3, y4, y5, y6 = [], [], [], [], [], []

def animation_function(i):
    y1 = i
    y2 = 5 * i
    y3 = 3 * i
    y4 = 2 * i
    y5 = 6 * i
    y6 = 3 * i

    plt.xlabel("Country")
    plt.ylabel("GDP of Country")
    
    plt.bar(["India", "China", "Germany",
            "USA", "Canada", "UK"],
            [y1, y2, y3, y4, y5, y6],
            color = palette)

plt.title("Bar Chart Animation")

animation = FuncAnimation(fig, animation_function,
                        interval = 50)
plt.show()

I am able to see only image not the animation. How to solve it?this is what I getthis is what I should should get

Mr. T
  • 11,960
  • 10
  • 32
  • 54
  • The animation itself works in matplotlib 3.5.1., so I assume it is a setting in jupyter notebook that prevents it from being animated. – Mr. T Apr 15 '22 at 19:52

1 Answers1

0

You probably need to install the ipympl module. You can install it with pip install ipympl or with conda install -c conda-forge ipympl.

Then, when you need an interactive frame to wrap Matplotlib's output (such as is the case for animations), you execute the following command at the top of your notebook: %matplotlib widget.

Then, you execute your cell with the animation, and "magically" it will be rendered on the screen.

Davide_sd
  • 10,578
  • 3
  • 18
  • 30