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

R中Plotly绘制Y轴旋转体3D图:子图重叠问题求助

解决Plotly中3D旋转体子图重叠问题

当使用Plotly的subplot()函数组合两个3D图形时,默认会共享画布空间导致重叠,核心原因是3D场景不会自动分配独立布局区域。以下是具体解决方法:

关键修改步骤

给每个3D子图的场景(scene)设置独立的domain参数,明确其在画布上的位置范围,同时通过margin参数预留子图间的空隙。

修改后的完整代码片段

# 创建sigma=1的图后,添加布局配置
figsigma1 <- plot_ly(df1,
        type = 'scatter3d',
        mode='markers',
        x = ~X, 
        y = ~Y,
        z = ~Z,
        marker = list(
          color= ~Y,
          colorscale = "Viridis",
          cmax = 0.4,
          cmin = 0,
          colorbar=list(
            title='Intensity (AU)'
          ),
          reversescale =F
        )
) %>%
  layout(scene = list(
    domain = list(x = c(0, 0.45)), # 左侧区域
    xaxis = list(title = "X"),
    yaxis = list(title = "Y"),
    zaxis = list(title = "Z")
  ))

# 创建sigma=2的图后,添加布局配置
figsigma2 <- plot_ly(df2,
                     type = 'scatter3d',
                     mode='markers',
                     x = ~X, 
                     y = ~Y,
                     z = ~Z,
                     marker = list(
                       color= ~Y,
                       colorscale = "Viridis",
                       cmax = 0.4,
                       cmin = 0,
                       colorbar=list(
                         title='Intensity (AU)'
                       ),
                       reversescale =F
                     )
) %>%
  layout(scene = list(
    domain = list(x = c(0.55, 1)), # 右侧区域
    xaxis = list(title = "X"),
    yaxis = list(title = "Y"),
    zaxis = list(title = "Z")
  ))

# 组合子图并设置间距
main_plot <- subplot(figsigma1, figsigma2, margin = 0.05)
main_plot

原理说明

  • domain参数定义了每个3D场景在画布上的坐标范围(取值0到1),这里将第一个图限制在左侧45%的区域,第二个图放在右侧从55%开始的区域,中间预留10%的空隙避免挤压。
  • margin参数进一步调整子图整体的边距,确保布局更协调。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 15:42:36