Plotly动画气泡图:情节中没有数据

时间:2017-08-16 11:03:52

标签: python-2.7 plot jupyter-notebook plotly bubble-chart

我正在尝试使用这些列调整标准的动态气泡图表到csv文件:

gdp=pd.read_csv('gdp-vs-happiness.csv')

gdp=gdp.ix[~(gdp['year'] < 2005)]
gdp=gdp.dropna()

dataset = gdp

数据点的大小将是人口的函数,我将生活满意度绘制为国家gdp的函数。我在数据集上做了一些工作:

years = ['2005','2006', '2007','2008','2009','2010','2011','2012','2013','2014','2015','2016']

# make list of continents
countries = []
for country in dataset['country']:
    countries.append(country)

# make figure
figure = {
    'data': [],
    'layout': {},
    'frames': []
}
config = {'scrollzoom': True}

# fill in most of layout
figure['layout']['xaxis'] = {'title': 'GDP per Capita', 'type': 'log'}
figure['layout']['yaxis'] = {'range': [0, 10], 'title': 'Life Satisfaction'}
figure['layout']['hovermode'] = 'closest'
figure['layout']['sliders'] = {
    'args': [
        'slider.value', {
            'duration': 400,
            'ease': 'cubic-in-out'
        }
    ],
    'initialValue': '2005',
    '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': []
}

# make data
year = 2005
for country in countries:
    dataset_by_year = dataset[dataset['year'] == year]
    dataset_by_year_and_count = dataset_by_year[dataset_by_year['country'] == country]
    data_dict = {
        'x': list(dataset_by_year_and_count['GDP per capita']),
        'y': list(dataset_by_year_and_count['Life satisfaction in Cantril Ladder (World Happiness Report 2017)']),
        'mode': 'markers',
        'text': list(dataset_by_year_and_count['country']),
        'marker': {
            'sizemode': 'area',
            'sizeref': 200000,
            'size': list(dataset_by_year_and_count['Total population (Gapminder)'])
        },
        'name': country

    }
    figure['data'].append(data_dict)

# make frames
for year in years:
    frame = {'data': [], 'name': str(year)}
    for country in countries:
        dataset_by_year = dataset[dataset['year'] == int(year)]
        dataset_by_year_and_cont = dataset_by_year[dataset_by_year['country'] == country]

        data_dict = {
        'x': list(dataset_by_year_and_count['GDP per capita']),
        'y': list(dataset_by_year_and_count['Life satisfaction in Cantril Ladder (World Happiness Report 2017)']),
            'mode': 'markers',
            'text': list(dataset_by_year_and_count['country']),
            'marker': {
                'sizemode': 'area',
                'sizeref': 200000,
                'size': list(dataset_by_year_and_count['Total population (Gapminder)'])
            },
        'name': country

        }
        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=config)

然后是代码:

userInfo

这里的问题是我得到一个空图(滑块,布局,轴标签动画正在运行),根本没有数据,并且没有引发错误。所以老实说,我不知道问题出在哪里。它显然与脚本中的数据构建有关,但我不知道究竟是什么。

1 个答案:

答案 0 :(得分:1)

我使用了您提供的示例数据并对其进行了处理,我已经解决了一些问题并在每行添加了我的注释,主要原因在评论部分中指出,但您可以与您的代码交叉检查我的代码得到你需要的东西

<强>代码:

from plotly.offline import init_notebook_mode, iplot
from IPython.display import display, HTML

import pandas as pd

init_notebook_mode(connected=True)

url = 'testing.csv'
dataset = pd.read_csv(url)
# instead of hardcoding you can use unique() function to get the years present in the file, then convert to list and sort based on years
# years = dataset['year'].unique().tolist()
# years.sort()
years = ['2007','2008','2009','2010','2011','2012','2013'] # try to provide years that contain data in the data set

# make list of continents
countries = []
for country in dataset['country'].unique():
    countries.append(country)
# make figure
figure = {
    'data': [],
    'layout': {},
    'frames': []
}
config = {'scrollzoom': True}

# fill in most of layout

# there is a small ranging issue, where some points go out of the plot so try this code if you notice it

#figure['layout']['xaxis'] = {'title': 'GDP per Capita',  'autorange': False, 'range': [int(dataset['GDP per capita'].min()), int(dataset['GDP per capita'].max())]} #was not set properly
#figure['layout']['yaxis'] = {'title': 'Life Expectancy', 'autorange': False, 
#                             'range': [int(dataset['Life satisfaction in Cantril Ladder (World Happiness Report 2017)'].min()), 
#                                    int(dataset['Life satisfaction in Cantril Ladder (World Happiness Report 2017)'].max())]} #was not set properly


figure['layout']['xaxis'] = {'title': 'GDP per Capita', 'type': 'log', 'autorange': True} #was not set properly
figure['layout']['yaxis'] = {'title': 'Life Expectancy', 'autorange': True} #was not set properly
figure['layout']['hovermode'] = 'closest'
figure['layout']['showlegend'] = True
figure['layout']['sliders'] = {
    'args': [
        'slider.value', {
            'duration': 400,
            'ease': 'cubic-in-out'
        }
    ],
    'initialValue': '2007',
    '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': []
}

# make data -  here you need to specify the year being used as starting point, important to change
year = 2007
for country in countries:
    dataset_by_year = dataset[dataset['year'] == year]
    dataset_by_year_and_cont=dataset_by_year[dataset_by_year['country'] == country]

    data_dict = {
        'x': list(dataset_by_year_and_cont['GDP per capita']),
        'y': list(dataset_by_year_and_cont['Life satisfaction in Cantril Ladder (World Happiness Report 2017)']),
        'mode': 'markers',
        'text': [country], # since there is only one country we do not need to provide the list for text -  
        #Suggestion: No need to have this
        'marker': {
            'sizemode': 'area',
            'sizeref': 200000,
            'size': list(dataset_by_year_and_cont['Total population (Gapminder)'])
        },
        'name': country
    }
    figure['data'].append(data_dict)

# make frames
for year in years:
    frame = {'data': [], 'name': str(year)}
    dataset_by_year = dataset[dataset['year'] == int(year)] # here this has been moved because if the country 
    # is not present for that particular year there is no need to plot those traces
    for country in dataset_by_year['country']:

        dataset_by_year_and_cont=dataset_by_year[dataset_by_year['country'] == country]

        data_dict = {
            'x': list(dataset_by_year_and_cont['GDP per capita']),
            'y': list(dataset_by_year_and_cont['Life satisfaction in Cantril Ladder (World Happiness Report 2017)']),
            'mode': 'markers',
            'text': [country], # since there is only one country we do not need to provide the list for text - 
            #Suggestion: No need to have this
            'marker': {
                'sizemode': 'area',
                'sizeref': 200000,
                'size': list(dataset_by_year_and_cont['Total population (Gapminder)'])
            },
            'name': country,
            'type': 'scatter',
            'showlegend': True
        }
        frame['data'].append(data_dict)

    figure['frames'].append(frame) #this block was indented and should not have been.
    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=config)

<强>输出: enter image description here