Полностью анимация отображает пустой вид - PullRequest
0 голосов
/ 28 апреля 2020

Я собрал некоторые данные в отчетах об индексе мирового счастья за 2015–2019 годы, к которым вы можете получить доступ здесь и загрузить набор данных. Я хочу сделать анимационный слайдер счастья против какого-то другого фактора (например, экономика, здоровье и т. Д. c) на протяжении многих лет. Ниже мой код:

import pandas as pd
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
from plotly.graph_objs import *
init_notebook_mode()

from chart_studio.grid_objs import Grid, Column
from plotly.tools import FigureFactory as FF 

dataset = pd.read_csv('Full dataset.csv')
dataset.drop(['Family', 'Standard Error'], axis=1, inplace=True)

years_from_col = set(dataset['year'])
years_ints = sorted(list(years_from_col))
years = [str(year) for year in years_ints]

# make list of continents
continents = []
for continent in dataset['Continent']:
    if continent not in continents: 
        continents.append(continent)

df = pd.DataFrame()

# make grid
for year in years:
    for continent in continents:
        dataset_by_year = dataset[dataset['year'] == int(year)]
        dataset_by_year_and_cont = dataset_by_year[dataset_by_year['Continent'] == continent]
        for col_name in dataset_by_year_and_cont:
            # each column name is unique
            temp = '{year}+{continent}+{header}_grid'.format(
                year=year, continent=continent, header=col_name
            )
            #if dataset_by_year_and_cont[col_name].size != 0:
            df = df.append({'value': list(dataset_by_year_and_cont[col_name]), 'key': temp}, ignore_index=True)

figure = {
    'data': [],
    'layout': {},
    'frames': []
}
figure['layout']['xaxis'] = {'title': 'Economy (GDP per Capita)', 'type': 'log', 'autorange': True}
figure['layout']['yaxis'] = {'title': 'Happiness Score', 'autorange': True}
figure['layout']['hovermode'] = 'closest'
figure['layout']['showlegend'] = True
figure['layout']['sliders'] = {
    'args': [
        'slider.value', {
            'duration': 400,
            'ease': 'cubic-in-out'
        }
    ],
    'initialValue': '2015',
    'plotlycommand': 'animate',
    'values': years,
    'visible': True
}
figure['layout']['updatemenus'] = [
    {
        'buttons': [
            {
                'args': [None, {'frame': {'duration': 500, 'redraw': False},
                         'fromcurrent': True, 'transition': {'duration': 300, 'easing': 'quadratic-in-out'}}],
                'label': 'Play',
                'method': 'animate'
            },
            {
                'args': [[None], {'frame': {'duration': 0, 'redraw': False}, 'mode': 'immediate',
                'transition': {'duration': 0}}],
                'label': 'Pause',
                'method': 'animate'
            }
        ],
        'direction': 'left',
        'pad': {'r': 10, 't': 87},
        'showactive': False,
        'type': 'buttons',
        'x': 0.1,
        'xanchor': 'right',
        'y': 0,
        'yanchor': 'top'
    }
]

sliders_dict = {
    'active': 0,
    'yanchor': 'top',
    'xanchor': 'left',
    'currentvalue': {
        'font': {'size': 20},
        'prefix': 'Year:',
        'visible': True,
        'xanchor': 'right'
    },
    'transition': {'duration': 300, 'easing': 'cubic-in-out'},
    'pad': {'b': 10, 't': 50},
    'len': 0.9,
    'x': 0.1,
    'y': 0,
    'steps': []
}



custom_colors = {
    'Asia': 'rgb(171, 99, 250)',
    'Europe': 'rgb(230, 99, 250)',
    'Africa': 'rgb(99, 110, 250)',
    'North America': 'rgb(25, 211, 243)',
    'South America': 'rgb(25, 163, 243)',
    'Oceania': 'rgb(50, 170, 255)'
}

col_name_template = '{year}+{continent}+{header}_grid'
year = 2015
for continent in continents:
    data_dict = {
        'x': df.loc[df['key']==col_name_template.format(
            year=year, continent=continent, header='Economy (GDP per Capita)'
        ), 'value'].values[0],
        'y': df.loc[df['key']==col_name_template.format(
            year=year, continent=continent, header='Happiness Score'
        ), 'value'].values[0],
        'mode': 'markers',
        'text': df.loc[df['key']==col_name_template.format(
            year=year, continent=continent, header='Country'
        ), 'value'].values[0],
        'marker': {
            'sizemode': 'area',
            'sizeref': 200000,
            'size': df.loc[df['key']==col_name_template.format(
                year=year, continent=continent, header='Generosity'
            ), 'value'].values[0],
            'color': custom_colors[continent]
        },
        'name': continent
    }
    figure['data'].append(data_dict)

for year in years:
    frame = {'data': [], 'name': str(year)}
    for continent in continents:
        data_dict = {
            'x': df.loc[df['key']==col_name_template.format(
                year=year, continent=continent, header='Economy (GDP per Capita)'
            ), 'value'].values[0],
            'y': df.loc[df['key']==col_name_template.format(
                year=year, continent=continent, header='Happiness Score'
            ), 'value'].values[0],
            'mode': 'markers',
            'text': df.loc[df['key']==col_name_template.format(
                year=year, continent=continent, header='Country'
            ), 'value'].values[0],
            'marker': {
                'sizemode': 'area',
                'sizeref': 200000,
                'size': df.loc[df['key']==col_name_template.format(
                    year=year, continent=continent, header='Generosity'
                ), 'value'].values[0],
                'color': custom_colors[continent]
            },
            'name': continent
        }
        frame['data'].append(data_dict)

    figure['frames'].append(frame)
    slider_step = {'args': [
        [year],
        {'frame': {'duration': 300, 'redraw': False},
         'mode': 'immediate',
       'transition': {'duration': 300}}
     ],
     'label': year,
     'method': 'animate'}
    sliders_dict['steps'].append(slider_step)


figure['layout']['sliders'] = [sliders_dict]
iplot(figure, config={'scrollzoom': True})

Он производит анимацию пустого, а именно:

enter image description here

Что пошло не так? И как мне это исправить?

1 Ответ

1 голос
/ 28 апреля 2020
import pandas as pd
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode()

dataset = pd.read_csv('https://raw.githubusercontent.com/PrashantSaikia/Happiness-Index/master/Full%20dataset.csv')

years = list(dataset['year'].sort_values().unique())

continents = list(dataset['Continent'].sort_values().unique())

layout = {

    'xaxis': {
        'title': 'Economy (GDP per Capita)',
        'type': 'log',
        'autorange': True
    },

    'yaxis': {
        'title': 'Happiness Score',
        'autorange': True
    },

    'hovermode': 'closest',

    'showlegend': True,

    'updatemenus': [{
        'buttons': [
            {
                'args': [None, {'frame': {'duration': 600, 'redraw': True}, 'fromcurrent': True, 'transition': {'duration': 0}}],
                'label': 'Play',
                'method': 'animate'
            },
            {
                'args': [[None], {'frame': {'duration': 0, 'redraw': False}, 'mode': 'immediate', 'transition': {'duration': 0}}],
                'label': 'Pause',
                'method': 'animate'
            }
        ],
        'direction': 'left',
        'pad': {'r': 10, 't': 87},
        'showactive': False,
        'type': 'buttons',
        'x': 0.1,
        'xanchor': 'right',
        'y': 0,
        'yanchor': 'top'
    }],

    'sliders': [{
        'active': 0,
        'yanchor': 'top',
        'xanchor': 'left',
        'currentvalue': {
            'font': {'size': 20},
            'prefix': 'Year:',
            'visible': True,
            'xanchor': 'right'},
        'transition': {'duration': 300, 'easing': 'cubic-in-out'},
        'pad': {'b': 10, 't': 50},
        'len': 0.9,
        'x': 0.1,
        'y': 0,
        'steps': []
    }]

}

custom_colors = {
    'Asia': 'rgb(171, 99, 250)',
    'Europe': 'rgb(230, 99, 250)',
    'Africa': 'rgb(99, 110, 250)',
    'North America': 'rgb(25, 211, 243)',
    'South America': 'rgb(25, 163, 243)',
    'Oceania': 'rgb(50, 170, 255)'
}

data = []

year = years[0]

for continent in continents:

    df = dataset[(dataset['Continent'] == continent) & (dataset['year'] == year)]

    data.append(go.Scatter(x=df['Economy (GDP per Capita)'],
                           y=df['Happiness Score'],
                           text=df['Country'],
                           mode='markers',
                           marker={
                               'size': 100 * df['Generosity'],
                               'color': custom_colors[continent]
                           },
                           name=continent))

frames = []

for year in years[1:]:

    frame = {'data': [], 'name': str(year)}

    for continent in continents:

        df = dataset[(dataset['Continent'] == continent) & (dataset['year'] == year)]

        frame['data'].append(go.Scatter(x=df['Economy (GDP per Capita)'],
                                        y=df['Happiness Score'],
                                        text=df['Country'],
                                        mode='markers',
                                        marker={
                                            'size': 100 * df['Generosity'],
                                            'color': custom_colors[continent]
                                        },
                                        name=continent))

    frames.append(go.Frame(data=frame['data'], name=frame['name']))

    slider_step = {
        'args': [
            [str(year)],
            {'frame': {'duration': 600, 'redraw': True},
             'mode': 'immediate',
             'transition': {'duration': 200}}
        ],
        'label': str(year),
        'method': 'animate'
    }

    layout['sliders'][0]['steps'].append(slider_step)

figure = go.Figure(data=data, layout=layout, frames=frames)

iplot(figure)

enter image description here

...