ujimushi(@旧sradjp(15364))の日記

旧スラドの日記の引越先です

Remove spacing between subplots in PlotlyJS

勝手に始まった「勝手に回答」シリーズ。今回はRemove spacing between subplots in PlotlyJSJulia Discourse forumの質問です。 内容は私のつたない英語能力で日本語に訳すと「PlotlyJSのsubplot間の空白を削除したいんだけどどうしたらいい?」

この質問の中で,add_trace!とかしていて,この!でレイアウトが変わることが多いので,グラフを一度に表示したら改善するのではないかと思いました。 で,うんうん考えて次のような感じのソースを作ってみました。

using PlotlyJS

function main()
    trace1 = scatter(;  x = rand(100), y = rand(100),
                     mode="markers", name="linear")

    trace2 = scatter(; x = rand(100), y = rand(100),
                     xaxis = "x2", yaxis = "y2",
                     mode="markers", name="linear",
                     textposition="top center")

    trace3 = scatter(y = rand(100), x = rand(100),
                     xaxis = "x3", yaxis = "y3",
                     mode="markers", name="linear",
                     textposition="top center")

    trace4 = scatter3d(
        z = rand(100), x = rand(100), y = rand(100),
        xaxis = "x4", yaxis = "y4", zaxis = "z4",
        mode="markers", name="linear", textposition="top center"
    )
    layout = Layout(
        height=1300, width=1500, title_text="specs examples", showlegend=false,
        Subplots(
            rows = 2, cols = 2,
            specs = [Spec(kind="xy") Spec(kind="xy");
                     Spec(kind="xy") Spec(kind="scene")],
            vertical_spacing=0.0, horizontal_spacing=0.0,
        )
    )
    plot([trace1,  trace2, trace3,  trace4], layout)
end

@time height_potential_fluorescence = main()

実行結果は次の通り。うまくsubplots間の空白を削除できました。

で,元のソースと見比べてふと思いました。

「これって元のソースのvertical_spacing=0horizontal_spacing=0make_subplotsに移動するだけでOKじゃね?」

で次のような変更を実施。

--- source.jl    2024-02-13 21:21:24.000000000 +0900
+++ changed.jl    2024-02-13 21:21:24.123456789 +0900
@@ -34,7 +34,7 @@
         specs=[
         Spec(kind="xy") Spec(kind="xy")
         Spec(kind="xy") Spec(kind="scene")
-    ]
+    ], vertical_spacing=0.0, horizontal_spacing=0.0
     )
 
     add_trace!(p, trace1, row=1, col=1)
@@ -42,8 +42,7 @@
     add_trace!(p, trace3, row=2, col=1)
     add_trace!(p, trace4, row=2, col=2)
 
-    relayout!(p, height=1300, width=1500, title_text="specs examples", showlegend=false,vertical_spacing=0., horizontal_spacing=0. )
-
+    relayout!(p, height=1300, width=1500, title_text="specs examples", showlegend=false)
     p
     
 end

そして実行結果は次の通り。

おんなじじゃねぇか 。はい,撤収~。