在plot.ly中将y轴格式化为百分比

Wes*_*ssi 13 javascript plotly

如何在plot.ly中将y轴格式化为百分比?在var layout我有y轴的以下设置:

   yaxis: {
            hoverformat: ",.0%"
          },
Run Code Online (Sandbox Code Playgroud)

这会将悬停更改为百分比,但y轴上打印的值仍然是0-1而不是0-100.

任何帮助深表感谢.

Max*_*ers 19

要更改您需要设置的y轴格式tickformat,不要hoverformat.

var trace1 = {
  x: [1, 2, 3, 4],
  y: [0.10, 0.15, 0.43, 0.17],
  type: 'scatter'
};

var trace2 = {
  x: [1, 2, 3, 4],
  y: [0.16, 0.5, 0.11, 0.9],
  type: 'scatter'
};

var layout = {
  yaxis: {
    tickformat: ',.0%',
    range: [0,1]
  }
}

var data = [trace1, trace2];
Plotly.newPlot('myDiv', data, layout);
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<div id="myDiv" style="width: 480px; height: 400px;">
Run Code Online (Sandbox Code Playgroud)

  • 如果你想显示为"99.8%,99.9%"等,那么你应该设置`tickformat:'.1%'`,其中`1`确定小数位数. (5认同)

jef*_*ale 9

对于其他人:如果您将 Plotly Express 或 Cufflinks 与 Plotly 一起使用,您可以返回图形对象并yaxis.tickformat使用以下内容进行调整:

`my_fig.layout.yaxis.tickformat = ',.0%'`    
Run Code Online (Sandbox Code Playgroud)

正如@shrmn 在评论中指出的那样,用不同的数字替换零,然后将显示小数位数。