93365


Bokehを使って人口推移のグラフを作成しています。  
以下のコードを読み込むとグラフが表示され、右側に凡例が表示されます。  
ここでは `g`で定義している、凡例にある都市の名前と `tooltips`の表示を対応させたいのですが、方法がわかりません。  
具体的に言うと、ここでは紫の棒グラフとして表示される"Kyoto"のデータにカーソルを当てた時、"Kyoto"のデータであることがわかるように表示したいのです。

プログラミングは始めたばかりなので、説明が分かりにくい、言葉の使い方を間違えているなどあるかもしれませんが、  
どなたかご教示していただけると嬉しいです！

以下がコードになります。

[code]

    datak1=[59,67,76,108,108,99,110,120,128,136,141,146,147,147,146,146,146,147,147]
    datak2=[60,64,78,91,96,60,80,98,111,121,128,136,136,141,147,142,149,152,154]
    datas1=[10,14,16,19,20,25,31,42,52,79,101,124,140,154,167,175,182,188,191]
    datak3=[2,5,10,15,30,25,31,44,63,85,97,101,104,108,117,120,124,132,142]
    dataf=[9,14,22,29,30,32,39,54,64,74,85,100,108,116,123,128,134,140,146]
    datah=[16,19,27,31,34,22,28,35,43,50,54,85,89,104,108,110,112,115,117]
    datas2=[11,14,19,21,22,29,34,37,42,48,54,61,66,70,91,97,100,102,104]
    datas3=[3,5,6,8,11,22,24,32,37,48,60,72,78,82,90,96,102,117,122]
    data=[datak1,datak2,datas1,datak3,dataf,datah,datas2,datas3]
    import numpy as np
    from bokeh.plotting import figure, output_notebook, show
    output_notebook()
    from bokeh.palettes import d3
    from bokeh.models import Legend
    from bokeh.models.tools import HoverTool
    
    x = range(len(data[0]))
    g = ['T9','T14','S5','S10','S15','S22','S25','S30','S35','S40','S45','S50','S55','S60','H2','H7','H12','H17','H22']
    z = ["Kyoto","Kobe","Sapporo","Kawasaki","Fukuoka","Hiroshima","Sendai","Saitama"] 
    
    p = figure(x_range=g,toolbar_location="right",tools=[HoverTool()],
               sizing_mode="stretch_width",max_width=700, plot_height=250, 
               tooltips="In '@x' @z has @y million people",
              title='The Population Transition of Japanese City',
               x_axis_label='Year in Japanese Calender',
               y_axis_label='Population',
               width=800,height=350)
    p.line(g, data[0], line_color='purple', legend_label= "Kyoto")
    p.line(g, data[1], line_color='green', legend_label="Kobe")
    p.line(g, data[2], line_color="yellow", legend_label="Sapporo")
    p.line(g, data[3], line_color="orange", legend_label="Kawasaki")
    p.line(g, data[4], line_color="gray", legend_label="Fukuoka")
    p.line(g, data[5], line_color="red", legend_label="Hiroshima" )
    p.line(g, data[6], line_color="pink", legend_label="Sendai")
    p.line(g, data[7], line_color="Brown", legend_label="Saitama")
    
    p.legend.location = "center"
    p.legend.title = 'The Name of City'
    p.legend.title_text_font_style = "bold"
    p.legend.title_text_font_size = "15px"
    p.add_layout(p.legend[0], "right")
    
    show(p)
    
[/code]


CC BY-SA 4.0
2023-01-15T13:24:38.660
93359
2023-01-15T21:37:43.737
2023-01-15T15:49:11.127
3060
56585
question
python bokeh
Python の Bokeh で作成するグラフで、凡例とツールチップの表示を対応させたい

[code]

    from bokeh.plotting import figure, show, ColumnDataSource
    from bokeh.models.tools import HoverTool, CustomJSHover
    
    data = [[59, 67, 76, 108, 108, 99, 110, 120, 128, 136, 141, 146, 147, 147, 146, 146, 146, 147, 147],
            [60, 64, 78,  91,  96, 60,  80,  98, 111, 121, 128, 136, 136, 141, 147, 142, 149, 152, 154],
            [10, 14, 16,  19,  20, 25,  31,  42,  52,  79, 101, 124, 140, 154, 167, 175, 182, 188, 191],
            [ 2,  5, 10,  15,  30, 25,  31,  44,  63,  85,  97, 101, 104, 108, 117, 120, 124, 132, 142],
            [ 9, 14, 22,  29,  30, 32,  39,  54,  64,  74,  85, 100, 108, 116, 123, 128, 134, 140, 146],
            [16, 19, 27,  31,  34, 22,  28,  35,  43,  50,  54,  85,  89, 104, 108, 110, 112, 115, 117],
            [11, 14, 19,  21,  22, 29,  34,  37,  42,  48,  54,  61,  66,  70,  91,  97, 100, 102, 104],
            [ 3,  5,  6,   8,  11, 22,  24,  32,  37,  48,  60,  72,  78,  82,  90,  96, 102, 117, 122]]
    
    g = [ 'T9', 'T14',  'S5', 'S10', 'S15', 'S22', 'S25', 'S30', 'S35', 'S40',
         'S45', 'S50', 'S55', 'S60',  'H2',  'H7', 'H12', 'H17', 'H22']
    cities = ['Kyoto', 'Kobe', 'Sapporo', 'Kawasaki', 'Fukuoka', 'Hiroshima', 'Sendai', 'Saitama'] 
    colors = ['purple', 'green', 'yellow', 'orange', 'gray', 'red', 'pink', 'brown']
    
    x_custom = CustomJSHover(code=f'''
        const g = {g}
        return "" + g[special_vars.segment_index]
    ''')
    p = figure(x_range=g, toolbar_location='right', tools=[HoverTool(formatters={'@x': x_custom})],
               sizing_mode='stretch_width',max_width=700, max_height=250,
               tooltips="In '@x{custom}' @cities has $y million people",
               title='The Population Transition of Japanese City',
               x_axis_label='Year in Japanese Calender', y_axis_label='Population',
               width=800, height=350)
    
    source = ColumnDataSource(data=dict(x=[g]*len(data), y=data, color=colors, cities=cities))
    p.multi_line(xs='x', ys='y', line_color='color', legend_field='cities', source=source)
    
    p.legend.location = 'center'
    p.legend.title = 'The Name of City'
    p.legend.title_text_font_style = 'bold'
    p.legend.title_text_font_size = '15px'
    p.add_layout(p.legend[0], 'right')
    
    show(p)
    
[/code]

[![画像の説明をここに入力](https://i.stack.imgur.com/hKMWF.png)](https://i.stack.imgur.com/hKMWF.png)


CC BY-SA 4.0
2023-01-15T21:37:43.737
93365
2023-01-15T21:37:43.737
47127
93359
answer