【问题标题】:Plotly Dash: How to add footnotes and source text to plotsPlotly Dash:如何在绘图中添加脚注和源文本
【发布时间】:2021-07-07 08:44:43
【问题描述】:

下面是我的代码:

app = dash.Dash(__name__, meta_tags=[{
      'name': 'viewport',
      'content': 'width=device-width, initial-scale=1.0'
    }])
server=app.server

tabs_styles = {'display': 'inlineBlock', 'height': 'auto', 'width': 'auto',
               'position': 'fixed', "background": "#323130", 'top': '12.5vh', 'left': '7.5vw',
               'border': 'grey', 'border-radius': '4px'}

tab_style = {
    "background": "#323130",
    'text-transform': 'uppercase',
    'color': 'white',
    'border': '#A9A9A9',
    'font-size': '10px',
    'font-weight': 600,
    'align-items': 'center',
    'justify-content': 'center',
    'border-radius': '4px',
    'padding':'6px'
}

tab_selected_style = {
    "background": "#A9A9A9",
    'text-transform': 'uppercase',
    'color': 'white',
    'font-size': '10px',
    'font-weight': 600,
    'align-items': 'center',
    'justify-content': 'center',
    'border-radius': '4px',
    'padding':'6px'
}

app.layout = html.Div([
    dcc.Tabs(id='tabs-example', value='tab-1', mobile_breakpoint=0, children=[
        dcc.Tab(label='India', value='tab-1',style=tab_style, selected_style=tab_selected_style),
        dcc.Tab(label='Ahmedabad', value='tab-2',style=tab_style, selected_style=tab_selected_style),
        dcc.Tab(label='Bengaluru', value='tab-3',style=tab_style, selected_style=tab_selected_style)
    ]),
    html.Div(id='tabs-example-content')
])

@app.callback(Output('tabs-example-content', 'children'),
              Input('tabs-example', 'value'))

def render_content(tab):
    if tab == 'tab-1':
        return html.Div([
            dcc.Graph(id='g2', figure=india)], 
            className="row", 
            style={"display": "block","margin-left": "auto","margin-right": "auto"})
    elif tab == 'tab-2':
        return html.Div([
            dcc.Graph(id='g2', figure=ahm)], 
            className="row", 
            style={"display": "block","margin-left": "auto","margin-right": "auto"})
    elif tab == 'tab-3':
        return html.Div([
            dcc.Graph(id='g2', figure=blr)], 
            className="row", 
            style={"display": "block","margin-left": "auto","margin-right": "auto"})


if __name__ == '__main__':
    app.run_server(debug=True, use_reloader=False)

我想在此仪表板中为每个绘图的每个选项卡下添加脚注。

  • 我应该如何修改才能实现这一点?

选项卡中绘图的预期输出如下所示: 一个情节,以及每个选项卡的情节下方的一些脚注。

链接到我的代码的仪表板:https://isb-quant-index.herokuapp.com/

eg3:https://twitter.com/ShamikaRavi/status/1377893086810959872/photo/1

【问题讨论】:

    标签: python plotly data-visualization plotly-dash plotly-python


    【解决方案1】:

    我编写代码的理解是,您的问题是关于如何注释绘图而不是如何去做。我在官方reference中自定义了样例,并添加了带有链接的文本。

    import plotly.express as px
    import plotly.graph_objects as go
    import pandas as pd
    
    df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
    
    fig = go.Figure(go.Scatter(x=df['Date'], y=df['AAPL.High']))
    
    fig.update_xaxes(rangeslider_visible=True)
    
    note = 'NYSE Trading Days After Announcement<br>Source:<a href="https://www.nytimes.com/"">The NY TIMES</a> Data: <a href="https://www.yahoofinance.com/">Yahoo! Finance</a>'
    fig.add_annotation(
        showarrow=False,
        text=note,
        font=dict(size=10), 
        xref='x domain',
        x=0.5,
        yref='y domain',
        y=-0.5
        )
    
    fig.show()
    

    【讨论】:

      猜你喜欢
      • 2022-07-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-19
      • 2021-05-03
      • 2019-01-13
      • 2020-02-25
      • 2022-01-15
      • 2023-03-22
      相关资源
      最近更新 更多