Search code examples
pythonanimationsliderdata-visualizationplotly

Plotly animated slider in Python


I was trying to recreate this example in a Jupyter notebook.

https://plot.ly/python/gapminder-example/

but was getting this error:

PlotlyDictKeyError: 'slider' is not allowed in 'layout'

Path To Error: ['layout']['slider']

Valid attributes for 'layout' at path ['layout'] under parents ['figure']:

    ['angularaxis', 'annotations', 'autosize', 'bargap', 'bargroupgap',
    'barmode', 'barnorm', 'boxgap', 'boxgroupgap', 'boxmode', 'calendar',
    'direction', 'dragmode', 'font', 'geo', 'height', 'hiddenlabels',
    'hiddenlabelssrc', 'hidesources', 'hoverlabel', 'hovermode', 'images',
    'legend', 'mapbox', 'margin', 'orientation', 'paper_bgcolor',
    'plot_bgcolor', 'radialaxis', 'scene', 'separators', 'shapes',
    'showlegend', 'sliders', 'smith', 'ternary', 'title', 'titlefont',
    'updatemenus', 'width', 'xaxis', 'yaxis']

Run `<layout-object>.help('attribute')` on any of the above.
'<layout-object>' is the object at ['layout']

The animation runs without the slider dict added to layout and the slider is visible and operational, but does not change the graph. When I move the slider it produces the following error in the console:

Uncaught (in promise) undefined

Solution

  • Update:

    I checked the graph you have, I am observing the below error sometimes.

    Uncaught (in promise) undefined

    This error might by due to plotly missing an click or other event, but this is internally within the plotly.js file, if you go to Plotly Slider animation link and to the slider animation section, click play and click on the slider while play is running we get this error, even when I click on pause I get this error. But the animation keeps on playing if I press the play again, hence there is no major impact! It's just that an event is not handled properly.

    So as in the case of the graph you provided, I can get the animation working fine, eventhough I get the error (Uncaught (in promise) undefined) I am still able to play the animation!

    You can use either iplot(fig, validate=False) or plot(fig) to show the graphs in Python with the animation!

    Answer:

    The error is because the layout object has a property called sliders not slider, so wherever you are using slider under layout, please change this, also this plot is very complicated and may have other errors also, please share the code, for debugging. But for now this will be my answer.

    Before:

    ['layout']['slider'] 
    

    After:

    ['layout']['sliders']
    

    Please replace all the slider properties that are related to layout, these need to be changed to sliders.

    References:

    I have handled issues related to this particular slider animated Plotly graph. Please refer to them if need, they may help solve your issue!

    1. Plotly Animated Bubble Chart No Data in the Plot
    2. Plotly Error Invalid Figure or Data Argument
    3. Plotly Icreate Animations Offline on Jupyter Notebook