使用破折号构建交互式仪表板 python
Build interactive dashboard using dash python
所以我尝试在 python 中使用破折号制作交互式仪表板,使用以下数据作为示例:
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
它被保存为 pandas 数据帧并希望它类似于下图:
对不起,我画的很烂。
在图表中,我希望将鼠标悬停在“可交付数量”上时显示“交付百分比”
我没有使用 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)
所以我尝试在 python 中使用破折号制作交互式仪表板,使用以下数据作为示例:
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
它被保存为 pandas 数据帧并希望它类似于下图:
对不起,我画的很烂。 在图表中,我希望将鼠标悬停在“可交付数量”上时显示“交付百分比”
我没有使用 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)