Home > Software engineering >  NOT have axes ranges dynamically update when toggling a trace in plotly (turn off axis autoscaling)?
NOT have axes ranges dynamically update when toggling a trace in plotly (turn off axis autoscaling)?

Time:01-20

The default behavior of plotly is to scale axes automatically upon toggling a trace. How to counter this behavior?

library(plotly)
library(reshape2)

data("tips")

fig <- plot_ly(tips, x = ~total_bill, y = ~tip, type = 'scatter', mode = 'markers', split = ~sex) %>%
  layout( plot_bgcolor='#e5ecf6',
          xaxis = list(
            zerolinecolor = '#ffff',
            zerolinewidth = 2,
            gridcolor = 'ffff'),
          yaxis = list(
            zerolinecolor = '#ffff',
            zerolinewidth = 2,
            gridcolor = 'ffff'))
fig

enter image description here

CodePudding user response:

An option is to disable the autorange and specify a range for both axis like this:

library(plotly)
library(reshape2)

data("tips")

fig <- plot_ly(tips, x = ~total_bill, y = ~tip, type = 'scatter', mode = 'markers', split = ~sex) %>%
  layout( plot_bgcolor='#e5ecf6',
          xaxis = list(
            zerolinecolor = '#ffff',
            zerolinewidth = 2,
            gridcolor = 'ffff',
            autorange = FALSE,
            range = c(0, 50)),
          yaxis = list(
            zerolinecolor = '#ffff',
            zerolinewidth = 2,
            gridcolor = 'ffff', 
            autorange = FALSE,
            range = c(0, 7)))
fig

Created on 2023-01-19 with reprex v2.0.2

When you click on the legend, you will see that it doesn't autoscale the axis.

  • Related