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

问题描述 投票:10回答:2

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

   yaxis: {
            hoverformat: ",.0%"
          },

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

任何帮助深表感谢。

javascript plotly
2个回答
15
投票

要更改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);
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<div id="myDiv" style="width: 480px; height: 400px;">

1
投票

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

my_fig.layout.yaxis.tickformat = ',.0%' .    

正如@shrmn在评论中指出的那样,将一个不同的数字替换为零,将显示该小数位数。

© www.soinside.com 2019 - 2024. All rights reserved.