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

Plotly双Y轴图例移至底部时右侧次要坐标轴被截断问题问询

解决方案

核心原因是绘图区域的边距未适配底部图例和右侧Y轴的显示需求,通过调整layout相关参数即可实现需求,修复后可复现代码如下:

library(dplyr)
library(plotly)

plot_ly() %>% 
  add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10") %>% 
  add_lines(x = ~2:4, y = ~1:3, name = "slope of 1", yaxis = "y2") %>% 
  layout(
    title = "Double Y Axis", 
    yaxis = list(title = "first y axis", title.standoff = 10),
    yaxis2 =  list(tickfont = list(color = "red"),
                   overlaying = "y",
                   side = "right",
                   title = "second y axis",
                   title.standoff = 10),
    xaxis = list(title="x"),
    legend = list(orientation = 'h', x = 0.5, xanchor = "center", y = -0.15),
    margin = list(b = 50, r = 50)
  )

关键修改说明

  • margin参数:b设置底部边距为50像素,预留图例展示空间;r设置右侧边距为50像素,避免右侧Y轴刻度、标题被截断
  • 图例参数:将x设为0.5、xanchor设为center,实现图例自动水平居中,无需手动设置固定偏移值,适配不同宽度的响应式场景
  • title.standoff参数:给两个Y轴分别配置该参数,设置轴标题与刻度标签的间距,避免二者重叠

内容的提问来源于stack exchange,提问作者Annet

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.20 10:24:01