Plotly.py: Setting the rangeslider initial range - bug or user error?

Created on 13 Sep 2017  路  4Comments  路  Source: plotly/plotly.py

I'd like a rangeslider on my plot but I don't want to initially view all the data but instead just view e.g. the YTD. I thought I could do that with the range argument but setting that just compresses the view in the rangeslider axis but you still see all the data. Is this a bug? It's certainly not behaving as I would have expected and I can't really see a use-case for the present behaviour:
image

Repro notebook @ https://anaconda.org/dhirschfeld/plotlybug/notebook

Most helpful comment

Hi there,
https://plot.ly/python/reference/#layout-xaxis-rangeslider-range
That attribute was created to set the range of the rangeslider along the xaxis so the above result is the expected result.
Are you trying to set the range of your plot? In that case you should use the x-axis range argument:
https://plot.ly/python/reference/#layout-xaxis-range

import numpy as np
import pandas as pd

import plotly.graph_objs as go
import plotly.plotly as py

dates = pd.date_range('01-Jan-2010', pd.datetime.now().date(), freq='D')
df = pd.DataFrame(100 + np.random.randn(dates.size).cumsum(), dates, columns=['AAPL'])

trace = go.Scatter(x=df.index, y=df.AAPL)
data = [trace]
layout = dict(
    title='Time series with range slider and selectors',
    xaxis=dict(
        rangeselector=dict(
            buttons=list([
                dict(count=1,
                     label='1m',
                     step='month',
                     stepmode='backward'),
                dict(count=6,
                     label='6m',
                     step='month',
                     stepmode='backward'),
                dict(count=1,
                    label='YTD',
                    step='year',
                    stepmode='todate'),
                dict(count=1,
                    label='1y',
                    step='year',
                    stepmode='backward'),
                dict(step='all')
            ])
        ),
        rangeslider=dict(),
        type='date'
    )
)
fig = dict(data=data, layout=layout)

py.iplot(fig)


initial_range = [
    '2016-01-01', '2017-09-01'
]

fig['layout']['xaxis'].update(range=initial_range)
py.iplot(fig)

this will result in something like:
screen shot 2017-09-12 at 6 46 10 pm

All 4 comments

...so when you drag the rangeslider back it shows a range that wasn't even in your input data:
image

What I'm after is the below view being displayed by default i.e. as if I had already clicked the YTD button before the chart was initially rendered:
image

Hi there,
https://plot.ly/python/reference/#layout-xaxis-rangeslider-range
That attribute was created to set the range of the rangeslider along the xaxis so the above result is the expected result.
Are you trying to set the range of your plot? In that case you should use the x-axis range argument:
https://plot.ly/python/reference/#layout-xaxis-range

import numpy as np
import pandas as pd

import plotly.graph_objs as go
import plotly.plotly as py

dates = pd.date_range('01-Jan-2010', pd.datetime.now().date(), freq='D')
df = pd.DataFrame(100 + np.random.randn(dates.size).cumsum(), dates, columns=['AAPL'])

trace = go.Scatter(x=df.index, y=df.AAPL)
data = [trace]
layout = dict(
    title='Time series with range slider and selectors',
    xaxis=dict(
        rangeselector=dict(
            buttons=list([
                dict(count=1,
                     label='1m',
                     step='month',
                     stepmode='backward'),
                dict(count=6,
                     label='6m',
                     step='month',
                     stepmode='backward'),
                dict(count=1,
                    label='YTD',
                    step='year',
                    stepmode='todate'),
                dict(count=1,
                    label='1y',
                    step='year',
                    stepmode='backward'),
                dict(step='all')
            ])
        ),
        rangeslider=dict(),
        type='date'
    )
)
fig = dict(data=data, layout=layout)

py.iplot(fig)


initial_range = [
    '2016-01-01', '2017-09-01'
]

fig['layout']['xaxis'].update(range=initial_range)
py.iplot(fig)

this will result in something like:
screen shot 2017-09-12 at 6 46 10 pm

Yep, thanks @cldougl!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gv-collibris picture gv-collibris  路  4Comments

EmilienDupont picture EmilienDupont  路  4Comments

tssweeney picture tssweeney  路  4Comments

entron picture entron  路  4Comments

suciokhan picture suciokhan  路  3Comments