Aggregating time series data

Let’s say I have a times series of units sold over several months. The xaxis shows units sold per day.
How can I create a dropdown menu option that allows me to change to options that show units sold per week, month, and other time periods? I know you can do something like this histograms but can’t seem to find a solution for time series line plots.

Hi @gmcintire,

I’d recommend using pandas for your aggregation step, in particular the pandas.DataFrame.resample method. Then you can plot the resampled result in a bar trace.

For the drop down, you could use a FigureWidget and ipywidgets Dropdown control. Here’s an example that uses a dropdown plus a few other controls: https://plot.ly/python/figurewidget-app/

Hope that helps get you started!
-Jon

The figure widget seems to be the best non-Dash solution for this problem. Thanks!

I’ve been playing around with dropdown widgets and they’re not working for me. I copied the following code from the example on the plotly website

import pandas as pd
import plotly.plotly as py

from ipywidgets import widgets
from IPython.display import display, clear_output, Image
import plotly.graph_objs as go
from plotly.widgets import GraphWidget

# define our widgets
g = GraphWidget('https://plot.ly/~kevintest/1149/')
w = widgets.Dropdown(
options=['red', 'blue', 'green'],
value='red', description=‘Colour:’,)`

# generate a function to handle changes in the widget
def update_on_change(change):
g.restyle({'marker.color': change["new"]})

# set a listener for changes to the dropdown widget
w.observe(update_on_change, names="selected_label")

display(w)
display(g)

The dropdown menu has no effect on the color of the bars. I choose red, blue, green and nothing happens.

Any ideas why this is?

Hi @gmcintire,

It looks like you came across an old tutorial. GraphWidget is deprecated, you’ll want to use FigureWidget instead. I think it might also be an old version of ipywidgets. What page were you looking at?

In any case, here’s a working example

# Imports
import plotly.graph_objs as go

import pandas as pd
import plotly.plotly as py
from ipywidgets import widgets

# Build dropdown
options=['red', 'blue', 'green']
w = widgets.Dropdown(
    options=options,
    index=0,
    description='Colour:')

# Build Figure
fig = go.FigureWidget(data=[
    go.Bar(y=[1, 3, 2],
           marker={'color': options[0]})
])

# Add callback to dropdown to update bar colors
def update_on_change(change):
    fig.data[0].marker.color = change["new"]
w.observe(update_on_change, names="value")

# Display dropdown and figure in vertical layout
widgets.VBox([w, fig])

Hope that helps!
-Jon

So I figured the issue and it was I was missing the [“new”] after the change parameter. How this work? The change parameter is a string not a dictionary. But its working now.

Thanks for the response thought, I’ll check the figure widget tutorial and implement it.

-GM