Share all x-axes of a subplot (rows and columns)

Question:

I try to share all x-axes of a subplot structure with several columns, but I can’t get the solution. With ‘share_xaxes=True’ only the x-axes of the same row are linked, and I am not able to change the ‘xaxis’ paramater from the figures in the subplot. Any idea?

Asked By: Abraham Lopez

||

Answers:

In the Plotly documentation you can see that the axes have an attribute called scaleanchor (see https://plot.ly/python/reference/#layout-xaxis-scaleanchor). You can use it to connect as many axes as you like. I tested it out on a simple subplot with 2 rows and 2 columns where all x-axes are connected:

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)
Answered By: Philipp

I know this post is old, but maybe this can help someone else:

Just use the option shared_xaxes = 'all' when you create subplots with make_subplots() and all x-axes will be shared.

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.