gpt4 book ai didi

python - 使用 HoverTool 绘制 Bokeh 线图

转载 作者:太空宇宙 更新时间:2023-11-03 18:13:24 28 4
gpt4 key购买 nike

我想将 Bokeh HoverTool 与线/散点图结合使用。下面是代码(大部分取自 http://docs.bokeh.org/docs/gallery/correlation.html )。在我的示例中,悬停仅显示“acme”行的信息,我无法弄清楚如何使其适用于另一行“choam”。有什么建议/解决方案吗?

from numpy import cumprod, linspace, random
import time
from bokeh.plotting import *
from bokeh.objects import GridPlot, HoverTool

num_points = 20

now = time.time()
dt = 24*3600
dates = linspace(now, now + num_points*dt, num_points)
acme = cumprod(random.lognormal(0.0, 0.04, size=num_points))
choam = cumprod(random.lognormal(0.0, 0.04, size=num_points))

output_file("correlation.html", title="correlation.py example")

source = ColumnDataSource(
data=dict(
acme=acme,
choam=choam,
dates=dates
)
)

figure(x_axis_type = "datetime", tools="hover,pan,wheel_zoom,box_zoom,reset,previewsave")

hold()

line(dates, acme, color='#1F78B4', legend='ACME')
line(dates, choam, color='#FB9A99', legend='CHOAM')

scatter(dates, acme, color='#1F78B4', source = source, fill_color=None, size=8)
scatter(dates, choam, color='#33A02C', fill_color=None, size=8)

curplot().title = "Stock Returns"
grid().grid_line_alpha=0.3


hover = [t for t in curplot().tools if isinstance(t, HoverTool)][0]
hover.tooltips = OrderedDict([
('Price', "@acme"),
('Price', "@choam"),
('Date', "@dates"),
('Date', "@dates"),
])

show()

最佳答案

在 0.8 中,我对多个绘图使用了类似的东西:

source1 = ColumnDataSource(
data=dict(
acme=acme,
dates=dates
)
)

source2 = ColumnDataSource(
data=dict(
choam=choam,
dates=dates
)
)

scatter(dates, acme, color='#1F78B4', source = source1, fill_color=None, size=8)
scatter(dates, choam, color='#33A02C', source = source2, fill_color=None, size=8)

不保证它会继续工作 - 仍在等待行工具提示:)

关于python - 使用 HoverTool 绘制 Bokeh 线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25450104/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com