Don’t understand plotly :(

Hi all,

I am trying to create a Bitcoin price tracker and I would like to display the bitcoin value with a timespan of 24h in a area chart.

I don’t want the whole chart to be redrawn every hour I only want to update the data within the chart eg remove the last value and add the current value.

Has anyone done this before? I would very much appreciate a little example

Thanks :pray:

I’m not a plotly expert, but I would expect to regenerate the whole chart every time the data changes, even if it’s just adding one point at the end and removing one from the start.

If you show the code you have, we can see if there is anything that can be improved.

I have two suggestions, only one sounds silly.

  1. Have a conversation with ChatGPT about how to use plotly at the most basic level and see if it can tie in concepts you already understand.

  2. Here is a free course that shows you, with visuals and live code, how to make plots and plot data with matplotlib. This is not plotly at all, but if you can get it to work you will understand how to follow the docs on using plotly with all the same concepts.

Chat GPT says, in pseudo code which needs onwards tweaking:


import plotly.graph_objs as go
import anvil.server

# Initial chart setup
initial_data = get_initial_bitcoin_prices()  # Fetch initial data
chart = go.Figure(data=[go.Scatter(x=initial_data['time'], y=initial_data['price'], fill='tozeroy')])

# Function to update chart data
def update_chart():
    new_price = get_latest_bitcoin_price()  # Fetch latest price
    chart.data[0].x = chart.data[0].x[1:] + [new_price['time']]  # Remove oldest time, add newest time
    chart.data[0].y = chart.data[0].y[1:] + [new_price['price']]  # Remove oldest price, add newest price
    chart.layout.xaxis.range = [min(chart.data[0].x), max(chart.data[0].x)]  # Update x-axis range if needed

# Set up a timer or repeating task to periodically update the chart
anvil.server.call('schedule_task', update_chart, period_in_minutes=60)

It’s partly pants, but a decent starter for ten.