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

Plotly.relayout() and multiple yaxis handling

I upgraded plotly.js recently and my app stopped working properly. I use multiple Y axes approach - the default yaxis is the only visible but has no related trace. All traces have their own yaxisN overlying the default one (yaxis). The reason is that yaxesN have autorange set on by default but user can change it and set custom range (for each trace).

The behavior in handling of multiple yaxis has changed since v1.32.0.

In v1.31.2 when I added layouts for yaxis2, yaxis3, etc using Plotly.relayout() function they were added - I could see them in _fullLayout object as properties yaxis2, yaxis3, etc.

Since v1.32.0 they are not added until there is a trace referencing them (e.g. for layout for yaxis2 there must be a trace with property yaxis: ‘y2’). If no trace has its ‘yaxis’ set to ‘y2’, the ‘yaxis2’ property does not exist in _fullLayout. It looks like some kind of optimization.

The problem is that it works like this even for the default yaxis - i.e. if there is no trace with ‘yaxis’ property set to value of ‘y’ but there is another trace with ‘yaxis’ set to ‘y2’, the ‘yaxis’ is removed from _fullLayout despite yaxis2 is overlaying ‘y’.

Is this behavior by design?

I’m asking because I need to fix my app and need to decide how. So far I found the only solution to add empty trace (it references yaxis by default, which ensures the yaxis is not removed from layout).

In following codepen you can compare the behavior: https://codepen.io/exavier/pen/YQKmWM?editors=1010
It uses the latest plotly.js version, but if you change the reference version in HTML to v1.31.2 you can see the difference. (_fullLayout and _fullData are logged to console).

1 Like

Thanks for report. This is most likely a regression caused by https://github.com/plotly/plotly.js/pull/2227

Adding an invisible first trace (simply by making data = [{}] instead of []) gets your codePen back to its desired behavior: https://codepen.io/etpinard/pen/eVmVNG?editors=1010

Yes, I’m afraid it is by design. Axes that aren’t referenced by anything (a trace, even an invisible trace; an annotation; a shape; an image) are not drawn. The reason for this is to help with plots that change trace type. If you have a bar chart and you turn it into a pie, for example, you do not want to see the axes. But we didn’t want to completely delete the axis objects - if you change your mind and wisely convert back to bars it’s helpful to retain whatever styling you had already applied.

Apologies that this broke your app - we weren’t aware of folks expecting these orphan axes to appear. Fortunately the fix should be fairly easy…

OK, thanks.

Just an idea - perhaps yaxis could be considered referenced if there is another yaxisN having property overlaying: "y"