共享子图的所有x轴(行和列)

时间:2019-05-20 15:44:11

标签: python python-3.x plotly-dash

我尝试与几列共享一个子图结构的所有x轴,但是我找不到解决方案。使用“ share_xaxes = True”时,仅链接同一行的x轴,并且我无法从子图中的图形更改“ xaxis”参数。有想法吗?

1 个答案:

答案 0 :(得分:0)

在Plotly文档中,您可以看到轴具有名为 scaleanchor 的属性(请参见https://plot.ly/python/reference/#layout-xaxis-scaleanchor)。您可以使用它连接任意数量的轴。我在一个简单的子图中进行了测试,该子图中有2行和2列,其中所有x轴都已连接:

import plotly.plotly as py
import plotly.graph_objs as go

# -*- coding: utf-8 -*-
import dash
import dash_core_components as dcc
import dash_html_components as html

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)


def create_figure():
    trace1 = go.Scatter(
        x=[1, 2, 3],
        y=[2, 3, 4]
    )
    trace2 = go.Scatter(
        x=[1, 2, 3],
        y=[5, 5, 5],
        xaxis='x2',
        yaxis='y2'
    )
    trace3 = go.Scatter(
        x=[1, 2, 3],
        y=[600, 700, 800],
        xaxis='x3',
        yaxis='y3'
    )
    trace4 = go.Scatter(
        x=[1, 2, 3],
        y=[7000, 8000, 9000],
        xaxis='x4',
        yaxis='y4'
    )
    data = [trace1, trace2, trace3, trace4]
    layout = go.Layout(
        xaxis=dict(
            domain=[0, 0.45],
            anchor='y'
        ),
        xaxis2=dict(
            domain=[0.55, 1],
            anchor='y2',
            scaleanchor='x'
        ),
        xaxis3=dict(
            domain=[0, 0.45],
            anchor='y3',
            scaleanchor='x'
        ),
        xaxis4=dict(
            domain=[0.55, 1],
            anchor='y4',
            scaleanchor='x'
        ),
        yaxis=dict(
            domain=[0, 0.45],
            anchor='x'
        ),
        yaxis2=dict(
            domain=[0, 0.45],
            anchor='x2'
        ),
        yaxis3=dict(
            domain=[0.55, 1],
            anchor='x3'
        ),
        yaxis4=dict(
            domain=[0.55, 1],
            anchor='x4'
        )
    )
    fig = go.Figure(data=data, layout=layout)
    return fig


app.layout = html.Div(children=[
    html.H1(children='Hello Dash'),

    html.Div(children='''
        Dash: A web application framework for Python.
    '''),

    dcc.Graph(
        id='example-graph',
        figure=create_figure()
    )
])


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