Bokeh悬停工具提示:我怎么看不到图形上为零的值?

2024-06-26 14:48:02 发布

您现在位置:Python中文网/ 问答频道 /正文

Pie chart

如上图所示,即使P1值为0,工具提示也会显示P1和P2。当我把鼠标悬停在P3上时也是如此。如果值为0,如何确保悬停工具提示不会显示值?在这种情况下,在悬停时不能看到P1值,只能看到P2和P3。在

以下是我所说的定义:

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

Tags: 工具nonedataindexplotvaluecolorcolors
2条回答

因为您没有提供代码,所以最简单的方法可能是在绘图之前,从您正在构建ColumnDataSource的数据中删除0值切片。在

这应该能解决你的问题。这段代码按照Paul的建议删除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
    }
    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)

不删除0项的图例的另一个解决方法:

^{pr2}$

相关问题 更多 >