You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何关闭Plotly切换Trace时的坐标轴自动缩放功能?

取消Plotly切换轨迹时自动缩放坐标轴的方法

要取消Plotly切换轨迹时自动缩放坐标轴的默认行为,只需在布局的坐标轴配置中添加fixedrange = TRUE参数,该参数会锁定坐标轴范围,切换轨迹时不会随显示的数据自动调整。

修改你提供的代码如下:

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',
            fixedrange = TRUE),  # 添加此行锁定x轴范围
          yaxis = list(
            zerolinecolor = '#ffff',
            zerolinewidth = 2,
            gridcolor = 'ffff',
            fixedrange = TRUE))  # 添加此行锁定y轴范围
fig

添加fixedrange = TRUE后,无论点击图例切换显示男性/女性的小费数据,x轴(账单总额)和y轴(小费金额)的范围都会保持初始状态,不会自动缩放适配当前显示的数据集。

内容的提问来源于stack exchange,提问作者Emile Zäkiev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 19:20:27