栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Jupyter Notebook上离线显示icreate_animations

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Jupyter Notebook上离线显示icreate_animations

因此,让我们从代码中的问题开始。

  1. 您正在

    from plotly.grid_objs import Grid, Column
    用来制作图形,但这是在线绘图的功能,我已经用一个简单的数据框替换了您的逻辑网格,基本上完成了同样的事情。

  2. 其次,我注意到该图形没有显示数据,经过分析,我最终发现您已将X轴的范围设置为

    [30, 85]
    ,但X轴的值介于之间
    1-15k

请在下面找到我的更新代码。希望这能解决您的问题。

码:

import pandas as pdimport numpy as npfrom __future__ import divisionimport plotly.plotly as pyfrom plotly.offline import download_plotlyjs, init_notebook_mode, iplotfrom plotly.graph_objs import *init_notebook_mode()from plotly.grid_objs import Grid, Columnfrom plotly.tools import FigureFactory as FFdataset=pd.read_csv('god_mod_copia.csv')years_from_col = set(dataset['year'])years_ints = sorted(list(years_from_col))years = [str(year) for year in years_ints]# make list of continentscontinents = []for continent in dataset['continent']:    if continent not in continents:         continents.append(continent)df = pd.Dataframe()# make gridfor 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': 'GDP per Capita', 'type': 'log', 'autorange': True} #was not set properlyfigure['layout']['yaxis'] = {'title': 'Life Expectancy', 'autorange': True} #was not set properlyfigure['layout']['hovermode'] = 'closest'figure['layout']['showlegend'] = Truefigure['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': []}custom_colors = {    'Asia': 'rgb(171, 99, 250)',    'Europe': 'rgb(230, 99, 250)',    'Africa': 'rgb(99, 110, 250)',    'Americas': 'rgb(25, 211, 243)',    #'Oceania': 'rgb(9, 255, 255)'     'Oceania': 'rgb(50, 170, 255)'}col_name_template = '{year}+{continent}+{header}_grid'year = 2007for continent in continents:    data_dict = {        'x': df.loc[df['key']==col_name_template.format( year=year, continent=continent, header='GDP_per_capita'        ), 'value'].values[0],        'y': df.loc[df['key']==col_name_template.format( year=year, continent=continent, header='Life_satisfaction'        ), '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='Total_population' ), '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='GDP_per_capita' ), 'value'].values[0], 'y': df.loc[df['key']==col_name_template.format(     year=year, continent=continent, header='Life_satisfaction' ), '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='Total_population'     ), 'value'].values[0],     'color': custom_colors[continent] }, 'name': continent        }        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={'scrollzoom': True})


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/662048.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号