最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Show full axis range for Plotly chart? - Stack Overflow

programmeradmin1浏览0评论

How can you show from 0,0 (or the lowest negative point) to 8,8 (or the highest data point) on the chart x,y while also showing the full chart?

var data = [
  {
    x: [3, 4, 5, 6, 7, 8],
    y: [3, 4, 5, 6, 7, 8],
    type: 'scatter'
  }
];
var layout = {
  // autosize: true,
  // rangemode: "tozero",
  width: 500,
  height: 500,
  autoscale: false,
  xaxis: { autorange: false },
  yaxis: { autorange: false },
};
Plotly.newPlot('myDiv', data, layout, {showSendToCloud: true});

If you enable autorange it shows the data, but not 0,0.

If you disable autorange it shows 0,0 but not the data.

Here is a working demo:

How can you show from 0,0 (or the lowest negative point) to 8,8 (or the highest data point) on the chart x,y while also showing the full chart?

var data = [
  {
    x: [3, 4, 5, 6, 7, 8],
    y: [3, 4, 5, 6, 7, 8],
    type: 'scatter'
  }
];
var layout = {
  // autosize: true,
  // rangemode: "tozero",
  width: 500,
  height: 500,
  autoscale: false,
  xaxis: { autorange: false },
  yaxis: { autorange: false },
};
Plotly.newPlot('myDiv', data, layout, {showSendToCloud: true});

If you enable autorange it shows the data, but not 0,0.

If you disable autorange it shows 0,0 but not the data.

Here is a working demo: https://codepen.io/Xeoncross/pen/MRREXq

Share Improve this question asked Apr 29, 2019 at 17:14 XeoncrossXeoncross 57.3k83 gold badges271 silver badges371 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Your data is all there and showing it is just "hidden" because of the default zoom level. If you want all of the data points to show at once by default you can set a range in your x and y axis.

Also, setting a range will not affect your zooming in future uses.

Basically you can set the range min and max to be 0 and your highest value.

xaxis: { autorange: false, range:[0, 10]},
yaxis: { autorange: false, range:[0, 10]},



In the following snippet I made two variables to track the x and y max and add 2. This will ensure there is a buffer of 2 on the top to show all of the data points.

var data = [
  {
    x: [3, 4, 5, 6, 7, 8],
    y: [3, 4, 5, 6, 7, 8],
    type: 'scatter'
  }
];

var xMax = data[0].x[data[0].x.length - 1] + 2;
var yMax = data[0].y[data[0].y.length - 1] + 2;
var layout = {
  // autosize: true,
  // rangemode: "tozero",
  width: 500,
  height: 500,
  autoscale: false,
  xaxis: { autorange: false, range:[0, xMax]},
  yaxis: { autorange: false, range:[0, yMax]},
};

Plotly.newPlot('myDiv', data, layout, {showSendToCloud: true});
<head>
  <!-- Plotly.js -->
  <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
  <!-- jquery -->
  <script src="//cdnjs.cloudflare./ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<!-- Plotly chart will be drawn inside this DIV -->
<div id="myDiv"></div>
</body>


You can just set the rangemode to 'tozero' and enable autorange for each axis:

var data = [
  {
    x: [3, 4, 5, 6, 7, 8],
    y: [3, 4, 5, 6, 7, 8],
    type: 'scatter'
  }
];
var layout = {
  width: 500,
  height: 500,
  autoscale: false,
  xaxis: { rangemode:'tozero', autorange:true},
  yaxis: { rangemode:'tozero', autorange:true}
};

Plotly.newPlot('myDiv', data, layout, {showSendToCloud: true});
<head>
  <!-- Plotly.js -->
  <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
  <!-- jquery -->
  <script src="//cdnjs.cloudflare./ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<!-- Plotly chart will be drawn inside this DIV -->
<div id="myDiv"></div>
</body>

See the official Plotly examples.

发布评论

评论列表(0)

  1. 暂无评论