0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

bokeh: グリッドレイアウトでグラフの幅が異なる場合の表示を確認した

Last updated at Posted at 2024-05-12

環境

  • Python 3.12.1
  • bokeh 3.4.1

確認したいこと

3つのグラフをグリッドレイアウトで表示したいです。

グラフs1,s2,s3を、以下のように2行2列のグリッドで表示します。

s1,s2
s3,empty

ただしs1s3の幅は異なります。このとき、グリッドレイアウトがどう表示されるかを以下の点を変えて確認しました。

Pythonスクリプト

散布図s1,s2の幅は200pxです。散布図s3だけ幅は300pxです。

sample.py
from bokeh.layouts import gridplot, grid, layout, column
from bokeh.plotting import figure, show, save, output_file, reset_output
from bokeh.models.widgets.markups import Div


def create_scatters() -> tuple[figure, figure, figure]:
    x = list(range(11))
    y = x

    s1 = figure(background_fill_color="#fafafa", width=200, height=200)
    s1.scatter(x, y)

    s2 = figure(background_fill_color="#fafafa", width=200, height=200)
    s2.scatter(x, y)

    s3 = figure(background_fill_color="#fafafa", width=300, height=200)
    s3.scatter(x, y)

    return s1, s2, s3


# bokeh.layouts.grid
s1, s2, s3 = create_scatters()
output_file("grid-1.html")
save(grid([[s1, s2], [s3, None]]))


s1, s2, s3 = create_scatters()
output_file("grid-2.html")
save(grid([[s1, s2], [s3]]))


# bokeh.layouts.gridplot
s1, s2, s3 = create_scatters()
output_file("gridplot-1.html")
save(gridplot([[s1, s2], [s3, None]]))


s1, s2, s3 = create_scatters()
output_file("gridplot-2.html")
save(gridplot([[s1, s2], [s3]]))

結果

bokeh.layouts.gridで出力する

grid-1.html

save(grid([[s1, s2], [s3, None]]))

image.png

s1s2の間に隙間ができました。

grid-2.html

save(grid([[s1, s2], [s3]]))

image.png

s1s2の間に隙間はできませんでした。

bokeh.layouts.gridplotで出力する

gridplot-1.html

save(gridplot([[s1, s2], [s3, None]]))

image.png

s1s2の間に隙間ができました。

gridplot-2.html

save(gridplot([[s1, s2], [s3]]))

image.png

s1s2の間に隙間ができました。

隙間が生まれるときと生まれないときでCSSはどう異なるのか

grid([[s1, s2], [s3, None]])を指定したときだけ、s1s2の間に隙間ができませんでした。
隙間ができるときとできないときで、HTML/CSSがどう違うのかを確認しました。

image.png

grid-1.htmlでは、s3に相当する<div class="bk-Figure" />のスタイルは以下のようになっていました。

grid-row-start: 2;
grid-row-end: span 1;
grid-column-start: 1;
grid-column-end: span 1;

一方grid-2.htmlでは、s3に相当するdiv要素のスタイルは以下のようになっていました。

grid-row-start: 2;
grid-row-end: span 1;
grid-column-start: 1;
grid-column-end: span 2;

grid-column-endの違いによって、隙間ができる/できないが変わることが分かりました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?