检索Bokeh中直方图条高度所需的代码是什么?

2024-04-26 14:14:39 发布

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

目标:获取Bokeh(python)提供的悬停/工具提示中y轴的值。你知道吗

目前,我试过@y和@height都给我'???'悬停/工具提示中的值。我密切关注了Bokeh中围绕悬停/工具提示的大部分documentation,但没有结果。你知道吗

enter image description here

任何建议都会有帮助。。。你知道吗

下面是我使用的代码:

from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.sampledata.autompg import autompg as df

from numpy import histogram, linspace

from bokeh.models import HoverTool

# Information contained within the hoover

hist, edges = histogram(data['Age'], density=False, bins=10)

hover = HoverTool( tooltips="""
    <div style ="border-style: solid;border-width: 15px;border-color: gray;background-color:gray;padding:0">         
        <div>
            <span style="font-size: 12px; color: white;font-family:century gothic;">'@y'</span>
            <span style="font-size: 12px; color: white;font-family:century gothic;"> Observations</span>
        </div>
    </div style>

    """
)


p = figure(plot_height=300,tools=[hover])
p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:],line_color="white", line_width=1,color='gray',fill_alpha=.50)

p.title="Distribution of Age"

p.title_text_font = "Century Gothic"
p.title_text_font_style='normal'
p.title_location='above'
p.title_text_align= 'center'
p.xaxis.axis_label_text_font = "Century Gothic"
p.xaxis.axis_label_text_color = 'black'
p.xaxis.axis_label_text_font_style='normal'
p.yaxis.axis_label_text_font = "Century Gothic"
p.yaxis.axis_label_text_color = 'black'
p.yaxis.axis_label_text_font_style = "normal"
p.xaxis.axis_label_standoff = 10
p.yaxis.axis_label_standoff = 10

# Tufte style
p.background_fill_color = None

p.border_fill_color = None
p.min_border_left = 80

p.xaxis.major_tick_line_color = None  # turn off x-axis major ticks
p.xaxis.minor_tick_line_color = None  # turn off x-axis minor ticks

p.yaxis.major_tick_line_color = None  # turn off y-axis major ticks
p.yaxis.minor_tick_line_color = None  # turn off y-axis minor ticks

p.ygrid.grid_line_color = "gray"
p.ygrid.grid_line_alpha = 0.25
p.ygrid.grid_line_width = 1
p.xgrid.grid_line_color = None

p.yaxis.axis_line_color = None
p.xaxis.axis_line_color = None

# Remove outline of graph
p.outline_line_color = None

# Remove Bokeh logo
#p.toolbar.logo = None
p.toolbar_location = None
p.xaxis.axis_label = 'Age'
p.yaxis.axis_label = 'Count'

show(p)

Tags: textfromimportnonetitlestylelinebokeh
1条回答
网友
1楼 · 发布于 2024-04-26 14:14:39

为了@xxx工作,需要将数据源传递给glyph方法。我不确定$xxx是否应该在没有数据源的情况下工作,或者它是否是quadglyph的一个特性。你知道吗

你可以这样做:

# After you bin your data
h_data = pd.DataFrame({'Count': hist, 'Bin': edges[1:]})  # Adjust the edges selection as desired

# HoverTool definition
hover = HoverTool( tooltips="""
<div style ="border-style: solid;border-width: 15px;border-color: gray;background-color:gray;padding:0">         
    <div>
        <span style="font-size: 12px; color: white;font-family:century gothic;">@Count</span>
        <span style="font-size: 12px; color: white;font-family:century gothic;"> Observations</span>
    </div>
</div style>
""")

# Instead of the quad call
p.vbar(x='Bin', width=2.75, top='Count', bottom=0, line_color='white', line_width=1, color='gray', fill_alpha=0.50, source=h_data)

您也可以通过一个低级的quad调用来完成,但是您需要创建一个包含3列的数据源(左边缘和右边缘,底部仍然可以是常量0)。vbar更显式,您甚至可以改进“Bin”列来表示间隔,而不是右(或左)边。你知道吗

注意有一些关于p.title_text_font_style的拼写错误(我认为或者你可能使用了旧版本的bokeh),应该是p.title.text_font_style,还有一个或两个相同的类型。你知道吗

相关问题 更多 >