Format y-axis as percent in plot.ly
In order to change the format of the y-axis you need to set tickformat
, not 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;">
For others: if you are using Plotly Express or Cufflinks with Plotly, you can return the figure object and adjust the yaxis.tickformat
with the following:
my_fig.layout.yaxis.tickformat = ',.0%'
As pointed out by @shrmn in the comments, substitute a different number for zero and that number of decimal places will be displayed.