如何为条形图实现日期范围

How to implement date range for bar chart

我已经在 plotly 中生成了一个条形图。每个条对应于 x 轴上的一个日期。我如何实现某种类型的日期选择器小部件,以便我可以看到特定日期或日期范围的栏。是否有任何模块可用于此作业。同样的解决方案是否也适用于旭日图。

第 1 部分 - 带“日期选择器”的条形图

import pandas as pd
import numpy as np
import plotly.express as px

df = pd.DataFrame({**{"date":pd.date_range("1-apr-2021", periods=100)}, **{c:np.random.uniform(i,i+1, 100) for i,c in enumerate(list("abc"))}})

px.bar(df, x="date", y=list("abc")).update_layout(xaxis={"rangeslider":{"visible":True}})

第 2 部分 - 朝阳的日期选择器

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import plotly.express as px
import pandas as pd
import numpy as np
from jupyter_dash import JupyterDash

app = JupyterDash(__name__)

app.layout = html.Div(
    [
        dcc.DatePickerRange(
            id="date-picker",
            start_date=df["date"].min(),
            end_date=df["date"].max(),
            display_format="MMMM Y, DD",
        ),
        dcc.Graph(id="sunburst-fig"),
    ]
)


@app.callback(
    Output("sunburst-fig", "figure"),
    [Input("date-picker", "start_date"), Input("date-picker", "end_date")],
)
def update_graph(start_date, end_date):
    if start_date:
        dfp = df.loc[df["date"].between(*pd.to_datetime([start_date, end_date]))]
    else:
        dfp = df

    fig = px.sunburst(dfp.assign(day=df["date"].dt.strftime("%a"), month=df["date"].dt.strftime("%b")),
            path=["month","day"], values="a")
    return fig


if __name__ == "__main__":
    #     app.run_server(debug=True)
    app.run_server(mode="inline")