pythondateplotlydate-rangerangeslider

How to change default rangeselector in plotly python


I am using plotly(python) to implement range selector where I've created multiple buttons for selecting ranges and I want to show only one month data by default but it showing all data. My code and its output are below what changes should I make?

 import plotly.graph_objects as go
    
    import pandas as pd
    
    # Load data
    df = pd.read_csv(
        "https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
    df.columns = [col.replace("AAPL.", "") for col in df.columns]
    
    # Create figure
    fig = go.Figure()
    
    fig.add_trace(
        go.Scatter(x=list(df.Date), y=list(df.High)))
    
    # Set title
    fig.update_layout(
        title_text="Time series with range slider and selectors"
    )
    
    # Add range slider
    fig.update_layout(
        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(
                visible=True
            ),
            type="date"
        )
    )
    
    fig.show()

enter image description here


Solution

  • You can add range=[{start_date},{end_date}] to the dictionary you're passing to the xaxis parameter. For example:

    fig.update_layout(
        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(
                visible=True
            ),
            type="date",
            range=["2016-01-01","2016-02-01"]
        )
    )
    

    enter image description here