【问题标题】:Create Legend Label for Quad glyph - Bokeh为四边形字形创建图例标签 - 散景
【发布时间】:2020-08-23 08:36:59
【问题描述】:

我有一个显示 2 个数据集的四边形图。我想在情节中添加一个图例,但是我不确定如何使用 Quad 字形来做到这一点。

之前的示例使用了“legend”,但现在已弃用,我尝试使用 'legend_label' 但是这是行不通的。 我的最终目标是使用图例以交互方式显示两个数据集

    # Convert dataframe to column data source
    src1 = ColumnDataSource(Merged_Bins)
    src2 = ColumnDataSource(Merged_Bins)

    #------------------------------------------------------------------------------------------------
    # Plot Histogram using Bokeh plotting library
    #------------------------------------------------------------------------------------------------

    plot = figure(y_range=Range1d(start=0, end=Max_Histogram_Value),sizing_mode="scale_width",width=3000,height= 600,
                  title= "Histogram Plot",
                  x_axis_label="Time (ms)",
                  y_axis_label="Count",toolbar_location = "below")
    plot.yaxis.ticker = FixedTicker(ticks=list(tick_vals))


    glyph1=Quad(bottom=0, top='Delay1', left='left1',
              right='right1', fill_color='#FF7F00',
              line_color='black', fill_alpha=0.7,line_alpha=0.5,name="Option 2")
              
    glyph1_plot=plot.add_glyph(src1, glyph1)
    glyph2=Quad(bottom=0, top='Delay2', left='left2',
              right='right2', fill_color='#616261',
              line_color='#616261',line_alpha=0.1, fill_alpha=0.1,name="Original Design")
              
    plot.add_glyph(src2, glyph2)

    # Add hover tool for when mouse is over data
    hover1 = HoverTool(tooltips=[('Delay Envelope', '@Bin_interval'),('Count', '@Delay1'),('Count Original', '@Delay2')],mode='vline',renderers=[glyph1_plot])
    plot.add_tools(hover1)
    plot.legend.location = "top_left"
    plot.legend.click_policy="hide"
    # Set autohide to true to only show the toolbar when mouse is over plot
    plot.toolbar.autohide = True
    script, div = components(plot)
    show(plot)

【问题讨论】:

    标签: python plot data-science bokeh


    【解决方案1】:

    如果您使用Figure.quad 方法而不是使用显式创建的Quad 实例手动调用Figure.add_glyph,它就可以正常工作。所有legen_* 参数都由Figure 类的字形方法解析——字形类本身根本不使用它们。

    from bokeh.io import show
    from bokeh.plotting import figure
    
    p = figure()
    p.quad(-1, 1, 1, -1, legend_label='Hello')
    p.quad(1, 3, 3, 1, color='green', legend_label='there')
    
    show(p)
    

    或者,如果您出于某种原因确实需要手动方法,您还可以通过创建 Legend 类的实例并将其添加到带有 Figure.add_layout 的图形中来手动创建图例。

    另外,在不相关的注释中 - 您的绘图看起来像是使用 vbar 而不是 quad 创建的,因为所有条形似乎都具有相同的宽度。如果是这样,也许在您的情况下使用vbar 会更简单。

    【讨论】:

      猜你喜欢
      • 2015-05-19
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多