使用dash python构建交互式仪表板

2024-10-06 12:19:18 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我尝试使用python中的dash plotly,以以下数据为例,制作一个交互式仪表板:

        Date  Traded Qty  Deliverable Qty  Delivery %     LTP  Open Interest
0 2020-12-18    29816205          5872798       19.70  268.15       73110000
1 2020-12-21    55160758         14528986       26.34  272.00       71454000
2 2020-12-22    51189571         10781372       21.06  253.85       71013000
3 2020-12-23    29056404          4792004       16.49  258.20       67350000
4 2020-12-24    28585509          6820426       23.86  263.75       68697000

它保存为熊猫数据帧,并希望与下图类似:

chart

对不起,我画得太糟了。 在图表中,当鼠标悬停在“可交付数量”上时,我希望显示“交付%”


Tags: 数据数量date图表仪表板openplotlyqty
1条回答
网友
1楼 · 发布于 2024-10-06 12:19:18

我没有Dash方面的经验,但是this page会帮你的。我建议您仔细研究一下,并在提问之前尝试编写一些代码。我在jupyterlab环境中,所以我为它实现了一些live riley

import dash 
from jupyter_dash import JupyterDash 
import dash_core_components as dcc 
import dash_html_components as html
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import io
import pandas as pd 

data = '''
Date  "Traded Qty"  "Deliverable Qty"  "Delivery %"     LTP  "Open Interest"
0 2020-12-18    29816205          5872798       19.70  268.15       73110000
1 2020-12-21    55160758         14528986       26.34  272.00       71454000
2 2020-12-22    51189571         10781372       21.06  253.85       71013000
3 2020-12-23    29056404          4792004       16.49  258.20       67350000
4 2020-12-24    28585509          6820426       23.86  263.75       68697000
'''

df = pd.read_csv(io.StringIO(data), delim_whitespace=True)
fig = make_subplots(specs=[[{"secondary_y": True}]])

fig.add_trace(go.Bar(x=df['Date'], y=df['Traded Qty'], name='Traded Qty'))
fig.add_trace(go.Bar(x=df['Date'],
                     y=df['Deliverable Qty'],
                     name='Deliverable Qty',
                     hovertext=df['Delivery %'],
                     hovertemplate='<br>'.join([
                         'Deliverable Qty: %{y}',
                         'Delivery %: %{hovertext}'
                     ])
                    ))
fig.add_trace(go.Scatter(x=df['Date'], y=df['LTP'], mode='lines+markers', name='LTP'), secondary_y=True)
fig.add_trace(go.Scatter(x=df['Date'], y=df['Open Interest'], mode='lines+markers', name='Open Interest'))

fig.update_layout(xaxis=dict(type = "category"), barmode='stack', margin=dict(l=20, r=20, t=20, b=20))
fig.update_yaxes(range=[0,300], secondary_y=True)

# instance create
app = JupyterDash(__name__)
# app = dash.Dash(__name__)

app.layout = html.Div(children=[
    html.H3(children='Trade Chart'),
    dcc.Graph(
        id='example-graph',
        figure=fig
    )
])


if __name__ == '__main__':
    # notebook on action
    app.run_server(mode='inline')
    # app.run_server(debug=True)

enter image description here

相关问题 更多 >