Bar and line charts combined in PlotlyJS

Hello!

I’m trying to plot a bar chart and a line chart combined in the same double y-axis plot, using PlotlyJS, by running the following code:

fixed_cost = [791.57;773.16;765.58;758.15;750.86;743.71;736.69;729.81;723.05;716.42;709.91]

plot(
    [
        scatter(x = 0:0.01:0.1, y = [0;0;0;0;0;0;0;0;0;0.008;0.0394], name = "avg_penalty_cost"),
        scatter(x = 0:0.01:0.1, y = [0;0;0;0;0;0;0;0;0;0.1602;0.7873], name = "cvar_penalty_cost", marker_color = "MediumPurple"),
        bar(x = 0:0.01:0.1, y = fixed_cost, name = "fixed_cost", text = fixed_cost, textposition = "auto", marker_color = "LightSkyBlue", yaxis = "y2"),
    ],
    Layout(
        title_text = "Sensitivity Analysis",
        xaxis = attr(title = "δᴱ", tickmode = "linear", tick0 = 0, dtick = 0.01),
        yaxis_title_text = "Penalty Cost (\$)",
        yaxis2 = attr(
            title = "Fixed Cost (\$)",
            showlegend=false,
            overlaying = "y",
            side = "right"
        ), 
        legend = attr(
            x = 1,
            y = 1.02,
            yanchor = "bottom",
            xanchor = "right",
            orientation = "h"
        )


    )
)

The plot I obtained has the correct axis, however, the line chart is behind the bar chart, and I wish it was the opposite (I’d like the bar chart to be behind the line chart).

Any ideas on how I can fix that?

Your bars cover the lines because in yaxis2 definition you have

overlaying = "y"

Comment out this line, and add to Layout:

yaxis_overlaying="y2",
1 Like

Thank you very much! Now it looks exactly how I wanted it!