[Plotly] how to 2 x-axis? Please help me... please

I need your help.
I would like to ask for your help regarding plotly.

As shown in the picture below, I drew a graph with two x-axis up and down using plotly.
image

However, I would like to draw a 2x2 shape graph with two x-axis up and down like the picture below, but I can’t figure it out.

I’m looking for an angel to help me.
Please I beg you.
Thank you.

#The code that created one chart.
import numpy as np

import plotly.graph_objects as go

from plotly.subplots import make_subplots

fig=make_subplots(

specs=[[{“secondary_y”: True}]])

print(fig.layout)

fig.update_layout(xaxis2= {‘anchor’: ‘y’, ‘overlaying’: ‘x’, ‘side’: ‘top’},

yaxis_domain=[0, 0.94]);

fig.add_trace(

go.Bar(x=[1, 2, 3, 4],

y=[7, 4, 5, 6],

name=“bar”,

), secondary_y=False)

fig.add_trace(

go.Scatter(x=[-2, -1, 0, 1],

y=[4, 2, 5, 6],

name=“scatt1”,

line_color="#ee0000"), secondary_y=True)

fig.data[1].update(xaxis=‘x2’)

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

Hi,

Welcome to the community! :slightly_smiling_face:

Here’s an example based on yours with a 1x2 subplot, it should be straightforward to generalize for different configurations:

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

fig=make_subplots(
    cols=2, 
    rows=1,
    specs=[[{"secondary_y": True}, {"secondary_y": True}]]
)


bar_trace = go.Bar(
    x=[1, 2, 3, 4], 
    y=[7, 4, 5, 6],
    name="bar",
)

scatter_trace = go.Scatter(
    x=[-2, -1, 0, 1],
    y=[4, 2, 5, 6],
    name="scatt1",
    line_color="#ee0000"
)

fig.add_trace(bar_trace, col=1, row=1)
fig.add_trace(scatter_trace, col=1, row=1, secondary_y=True)

fig.add_trace(bar_trace, col=2, row=1)
fig.add_trace(scatter_trace, col=2, row=1, secondary_y=True)

# Copy 'anchor' and 'domain' of each subplot original xaxis
fig.update_layout(
    xaxis3={**fig.layout.xaxis.to_plotly_json(), "side": "top", "overlaying": "x"},
    xaxis4={**fig.layout.xaxis2.to_plotly_json(), "side": "top", "overlaying": "x2"},
)

# Re-assign scatter traces to axes created above
fig.data[1].update(xaxis="x3")
fig.data[3].update(xaxis="x4")

Let us know if this helps!

1 Like

you are my angel!!!
thank you! thank you!!!