-1

I want to make a gdp vs life expectancy for Ireland over the course of a few years. I want to plot the first point on a scatter plot, then I want wait a few seconds and have the next point plot.

ax = plt.figure()


for i in year:
    plt.scatter(ie_gdp[i], ie_life[i])
    plt.draw()
    plt.show()
    plt.pause(1)

So far this is all I can come up with. However, using JupterLab this plots an individual plot for each point. I've tried looking at animations online, but they all use live data. I already have the datasets cleaned and reay in ie_gdp and ie_life.

%matplotlib inline

fig = plt.figure(figsize = (15,15))
ax = fig.add_subplot(1,1,1)

def animate(i):
    xs = []
    ys = []
    for y in year:
        xs.append(ie_gdp[y])
        ys.append(ie_life[y])
        ax.cla()
        ax.scatter(xs,ys)
    

ani = animation.FuncAnimation(fig, animate, interval = 10000)
plt.show()

Above is my attempt at using animations, but it also doesn't work. I get this error: AttributeError: 'list' object has no attribute 'shape'

Any help would be appreciated.

DillM94
  • 71
  • 4
  • What is your specific problem implementing [these approaches](https://stackoverflow.com/q/42722691/8881141)? Interactive mode is sufficient for your task. – Mr. T Jan 21 '21 at 12:38
  • Using the ion example I get this error: UserWarning: Matplotlib is currently using module://ipykernel.pylab.backend_inline, which is a non-GUI backend, so cannot show the figure. – DillM94 Jan 21 '21 at 12:50
  • Strategy one: [Change the backend](https://stackoverflow.com/a/43015816/8881141). Strategy two: Use FuncAnimation. – Mr. T Jan 21 '21 at 13:03

1 Answers1

0

I'm not sure I understand your intended animation, but I animated the x-axis as year, y-axis as average age, and the size of the scatter plot as GDP value. The sample data is from the data provided by Plotly, so please replace it with your own data.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
#from IPython.display import HTML
#from matplotlib.animation import PillowWriter

# for sample data
import plotly.express as px
df = px.data.gapminder()

ie_df = df[df['country'] == 'Ireland']
ie_df
    country     continent   year    lifeExp     pop     gdpPercap   iso_alpha   iso_num
744     Ireland     Europe  1952    66.910  2952156     5210.280328     IRL     372
745     Ireland     Europe  1957    68.900  2878220     5599.077872     IRL     372
746     Ireland     Europe  1962    70.290  2830000     6631.597314     IRL     372
747     Ireland     Europe  1967    71.080  2900100     7655.568963     IRL     372
748     Ireland     Europe  1972    71.280  3024400     9530.772896     IRL     372
749     Ireland     Europe  1977    72.030  3271900     11150.981130    IRL     372
750     Ireland     Europe  1982    73.100  3480000     12618.321410    IRL     372
751     Ireland     Europe  1987    74.360  3539900     13872.866520    IRL     372
752     Ireland     Europe  1992    75.467  3557761     17558.815550    IRL     372
753     Ireland     Europe  1997    76.122  3667233     24521.947130    IRL     372
754     Ireland     Europe  2002    77.783  3879155     34077.049390    IRL     372
755     Ireland     Europe  2007    78.885  4109086     40675.996350    IRL     372

fig = plt.figure(figsize=(10,10))
ax = plt.axes(xlim=(1952,2012), ylim=(0, 45000))

scat = ax.scatter([], [], [], cmap='jet')

def animate(i):
    tmp = ie_df.iloc[:i,:]
    ax.clear()
    scat = ax.scatter(tmp['year'], tmp['lifeExp'], s=tmp['gdpPercap'], c=tmp['gdpPercap'], ec='k')
    ax.set_xlabel('Year', fontsize=15)
    ax.set_ylabel('lifeExp', fontsize=15)
    ax.set_title('Ireland(1952-2007)')
    return scat,

anim = FuncAnimation(fig, animate, frames=12, interval=1000, repeat=False)
#anim.save('gdp_life.gif', writer='Pillow')
plt.show()

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32