Black Lives Matter. Please consider donating to Black Girls Code today.

update data in bar charts

I am wondering whether plotly supports animation for bar charts in Jupyter.

My purpose is to display a bar chart with several sequences of data. The problem is that first I want to display a bar chart with the first sequence of data, then with 1 second delay, update the first plot with the second sequence of data (remove old data and put new data in the same plot) and so on.

Currently in Jupyter notebook, I clear the the cell output to display the next bar chart. Is there a way with plotly that I can just update the data in a single bar chart without changing the frame or clearing the whole output?

Thank you in advance!

Hi @tianji,

You can update an existing figure in the Jupyter notebook if you create the figure as a FigureWidget (See https://plot.ly/python/widget-app/) . Here’s an example with a bar chart.

First create the figure with initial data and display it

import plotly.graph_objs as go

data = [go.Bar(
            x=['giraffes', 'orangutans', 'monkeys'],
            y=[20, 14, 23]
    )]
layout = {'yaxis': {'range': [0, 25]}}
fig = go.FigureWidget(data, layout)
fig

Then some time later, update it with new data. This will automatically update the figure displayed above.

bar = fig.data[0]
bar.y = [10, 15, 20]

Hope that helps!
-Jon

2 Likes

Oh, man! You really saved my day. It works! Thanks a lot.

One more question, do you know whether plotly supports animation for bar charts although it is not about my concern any more?

Great! Right now the smooth transition animation is only support in scatter traces.
-Jon

1 Like