如何让我的 Plotly 子图使用相同的 y 轴范围?

How can I make my Plotly subplots use the same y-axis range?

我有一个散点图,其中有 3 个共享 x 轴的子图。我希望每个子图对 y 轴使用相同的范围,即从 0 开始。我试过使用 rangemode='tozero',但这不起作用。任何见解将不胜感激!

https://codepen.io/kbreuer/pen/XWWqwde

var trace1 = {
  x: [0, 1, 2],
  y: [2, 3, 4],
  type: 'scatter'
};

var trace2 = {
  x: [2, 3, 4],
  y: [3, 4, 5],
  yaxis: 'y2',
  type: 'scatter'
};

var trace3 = {
  x: [3, 4, 5],
  y: [1, 2, 3],
  yaxis: 'y3',
  type: 'scatter'
};

var data = [trace1, trace2, trace3];

var layout = {
  yaxis: {rangemode: 'tozero'},
  y2axis: {rangemode: 'tozero'},
  y3axis: {rangemode: 'tozero'},
  grid: {
    rows: 3,
    columns: 1,
    subplots: [['xy'], ['xy2'], ['xy3']]
   },
};

Plotly.newPlot('myDiv', data, layout);

你快搞定了。您的 layout 定义只有一个小问题。

y2axisy3axis 都不会被识别,因为我相信 Plotly 采用了约定,它希望任何附加轴的标识号都带有后缀。也就是说,你要的是:

var layout = {
  yaxis: {rangemode: 'tozero'},
  yaxis2: {rangemode: 'tozero'},
  yaxis3: {rangemode: 'tozero'},
  grid: {
    rows: 3,
    columns: 1,
    subplots: [['xy'], ['xy2'], ['xy3']]
   },
};