Python Plotly:向具有多个子图的散点图添加水平线

Python Plotly:向具有多个子图的散点图添加水平线,python,plotly,plotly-dash,Python,Plotly,Plotly Dash,我有一个在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, co

我有一个在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'
),它将不再工作。我试着从这个答案

我遇到的另一个可能与上述问题有关的问题是,第二行的ylabel与第一行的ylabel不同。我希望他们像第一排一样。我在下图中强调了我的意思。
下面的代码获取与您类似的数据样本,计算每列的平均值,并将其作为水平线添加到每个子图中以生成此图:

我已经从您的原始代码片段中删除了一些似乎会把事情搞砸的元素。我还忽略了破折号元素,因为它们不是生成最小可复制示例所必需的

代码:

#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
import pandas as pd

np.random.seed(123)
df=pd.DataFrame({ 'x':list(range(40)),
                    'y1':np.random.randint(20, 40, 40),
                    'y2':np.random.randint(100, 140, 40),
                    'y3':np.random.randint(20, 40, 40),
                    'y4':np.random.randint(20, 40, 40),
                    'y5':np.random.randint(100, 140, 40),
                    'y6':np.random.randint(20, 40, 40)})
df.set_index('x')

fig = make_subplots(rows=2, cols=3, vertical_spacing=0.1,
                    horizontal_spacing=0.1, shared_xaxes=True, shared_yaxes=False)

fig.add_trace(go.Scatter(x=list(range(40)), y=df['y1'], line_color='#fae823', showlegend=False,
                         hovertemplate=[], xaxis='x1', yaxis='y1'), row=1, col=1)

fig.add_trace(go.Scatter(x=list(range(40)), y=df['y2'], line_color='#fae823', showlegend=False,
                         hovertemplate=[], xaxis='x2', yaxis='y2'), row=1, col=2)

fig.add_trace(go.Scatter(x=list(range(40)), y=df['y3'], line_color='#fae823', showlegend=False,
                         hovertemplate=[], xaxis='x3', yaxis='y3'), row=1, col=3)

fig.add_trace(go.Scatter(x=list(range(40)), y=df['y4'], line_color='#fae823', showlegend=False,
                         hovertemplate=[], xaxis='x4', yaxis='y4'), row=2, col=1)

fig.add_trace(go.Scatter(x=list(range(40)), y=df['y5'], line_color='#fae823', showlegend=False,
                         hovertemplate=[], xaxis='x5', yaxis='y5'), row=2, col=2)

fig.add_trace(go.Scatter(x=list(range(40)), y=df['y6'], line_color='#fae823', showlegend=False,
                         hovertemplate=[], xaxis='x6', yaxis='y6'), row=2, col=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)

# add shapes
col_count = 1
for i in range(1,3):
    for j in range(1,4):
        fig.add_shape(go.layout.Shape(type="line",
                                        yref="paper",
                                        xref="x",
                                        x0=1,
                                        y0=df.iloc[:, col_count].mean(),
                                        x1=40,
                                        y1=df.iloc[:,col_count].mean(),
                                        #line=dict(color="RoyalBlue", width=3),),
                                        line=dict(color='red', width=3),),
                      row=i,
                      col=j)
        col_count = col_count+1

#fig.update_traces(xaxis='x1', col=1)
#fig.update_traces(xaxis='x2', col=2)
#fig.update_traces(xaxis='x3', col=3)
fig.show()

您希望通过这些实现什么:
fig.update_跟踪(xaxis='x3',col=3)
?它弄乱了轴的符号。@vestland是的。我现在才明白这就是问题所在。你记得那个十字准星吗?与此相关。在这个图表中,我想在三列中看到三种不同的股票。所以对于每一列,我想要一个十字线。所以我为每一列设置x轴。是的,我记得十字线。当我找到时间的时候,我也会看看这个。@vestland我刚刚更新了我的代码。我想我忘了在图表上加一条线了。现在没事了。谢谢你的尝试。正如您在评论中指出的,问题可能出在
fig.update\u跟踪中。考虑到这一点,我需要编辑我的问题,以更好地证明我需要什么。如果我删除那些
fig.update\u跟踪
十字头将被删除。所以感谢你的帮助。当我编辑完我的文章后,我会在评论中通知你。非常感谢你的帮助。@Amir我明白了。。。至少我的方法回答了你的问题。我会继续研究细节,你说得对。我接受了。我会问另一个问题。给你分,伙计。你真是太棒了谢谢你!专注的问题通常会吸引更好的答案,所以这是个好主意!