简体   繁体   English

蟒蛇散景; 使用GMapPlot上的CustomJS回调更改补丁颜色

[英]python bokeh; change Patch colours with CustomJS callback on GMapPlot

I'm trying to add a button to my bokeh plot that will allow me to change the colour used on a patch glyph that I've added to a GMapPlot using a callback. 我正在尝试在我的散景图中添加一个按钮,这将允许我更改我使用回调添加到GMapPlot的补丁字形上使用的颜色。

Currently what I have is: 目前我所拥有的是:

from bokeh.io import output_file, show
from bokeh.models import GMapPlot, GMapOptions, ColumnDataSource, DataRange1d, Patch

map_options = GMapOptions(lat=-41.281909, lng=174.775993, zoom=13)
p = GMapPlot(x_range=DataRange1d(), y_range=DataRange1d(), map_options=map_options, api_key=API_KEY)
lats = [-41.281909, -41.281044, -41.294968]
longs = [174.775993, 174.761222, 174.764916]
source = ColumnDataSource(data=dict(lats=lats, longs=longs))
patch = Patch(x='longs', y='lats', fill_color='red', fill_alpha=0.2)
p.add_glyph(source, patch)
output_file('gmapplot.html')
show(p)

I'd like to be able to edit that fill_color through a callback using a button. 我希望能够通过回调使用按钮编辑fill_color。 I tried appropriating this response but haven't been able to get it to work. 我试图挪用这个回复但却无法让它发挥作用。

Any help would be greatly appreciated. 任何帮助将不胜感激。

PS. PS。 If you're trying to run this code you will need to use your own google maps API key. 如果您尝试运行此代码,则需要使用自己的Google地图API密钥。 You can get one here . 你可以在这里找到一个。

The other response changes the fill_color of a bunch of circles to refer to a different column of colors in a column data source (so that all the circles can have their own color) by changing the field attribute. 其他响应改变fill_color一堆圆圈通过改变指的颜色的不同中的列的数据源(使所有的圆都可以有自己的颜色)的field属性。 Since you are trying to set just a single color value for the once Patch , you probably want to set value instead of field 由于您尝试仅为一次Patch设置单个颜色值,因此您可能希望设置value而不是field

cb = CustomJS(args=dict(patch=patch), code ="""
    patch.glyph.fill_color.value = 'blue';
""")

I guess it's possible you might need a trigger there but I don't think so. 我想你可能需要一个trigger ,但我不这么认为。

The patch and line glyphs are the two glyphs that are a little quirky in the API, because they don't really support vectorized properties like all the other glyphs do. patchline字形是API中有点古怪的两个字形,因为它们并不像所有其他字形那样真正支持矢量化属性。


UPDATE: Perhaps a complete example is clearer. 更新:也许一个完整的例子更清晰。 Also FYI the trigger is needed, at least as of current versions ( 0.12.4 ). 此外,还需要FYI trigger ,至少是当前版本( 0.12.4 )。

from bokeh.io import output_file, show
from bokeh.layouts import column
from bokeh.models import CustomJS, Select
from bokeh.plotting import figure

plot = figure()

r = plot.patch(x=[1, 2, 3, 4], y=[1, 2, 2, 1],
               fill_color="firebrick", fill_alpha=0.6, line_color="black")


select = Select(title="Select colors", value="firebrick",
                options = ["firebrick","navy", "olive"])

callback = CustomJS(args=dict(renderer=r, select=select), code ="""
    renderer.glyph.fill_color.value = select.value;
    renderer.trigger('change')
""")
select.callback = callback

output_file("foo.html")

show(column(select, plot))

The patch starts out red. 补丁开始是红色的。 Changing the Select widget in the UI causes the color of the patch to update: 更改UI中的“ Select窗口小部件会导致修补程序的颜色更新:

在此输入图像描述

If that's not what you are asking then I have to gently suggest that the question is not clear. 如果这不是你要问的那么我必须温和地暗示问题不明确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM