在python中以图形方式在烛台图表上隐藏悬停数据

时间:2020-05-12 15:49:48

标签: python plotly plotly-dash

我使用go.Candlestick创建了一个简单的烛台图。悬停烛台图表时,我想隐藏数据。我设置了hoverinfo='none',但它仍在悬停时显示一个图标,如下面的屏幕截图所示。我怎么藏起来?我的整个代码在图片下方。 enter image description here

这是我的完整代码:

def online_chart_1(sym_1):
    data_for_chart = pd.read_csv('C:\\Users\\AF\\Desktop\\python\\results\\csv\\data_for_chart.csv')
    historical_ha_vo = pd.read_csv('C:\\Users\\AF\\Desktop\\python\\results\\csv\\historical_ha_vo.csv')

    historical_ha_vo = historical_ha_vo.loc[historical_ha_vo.sym == sym_1].tail(59)
    data_for_chart = data_for_chart.loc[data_for_chart.sym == sym_1]
    df_appended = historical_ha_vo.append(data_for_chart, ignore_index=True)

    fig = make_subplots(rows=2, cols=1, row_heights=[0.8, 0.2], vertical_spacing=0)
    fig.add_trace(go.Candlestick(open=df_appended['o'], high=df_appended['h'], low=df_appended['l'],
                                 close=df_appended['c'],
                                 increasing_line_color='#0384fc', decreasing_line_color='#e8482c', name=sym_1,
                                 hoverinfo='none',
                                 ), row=1, col=1)

    fig.add_trace(go.Scatter(y=df_appended['vo'], marker_color='#fae823', name='VO', hovertemplate=[]), row=2, col=1)
    fig.update_layout({'plot_bgcolor': "#21201f", 'paper_bgcolor': "#21201f", 'legend_orientation': "h"},
                      legend=dict(y=1, x=0),
                      font=dict(color='#dedddc'), dragmode='pan', hovermode='x unified',
                      margin=dict(b=20, t=0, l=0, r=40))

    fig.update_xaxes(showgrid=False, zeroline=False, rangeslider_visible=False, showticklabels=False,
                     showspikes=True, spikemode='across', spikesnap='data', showline=False, spikedash='solid'
                     )
    fig.update_yaxes(showgrid=False, zeroline=False)
    fig.update_traces(xaxis='x')

    return fig

还有破折号:

app = dash.Dash(__name__)

app.layout = html.Div([html.Div(dcc.Graph(id='chart', figure=online_chart_1('zob'),
                                          config={'displayModeBar': False}))])


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

1 个答案:

答案 0 :(得分:0)

解决方案:

hoverinfo='skip'

情节:

enter image description here

如果没有hoverinfo='skip',下面的相同代码段将为您提供:

enter image description here

代码:

# imports
import plotly.graph_objects as go

import pandas as pd
from datetime import datetime

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')

fig = go.Figure(data=[go.Candlestick(x=df['Date'],
                open=df['AAPL.Open'],
                high=df['AAPL.High'],
                low=df['AAPL.Low'],
                close=df['AAPL.Close'], hoverinfo='skip')])

fig.show()