Multiple Y-Axis Domain Default and Lower Y-Axes Visibility Bug
High Level Description
I've encountered a bug related to the behavior of a y-axis when setting its domain manually and/or trying to hide some axes while showing others.
When a plotly figure object's layout.yaxis.domain is set to [0.0, 0.0] or [0.5, 0.5] or [1, 1], or any values where the difference is 0 will result in the figure actually displaying the yaxis as if it had a domain of [0, 1]. Further compounding the trickiness of this bug if you inspect the figure objects layout.yaxis.domain it will not show [0, 1] but it will show the desired domain that was input in the assignment.
More Details
To further add confusion to this bug, if the layout.yaxis.visible property is set to false then it will hide the y-ticks, y-axis-splines, y-axis-title, etc. but it will still show the background/overlap any traces or shapes that are on a y-axis that have visible: true and are below the hidden y-axis.
For example
Lets say I plot two y-axes y1 and y2, each with 1 trace on them metric1 and metric2 respectively. y1 with domain [0.0, 0.5] and y2 with domain [0.4, 1.0] but I try to hide everything related to y2. Nothing from y2 will show on the plot but y1 and the trace on y1, metric1 will be overlapped with the background where y2 overlaps it (the domain of [0.4, 0.5]).
const x = [0,1,2];
const values = {
metric1: [1,2,3],
metric2: [4,4,4],
};
var trace1 = {
x: x,
y: values['metric1'],
yaxis: 'y1',
name: 'metric1',
visible: true
};
var trace2 = {
x: x,
y: values['metric2'],
yaxis: 'y2',
name: 'metric2',
visible: false
};
var layout = {
yaxis: {title: 'metric1', domain: [0,0.5], visible: true},
yaxis2: {title: 'metric2', domain: [0.4,1], visible: false},
}
var data = [trace1, trace2];
Plotly.newPlot('myDiv', data, layout, {editable: true});
CodePen Example: https://codepen.io/Zachary-Foltz/pen/vYPNwyj
This doesn't seem like a big problem since you wouldn't normally specify overlapping axes but this actually results in a more sinister bug when combined with the issue mentioned above. Trying to set an axes domain to [0,0] or any other span who's difference is 0 will result in the figure displaying as if the domain was set to [0,1]. If the domain is set to [0,1] for an axis who is "above" another axis it will overlap the entire figure and nothing on any axes below it will be displayed even if the axis visible property is false. Notice in the example below the y-axis is correct for metric1 but there is not trace!
const x = [0,1,2];
const values = {
metric1: [1,2,3],
metric2: [4,4,4],
};
var trace1 = {
x: x,
y: values['metric1'],
yaxis: 'y1',
name: 'metric1',
visible: true
};
var trace2 = {
x: x,
y: values['metric2'],
yaxis: 'y2',
name: 'metric2',
visible: false
};
var layout = {
yaxis: {title: 'metric1', domain: [0,1], visible: true},
yaxis2: {title: 'metric2', domain: [0,0], visible: false},
}
var data = [trace1, trace2];
Plotly.newPlot('myDiv', data, layout, {editable: true});
CodePen Example: https://codepen.io/Zachary-Foltz/pen/wvOKbeZ
Implications of the bug
So let's say we want to call Plotly.update using a custom button such as in this example from the docs https://plotly.com/javascript/custom-buttons/#update-button
We want the button to hide our y2 y-axis and metrics2 trace from the example above.
To me the most straightforward way to do this is:
- set the trace
data.trace2.visibleproperty tofalse - set the
layout.yaxis2.visibleproperty tofalse - set the
layout.yaxis2.domainto what? `[0,0]' seems like a logical choice just to get it out of the way. The code for this would look like:
const x = [0,1,2];
const values = {
metric1: [1,2,3],
metric2: [4,4,4],
};
var trace1 = {
x: x,
y: values['metric1'],
yaxis: 'y1',
name: 'metric1'
};
var trace2 = {
x: x,
y: values['metric2'],
yaxis: 'y2',
name: 'metric2'
};
const buttons = [
{
args: [
{visible:[true,true]},
{
yaxis: {title: 'metric1', domain: [0, 0.5], visible: true},
yaxis2: {title: 'metric2', domain: [0.5, 1.0], visible: true},
}
],
label: 'all',
method: 'update'
},
{
args: [
{visible:[true,false]},
{
yaxis: {title: 'metric1', domain: [0.0, 1], visible: true},
yaxis2: {title: 'metric2', domain: [0.0, 0.0], visible: false},
}
],
label: 'metric1',
method: 'update'
},
{
args: [
{visible:[false,true]},
{
yaxis: {title: 'metric1', domain: [0.0, 0.0], visible: false},
yaxis2: {title: 'metric2', domain: [0.0, 1.0], visible: true},
}
],
label: 'metric2',
method: 'update'
}
];
var layout = {
updatemenus: [{buttons: buttons, type: 'buttons'}],
yaxis: {title: 'metric1', domain: [0.0, 0.5], visible: true},
yaxis2: {title: 'metric2', domain: [0.5, 1.0], visible: true},
}
var data = [trace1, trace2];
Plotly.newPlot('myDiv', data, layout, {editable: true});
CodePen Example: https://codepen.io/Zachary-Foltz/pen/bGZVyMX
As mentioned above this results in a blank figure when we click the metric1 button since the y2 axis is overlapping it. You may notice that the metric2 button works. This extends to any number of buttons and axis, where only the top level axis will work correctly because setting the domain of an axis to [0, 0] will result in a figure with the axis domain actually [0, 1]. Further complicating things if you log the domain from the CodePen above you can see that the figure object reports the domain is [0, 0] even though the visual display does not reflect that!
Other things that also do not work are:
- Removing the domain specification all together in the
button.argsproperty for axes intended to be hidden.
// same setup as above
const buttons = [
{
args: [
{
// same code for 'all' button as it works since we aren't hiding anything
},
{
args: [
{visible:[true,false]},
{
yaxis: {title: 'metric1', domain: [0.0, 1], visible: true},
yaxis2: {title: 'metric2', visible: false},
}
],
label: 'metric1',
method: 'update'
},
{
args: [
{visible:[false,true]},
{
yaxis: {title: 'metric1', visible: false},
yaxis2: {title: 'metric2', domain: [0.0, 1.0], visible: true},
}
],
label: 'metric2',
method: 'update'
}
];
// same code to plot the figure as examples above
- Setting either the domain or the whole yaxis object to
nullfor axes intended to be hidden.
// same setup as examples above
const buttons = [
{
args: [
{
// same code for 'all' button as it works since we aren't hiding anything
},
{
args: [
{visible:[true,false]},
{
yaxis: {title: 'metric1', domain: [0.0, 1], visible: true},
yaxis2: null,
}
],
label: 'metric1',
method: 'update'
},
{
args: [
{visible:[false,true]},
{
yaxis: null,
yaxis2: {title: 'metric2', domain: [0.0, 1.0], visible: true},
}
],
label: 'metric2',
method: 'update'
}
];
// same code to plot the figure as in the examples above
Current Workaround
You can get the functionality of hiding an axis by setting the domain of the axes to be very close to 0 span on the edge of the figure such as [0.0, 0.02] or [0.98, 1.00].
// same setup code as examples above
const buttons = [
{
args: [
{
// same code as above for 'all' button
},
{
args: [
{visible:[true,false]},
{
yaxis: {title: 'metric1', domain: [0.00, 1.00], visible: true},
yaxis2: {title: 'metric2', domain: [0.01, 0.02], visible: false},
}
],
label: 'metric1',
method: 'update'
},
{
args: [
{visible:[false,true]},
{
yaxis: {title: 'metric1', domain: [0.00, 0.02], visible: false},
yaxis2: {title: 'metric2', domain: [0.00, 1.00], visible: true},
}
],
label: 'metric2',
method: 'update'
}
];
//same code as above to plot
CodePen Example: https://codepen.io/Zachary-Foltz/pen/qBvOvrL
Questions for implementation of the fix
If I get some time, I will try to implement a fix for this bug, however, there are some questions regarding the desired way one should go about hiding an axis.
- Is setting the domain
[0.0, 0.0]a good way to clear the axis? Or should the current functionality of defaulting to a[0, 1]domain be preserved for some reason? Should we have to do something else such as set the domain to null? The axis to null? - Should the axis be cleared regardless of the domain setting if
visibleis set tofalse?
It seems that specifying the layout.yaxis.overlaying property for higher level axes that you want to hide fixes this issue. So I'm no longer sure if this is actually a bug then.
For completeness, setting the overlaying prop causes the respective axis domain to match the axis that it is set to overlay. This isn't a problem but in the custom button example above, you would need to specify the y2 axis is NOT overlaying for the 'all' button since you want to control the domain but then specify layout.yaxis2.overlaying: 'y1' for the 'metric1' button.
Also if you specify layout.yaxis1.overlaying: 'y1' it will reproduce the overlap bug strangely enough. So it is important to make sure an axis is not set to overlay itself or it will cause the overlapping hidden axis bug mentioned above.