plotly / plotly.js

Open-source JavaScript charting library behind Plotly and Dash
https://plotly.com/javascript/
MIT License
17.06k stars 1.87k forks source link

plotly wrongly automatically converts strings to integers if possible #7147

Open toolslive opened 1 month ago

toolslive commented 1 month ago
<head>
    <script src='https://cdn.plot.ly/plotly-2.35.0.min.js'></script>
</head>
<body>
    <div id='myDiv'>
    </div>
    <script>
     //var x = [1,2,3,4] // interpreted as number
     //var x = ['a','b','c', 'd']; // used as labels
     var x = ['-1','1e1', '0x64']; // now they're numbers :(
     var trace1 = {
         x: x,
         y: [10, 15, 13, 17],
         type: 'scatter'
     };

     var trace2 = {
         x: x,
         y: [16, 5, 11, 9],
         type: 'scatter'
     };

     var data = [trace1, trace2];

     Plotly.newPlot('myDiv', data);

    </script>
</body>

plotly_ducktyping

It should just use strings as labels (just like what happens when x= ['a','b','c','d'] ). A simple work around is to prefix the strings with &nbsp; to sabotage the convertion.

my-tien commented 1 month ago

You can set layout.xaxis.type = 'category' to keep the strings.

alexcjohnson commented 1 month ago

You can also set layout.autotypenumbers = 'strict' (which is currently in the default plotly.py template, but in plain plotly.js the default is 'convert types') https://plotly.com/javascript/reference/layout/#layout-autotypenumbers