Send secondary axis plots to the background

Hey there,

I’ve been playing a bit with plotly and I managed to pretty much create the graph I wanted: a graph with a secondary axis where

  1. the first axis (on the left) is for a scatter plot;
  2. the secondary axis (on the right) is for a bar plot.

The only issue I have is that the plot on the secondary axis seems to come on top of the plot from the first axis, which is deteriorating the readability of the graph. When I reverse the axis, the same happens: the secondary axis plot comes on top, whatever the order of plotting. Here is an illustration.

Does someone have a solution to plot bars on the secondary axis while keeping them in the background?

Many thanks for your help.

Nobody has info about my issue? Thanks very much for your help.

Hi @ama1, welcome to the forum! If you want the left axis to be on top, you can use the side attribute of go.layout.YAXis as follows:

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

# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])

# Add traces
fig.add_trace(
    go.Scatter(x=[1, 2, 3], y=[40, 50, 60], name="right axis data"),
    secondary_y=False,
)

fig.add_trace(
    go.Bar(x=[2, 3, 4], y=[4, 5, 6], name="left axis data"),
    secondary_y=True,
)

# Add figure title
fig.update_layout(
    title_text="Double Y Axis Example",
    yaxis=dict(title_text="<b>right</b> yaxis title", 
               side='right'
              ),
    yaxis2=dict(title_text="<b>left</b> yaxis title",
               side='left')
)

# Set x-axis title
fig.update_xaxes(title_text="xaxis title")


fig.show()

Hi @Emmanuelle. I tried your solution and it totally fixed the problem. Thanks a lot.

For reference, is it the normal/expected behavior that the graphs plotted on the secondary axis appear on top of those of the primary axis?