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

Range Slider/Selectors showing partial data or failing to show data

Hi,

I’m a relatively new Plotly user. I’m experiencing the same issue as this post, where the data isn’t being shown for range selectors and slider. However, the suggested workaround (i.e., explicitly set the range for slider) isn’t solving the issue. I’ve also looked at the related reference docs like this one and this one. Here’s the starting image.

For instance, if I select 1M, I don’t see any data. Similarly, if I select 3M, I only see part of the data (i.e., 2 data points). The only selectors that are correctly working are all and ytd. In the code below, I’ve included a sizable test sample for reproducibility.

One thing that I noticed is that the number of data points is impacting whether or not the problem occurs. If you only plot the first (or last) six data points, everything displays fine.

Any guidance would be greatly appreciated. If it’s a bug, hopefully someone at Plotly will see this post and say the same.

I’m running the following versions in a condo environment:
plotly 4.6.0
Python 3.7.7
jupyter notebook 6.0.3
pandas 1.0.3
numpy 1.18.1
Matplotlib 3.1.3

Here’s the code. It’s largely based on the Plotly documentation.

# Imports used earlier in the code, but not necessarily in this reproducible example
%matplotlib inline
import numpy as np
import pandas as pd
import plotly.graph_objects as go
from datetime import datetime
from plotly import offline

# Create figure
fig = go.Figure()

# Data Set
dates=[
'2018-03-31',
'2018-04-30',
'2018-05-31',
'2018-06-30',
'2018-07-31',
'2018-08-31',
'2018-09-30',
'2018-10-31',
'2018-11-30',
'2018-12-31',
'2019-01-31',
'2019-02-28',
'2019-03-31',
'2019-04-30',
'2019-05-31',
'2019-06-30',
'2019-07-31',
'2019-08-31',
'2019-09-30',
'2019-10-31',
'2019-11-30',
'2019-12-31',
'2020-01-31',
'2020-02-29',
'2020-03-31',
]

metric=[
    65,
    254,
    341,
    317,
    281,
    290,
    245,
    165,
    269,
    203,
    212,
    259,
    246,
    174,
    332,
    305,
    298,
    283,
    236,
    273,
    277,
    263,
    334,
    337,
    237,
]

# Add traces
fig.add_trace(go.Scatter(
                x=dates,
                y=metric,
                mode='lines+markers',
                marker=dict(symbol='circle'),
                line=dict(
                    color='royalblue',
                    width=2,
                    ),
                name='Metric'),
             )

# Add range selectors and slider
fig.update_layout(
    xaxis=dict(
        rangeselector=dict(
            buttons=list([
                dict(count=1,
                     label="1M",
                     step="month",
                     stepmode="backward"),
                dict(count=3,
                     label="3M",
                     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(label="All",
                     step="all")
            ])
        ),
        rangeslider=dict(
            visible=True,
            range=(dates[0], dates[-1])
        ),
        type="date"
    )
)

fig.show()

Hi @9eqUoa2vHj welcome to the forum! You need to specify the range of the xaxis, not the one of the rangeslider:

fig.update_layout(
    xaxis=dict(
        rangeselector=dict(
            buttons=list([
                dict(count=1,
                     label="1M",
                     step="month",
                     stepmode="backward"),
                dict(count=3,
                     label="3M",
                     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(label="All",
                     step="all")
            ])
        ),
        rangeslider=dict(
            visible=True,
            #range=(dates[0], dates[-1])
        ),
        range=(dates[0], dates[-1]),
        type="date"
    )
)

1 Like

Many thanks for your assistance, especially on a weekend. Your correction fixed the problem. Sometimes, you just have to laugh. I probably spent a few hours trying to debug it…just part of learning :grinning: Again, thank you for sharing your knowledge.

1 Like