Animated bar chart with frames

liste_a= [10,9,8,7,6,5,4,3,2,1]
liste_b= [0,1,2,3,4,5,6,7,8,9,10]
    
## Barplot
trace0 = go.Bar(
    x=['A', 'B'],
    y=[a, b]
)

layout=dict(xaxis=dict(zeroline=False, autorange=False),
            yaxis=dict(range=[0, 10], autorange=False, zeroline=False),
            title='Title', hovermode='closest',
            updatemenus= [{'type': 'buttons',
                           'buttons': [{'label': 'Play',
                                        'method': 'animate',
                                        'args': [None]}]}])

frames=[dict(data=[dict(x=['A', 'B'], 
                        y=[liste_a[k], liste_b[k]],
                       mode='markers')
                  ]) for k in range(len(liste_a))] 


data = [trace0]


figure1=dict(data=data, layout=layout, frames=frames)   
iplot(figure1)

Hello everybody, I just started working with plotly. I want to create an animated barchart. The animation should iterate over the values. If I have read in correctly, this should be possible through β€œframes”. How to integrate my barchart here?

Many thanks for the help.

Hi @getaway22,

You’ll need to specify the trace type in each frame. Something like

frames=[dict(data=[
    dict(type='bar',
         x=['A', 'B'], 
         y=[liste_a[k], liste_b[k]])])
        for k in range(len(liste_a))] 

One thing to be aware of though, is that at the moment scatter is the only trace type that supports smooth animations. So for bars, each frame will instantly change the bar height.

-Jon

Hi @jimmease ,

thank you very much, that helped me alot. I wasn’t aware, that there is a β€˜type’ parameter. I tried it with mode.
Thumbs up!

1 Like