【发布时间】:2020-05-25 10:11:42
【问题描述】:
我有一个在 Plotly dash 上运行的散点图。这是代码:
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly
import plotly.graph_objs as go
from plotly.subplots import make_subplots
import numpy as np
fig = make_subplots(rows=2, cols=3, vertical_spacing=0,
horizontal_spacing=0.05, shared_xaxes=True, shared_yaxes=False)
fig.add_trace(go.Scatter(x=list(range(40)), y=np.random.randint(20, 40, 40), line_color='#fae823', showlegend=False,
hovertemplate=[], xaxis='x1', yaxis='y1'), row=1, col=1)
fig.add_trace(go.Scatter(x=list(range(40)), y=np.random.randint(100, 140, 40), line_color='#fae823', showlegend=False,
hovertemplate=[], xaxis='x2', yaxis='y2'), row=1, col=2)
fig.add_trace(go.Scatter(x=list(range(40)), y=np.random.randint(20, 40, 40), line_color='#fae823', showlegend=False,
hovertemplate=[], xaxis='x3', yaxis='y3'), row=1, col=3)
fig.add_trace(go.Scatter(x=list(range(40)), y=np.random.randint(20, 40, 40), line_color='#fae823', showlegend=False,
hovertemplate=[], xaxis='x4', yaxis='y4'), row=2, col=1)
fig.add_trace(go.Scatter(x=list(range(40)), y=np.random.randint(100, 140, 40), line_color='#fae823', showlegend=False,
hovertemplate=[], xaxis='x5', yaxis='y5'), row=2, col=2)
fig.add_trace(go.Scatter(x=list(range(40)), y=np.random.randint(20, 40, 40), line_color='#fae823', showlegend=False,
hovertemplate=[], xaxis='x6', yaxis='y6'), row=2, col=3)
fig.add_shape(go.layout.Shape(type='line', yref='y3', xref='x3', x0=0, x1=30, y0=30, y1=30,
line=dict(color='red', width=3)))
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='dash',
spikecolor='#ebeae8', spikethickness=0.5)
fig.update_yaxes(showgrid=False, zeroline=False, showticklabels=True, showline=False)
fig.update_traces(xaxis='x1', col=1)
fig.update_traces(xaxis='x2', col=2)
fig.update_traces(xaxis='x3', col=3)
app = dash.Dash(__name__)
app.layout = html.Div(children=[
dcc.Graph(id='chart1', figure=fig,
config={'displayModeBar': False})
])
if __name__ == '__main__':
app.run_server(debug=True, dev_tools_ui=False, dev_tools_props_check=False)
但是,如果我将 xref 和 yref 更改为第二行的子图(例如 xref='x4' 和 yref='y4'),它就不再起作用了。我尝试了这个question.的答案
我遇到的另一个可能与上述问题有关的问题是第二行的 ylabels 与第一行的不同。我希望他们像第一排一样。我在下图中强调了我的意思。
【问题讨论】:
-
您希望通过这些 :
fig.update_traces(xaxis='x3', col=3)实现什么目标?它弄乱了轴符号。 -
@vestland 是的。我现在才知道这是问题所在。你还记得那个十字准线吗?它与此有关。在这张图表中,我想在三列中查看三只不同的股票。所以对于每一列我想要一个十字准线。所以对于每一列我设置 x 轴。
-
是的,我记得十字准线。有时间我也看看这个。
-
@vestland 我刚刚更新了我的代码。我想我忘了添加图表中的线。现在可以了。坦克斯。
标签: python plotly plotly-dash