Black Lives Matter. Please consider donating to Black Girls Code today.

Subplots with shared x-axes BUT show x-axis for each plot

Hi, is this possible?

As far as I researched, it seems like this seems to be not. But since it’s a very simple functionality, I think it has to be possible somehow. Do you have any advice?

Thanks.

1 Like

@dashuser2,
Yes, you are right: you can perform some updates to display xaxis ticklabels for all subplots, although they are defined with shared xaxes.

Example:

from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig = make_subplots(
    rows=3, cols=1,
    shared_xaxes=True)


fig.add_trace(go.Scatter(x= [1, 1.75, 2.5, 3.5], y=[-1, 3, 0, 3,  5]),
              row=1, col=1)

fig.add_trace(go.Scatter(x= [1, 1.75, 2.5, 3.5], y=[4, 2, 6, 3,  5]),
              row=2, col=1)
fig.add_trace(go.Scatter(x= [1, 1.5,  2, 2.5, 3, 3.5], y=[4, 2, 6, 3,  5, 0]),
              row=3, col=1)

fig.update_layout(width=700, height=500)

To find out the names of xaxis assigned to each subplot, we are displaying the fig.layout
and get:

Layout({
    'height': 500,
    'template': '...',
    'width': 700,
    'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'matches': 'x3', 'showticklabels': False},
    'xaxis2': {'anchor': 'y2', 'domain': [0.0, 1.0], 'matches': 'x3', 'showticklabels': False},
    'xaxis3': {'anchor': 'y3', 'domain': [0.0, 1.0]},
    'yaxis': {'anchor': 'x', 'domain': [0.7333333333333333, 1.0]},
    'yaxis2': {'anchor': 'x2', 'domain': [0.36666666666666664, 0.6333333333333333]},
    'yaxis3': {'anchor': 'x3', 'domain': [0.0, 0.26666666666666666]}
})

Hence the subplots without xaxis ticklabels are the subplots corresponding to (row=1, col=1) , referenced to xaxis, respectively (row=2, col=1), referenced to xaxis2. For these two xaxes we are setting showticklabels=True:

fig.update_layout(xaxis_showticklabels=True, xaxis2_showticklabels=True)

Now fig.show() displays ticklabels for all three subplots.

2 Likes