我正在尝试使用Plotly / Dash开发交互式仪表板。主要功能是:
功能(3)可以,不是问题,因此在我的描述中不再赘述。但是,(1)和(2)我无法上班。
总结一下到目前为止我对(1)和(2)所做的事情:一世。创建跟踪,以引用该跟踪应位于哪个图表中(即trace_01&yaxis ='y',trace_02&yaxis ='y2'等);ii。指定yaxis,yaxis2,yaxis3等。iii。生成标准仪表板的其余部分(即go.Figure(data = data,layout = layout),dash.Dash()等)。
问题是,在具有相同x轴的两个(或更多)图表(即y轴)上,我无法获得多套迹线。我无法使它正常工作。
核心问题代码:
# Set Traces/Data
t0101 = go.Scatter(x=df_fx.date, y=df_fx['close'],mode='lines',name='close',yaxis='y')
t0201 = go.Scatter(x=df_fx.date, y=df_fx['adx'],mode='lines',name='adx',yaxis='y2')
data = [t0101,t0201]
# Specify Layout
layout= go.Layout(
xaxis= dict(side= 'bottom', anchor= 'y2'),
yaxis= dict(side= 'right'),
yaxis2= dict(side='left'),)
# Call Figure
fig = go.Figure(data=data, layout=layout)
# Create App
app = dash.Dash()
app.layout = html.Div([dcc.Graph(id='chart', figure=fig)])
此代码生成以下输出:output from code
我真正想要实现的是仪表板输出,例如:required output
重要的是,解决方案可以支持功能(3),例如更改基础数据(所有图表/ y轴都使用相同的数据),或在图表上的各种迹线之间切换。
注意,上述代码中不包括功能性(3)的代码。主要问题是我无法使功能(1)和(2)正常工作。
我已经广泛搜索了所有文档和所有堆栈问题。其中大多数都涉及多个yaxis,但是使用overlay ='y'时,我不需要此解决方案。
我很抱歉,如果已经回答或在其他地方进行了记录,但我无法找到为什么我的实现不会输出所需的多坐标图的原因。
任何建议,技巧,错误都非常感谢。这真的是我在解决这个问题上花了太多小时/夜之后的最后一招。
Appendix 1: Input data sample附录2:Link to data (csv)附录3:Link to data (xlsx)附录4:完整代码:
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd
import datetime as dt
### Input Data_____________________________________________
# Extract
p_fx = 'AUD_NZD'
p_dir = "C:/Users/Gebruiker/Gen_Inds_"
p_file = p_dir + p_fx + ".csv"
df_0 = pd.read_csv(p_file,skiprows=1)
df_1 = pd.DataFrame(df_0.loc[df_0['AUD_NZD']=='AUD_NZD',['AUD_NZD','datetime','open','high','low','close','sma','sma.1','hma','hma.1','kama','kama.1','adx','aroonup', 'aroondown']])
# Prepare
df_1 = df_1.rename(columns={'AUD_NZD': "Pair",
"sma":"sma_s","sma.1":"sma_f",
"hma":"hma_s","hma.1":"hma_f",
"kama":"kama_s","kama.1":"kama_f",
})
df_1['datetime'] = df_1['datetime'].str.rstrip('.999989')
df_2 = df_1.astype({'datetime':'datetime64[ns]'})
df_2['date'] = pd.to_datetime(df_2['datetime'].dt.date, format='%Y-%m-%d')
# Input data
df_fx = pd.DataFrame(df_2[['date','Pair','open','high','low','close','sma_s','sma_f','hma_s','hma_f','kama_s','kama_f','adx','aroonup', 'aroondown']])
print(df_fx.Pair.value_counts())
### Figure_____________________________________________
# Data
t0101 = go.Scatter(x=df_fx.date, y=df_fx['close'],mode='lines',name='close',yaxis='y')
t0201 = go.Scatter(x=df_fx.date, y=df_fx['adx'],mode='lines',name='adx',yaxis='y2')
data = [t0101,t0201]
# Layout
layout= go.Layout(
xaxis= dict(side= 'bottom', anchor= 'y2'),
yaxis= dict(side= 'right'),
yaxis2= dict(side='left'),
)
# Figure
fig = go.Figure(data=data, layout=layout)
### App_____________________________________________
app = dash.Dash()
app.layout = html.Div([
dcc.Graph(id='chart', figure=fig)
])
### End_____________________________________________
if __name__ == '__main__':
app.run_server()
附录4:解决方案(在要求#3的回调中实现)
# add the relevant sub-charts / traces to the fig.
fig = psp.make_subplots(rows=2, cols=1)
fig.append_trace(go.Scatter(x=df_fx.date, y=df_fx.close ,mode='lines',name='close'),
row=1, col=1)
fig.append_trace(go.Scatter(x=df_fx.date, y=df_fx.sma_s ,mode='lines',name='sma_s'),
row=1, col=1)
fig.append_trace(go.Scatter(x=df_fx.date, y=df_fx.adx ,mode='lines',name='adx'),
row=2, col=1)
fig.update_layout(height=600, title_text="Close and ADX")
您需要使用的功能是子图。这是您的数据(随机生成的数据-在将CSV文件添加到问题之前创建的)。
# some fake data (use your CSV instead)
dates = pd.date_range(start = "2020-01-01", end = "2020-06-01", freq="1D")
close = np.random.uniform(90, 110, len(dates))
adx = np.random.uniform(190, 210, len(dates))
# important part - use sub-plots.
fig = make_subplots(rows=2, cols=1)
# add the relevant sub-charts / traces to the fig.
fig.append_trace(go.Scatter(x=dates, y=close ,mode='lines',name='close'),
row=1, col=1)
fig.append_trace(go.Scatter(x=dates, y=adx ,mode='lines',name='adx'),
row=2, col=1)
fig.update_layout(height=600, title_text="Close and ADX")
结果是: