如何使bokeh图上的颜色与其正确值(对数刻度)对齐?

2024-09-30 18:35:01 发布

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

我的问题与本次讨论类似-https://github.com/bokeh/bokeh/issues/5020

我正试图用对数比例的着色方案在bokeh图上对点进行主题化,并有一个与之配套的颜色条。我的“颜色值”(即,我的主题范围从1e-9到1e-3)

我现在的问题是,我可以使用LogColorMapper和LogTicker使colorbar适合定义的颜色范围,但是在我的点的fill_color属性上使用相同的LogColorMapper似乎映射不正确。请参见下面的示例:

import numpy as np
from bokeh.io import show
from bokeh.models import ColorBar, LogTicker,Ticker,HoverTool
from bokeh.models.sources import ColumnDataSource
from bokeh.models.mappers import LinearColorMapper, LogColorMapper
from bokeh.palettes import Viridis6, Viridis3,Spectral11
from bokeh.plotting import figure

x = np.linspace(0, 1000, num=1000)
y = [np.random.random()*1000 for x in range(0,1000)]
#generate a random lognormal list, median 1e-6 with standard deviation of 1 order of magnitude
z = 10**np.random.normal(-6, 1, size=1000)

source = ColumnDataSource(dict(x=x, y=y, z=z))

log_mapper = LogColorMapper(palette=Viridis6, low=1e-9, high=1e-3)

custom_hover = HoverTool()
custom_hover.tooltips=[('Value','@z'),]

p = figure(x_axis_type='linear', toolbar_location='above',tools=[custom_hover])
opts = dict(x='x', line_color=None, source=source)
p.circle(y='y', fill_color={'field': 'z', 'transform': log_mapper}, legend="Log mapper", **opts)

colorbar = ColorBar(color_mapper=log_mapper,ticker=LogTicker(), location=(0,0), orientation='horizontal', padding=0)

p.add_layout(colorbar, 'below')

show(p)

当这个情节发生时,我得到了这样的东西 screencap

使用鼠标悬停工具将鼠标悬停在点上(抱歉,我不知道如何嵌入/链接bokeh绘图)。。。您将看到绘图上的颜色与点值不匹配。我错过了什么


Tags: fromimportlogsource颜色modelscustomnp
3条回答

答案是这样的

我发现了问题并设法解决了问题。请参阅此线程:

它本质上与logcolorbar之间的日志转换不同于fill_color的日志转换有关,这会导致0和1(我的域)之间的值出现问题。请参阅我的帖子:github.com/bokeh/bokeh/pull/8832

显然,这将在下一个版本中解决,但在此期间,我的变通方法也可以解决

User1170556指出,通过执行 logcolorbar之间的对数变换不同于填充颜色的对数变换, cfthe PR

(在用户1170556对自己的问题给出答案后,我将删除此答案。)

这是Bokeh1.4中的一个问题,但已在master上解决,并将在即将发布的2.0版本中解决。原始代码在2.0“dev”版本中正常运行

相关问题 更多 >