✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

Draw go.Bar over filled go.Scatter (can't get Bar to plot on top of Scatter)

Heya,

I am trying to plot a basic bar histogram. To make the plot visually more appealing, I’d like to add in the background a semi-transparent filled scatter of the same data.

The code:

    def make_figure(self):
        """Draws histogram."""
        fig = go.Figure()
        fig.add_trace(
            go.Scatter(
                x=list(self.data.index),
                y=list(self.data["num_keys"]),
                mode="lines",
                line=dict(width=1, color="black", shape="spline"),
                fillcolor="rgba(1,1,1,0.5)",
                fill="tozeroy",
            )
        )
        fig.add_trace(
            go.Bar(
                x=list(self.data.index),
                y=list(self.data["num_keys"]),
                marker=dict(color="white", line=dict(color="black", width=1)),
            )
        )
        return fig

The problem is that the scatter ends up in the foreground. See output:

To solve this problem, I’ve tried several ways to instantiate the figure (eg fig = go.Figure(data=[scatter, bar], etc), but the scatter always ends up on top.

Is there a way to get the bars to plot on top of the scatter?

Thanks!