简体   繁体   中英

Plotly: same axes, but autoscale?

For a scatterplot in Plotly, is it possible to keep the axes flexible, but that y and x always start at the same range?

I am plotting a graph where distances are very important. Therefore, when x and y are different scales, it can be very misleading. However, it is not possible to fix them ahead of time.

Thanks!

Both the xaxis and yaxis layout properties, have a property called range , in this we can provide the max and min values, below are the properties you need to play around with to achieve what you want!

nticks (integer greater than or equal to 0)
default: 0
Specifies the maximum number of ticks for the particular axis. The actual number of ticks will be chosen automatically to be less than or equal to nticks. Has an effect only if tickmode is set to "auto".

tick0 (number or categorical coordinate string)
Sets the placement of the first tick on this axis. Use with dtick. If the axis type is "log", then you must take the log of your starting tick (eg to set the starting tick to 100, set the tick0 to 2) except when dtick="L" (see dtick for more info). If the axis type is "date", it should be a date string, like date data. If the axis type is "category", it should be a number, using the scale where each category is assigned a serial number from zero in the order it appears.

range (array)
Sets the range of this axis. If the axis type is "log", then you must take the log of your desired range (eg to set the range from 1 to 100, set the range from 0 to 2). If the axis type is "date", it should be date strings, like date data, though Date objects and unix milliseconds will be accepted and converted to strings. If the axis type is "category", it should be numbers, using the scale where each category is assigned a serial number from zero in the order it appears. Each object has one or more of the keys listed below.

dtick (number or categorical coordinate string)
Sets the step in-between ticks on this axis. Use with tick0 . Must be a positive number, or special strings available to "log" and "date" axes....

Check the official documentation also.

Here is a working example for reference.

 var trace1 = { x: [0, 1, 2, 3, 4, 5, 6, 7, 8], y: [8, 7, 6, 5, 4, 3, 2, 1, 0], type: 'scatter' }; var trace2 = { x: [0, 1, 2, 3, 4, 5, 6, 7, 8], y: [0, 1, 2, 3, 4, 5, 6, 7, 8], type: 'scatter' }; var data = [trace1, trace2]; var range = [1,8]; var layout = { xaxis: { range: range }, yaxis: { range: range } }; Plotly.newPlot('myDiv', data, layout); 
 <head> <!-- Plotly.js --> <script src="https://cdn.plot.ly/plotly-latest.min.js"></script> </head> <body> <!-- Plotly chart will be drawn inside this DIV --> <div id="myDiv" style="width: 100%; height: 500px;"></div> <script> /* JAVASCRIPT CODE GOES HERE */ </script> </body> 

If your issue is resolved completely, please let me know!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM