Search code examples
pythonhovertooltipbokehpie-chart

Bokeh hover tooltips: How do I not see values that are zero on a graph?


Pie chart

As seen in the pie chart image above, tooltips is showing P1 along with P2, even if P1 value is 0. Same is the case when I hover over P3. How can I make sure a value is not shown by hover tooltips if the value is 0? In this case P1 value should not be seen on hover but only P2 and P3.

Here is the definition I am calling:

def create_priority_graph(P1, P2, P3):

    x = {
        'P1': P1,
        'P2': P2,
        'P3': P3
    }
    colors = ["#e84d60", "#f2c707", "#718dbf"]
    data = pd.Series(x).reset_index(name='value').rename(columns={'index':'toolscore'})
    data['angle'] = data['value']/data['value'].sum() * 2*pi
    data['color'] = colors
    p = figure(plot_height=250, plot_width=300, title="Open Issues by priority", toolbar_location=None,tools="hover", tooltips="@toolscore: @value", x_range=(-0.5, 1.0))
    p.wedge(x=0, y=1, radius=0.35,start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),line_color="white", fill_color='color', legend='toolscore', source=data)
    p.axis.axis_label=None
    p.axis.visible=False
    p.grid.grid_line_color = None

    return p

Solution

  • This should fix your problem. This code drops 0-value slices as Paul suggested.

    import pandas as pd
    from bokeh.plotting import figure
    from bokeh.io import output_file, show
    from bokeh.models.glyphs import Wedge
    import math
    from bokeh.transform import cumsum
    
    def create_priority_graph(P1, P2, P3):
        x = {
            'P1': P1,
            'P2': P2,
            'P3': P3
        }
        colors = ["#e84d60", "#f2c707", "#718dbf"]
        data = pd.Series(x).reset_index(name='value').rename(columns={'index':'toolscore'})
        data['angle'] = data['value']/data['value'].sum() * 2*math.pi
        data['color'] = colors
        data = data[data.value != 0]
        p = figure(plot_height=250, plot_width=300, title="Open Issues by priority", toolbar_location=None,tools="hover", tooltips="@toolscore: @value", x_range=(-0.5, 1.0))
        p.wedge(x=0, y=1, radius=0.35,start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),line_color="white", fill_color='color', legend='toolscore', source=data)
        p.axis.axis_label=None
        p.axis.visible=False
        p.grid.grid_line_color = None
        return p
    
    p = create_priority_graph(3, 9, 0)
    
    show(p)
    

    Another workaround where it does not remove the legend for the item that's 0:

    import pandas as pd
    from bokeh.plotting import figure
    from bokeh.io import output_file, show
    from bokeh.models.glyphs import Wedge
    import math
    from bokeh.transform import cumsum
    
    def create_priority_graph(P1, P2, P3):
        x = {
            'P1': P1,
            'P2': P2,
            'P3': P3
        }
        for i in x.keys():
            if x[i] == 0:
                x[i] = 0.0001
        colors = ["#e84d60", "#f2c707", "#718dbf"]
        data = pd.Series(x).reset_index(name='value').rename(columns={'index':'toolscore'})
        data['angle'] = data['value']/data['value'].sum() * 2*math.pi
        data['color'] = colors
        p = figure(plot_height=250, plot_width=300, title="Open Issues by priority", toolbar_location=None,tools="hover", tooltips="@toolscore: @value", x_range=(-0.5, 1.0))
        p.wedge(x=0, y=1, radius=0.35,start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),line_color="white", fill_color='color', legend='toolscore', source=data)
        p.axis.axis_label=None
        p.axis.visible=False
        p.grid.grid_line_color = None
        return p
    
    p = create_priority_graph(3, 9, 0)
    
    show(p)