绘制多个状态散景

时间:2018-01-19 05:46:42

标签: bokeh geo

我试图策划两件事:

  1. MD,VA和DC的县
  2. Lats / Longs用于在该区域内安装房源(最贵的是某种颜色) 另一种颜色最便宜)
  3. 但是,我在第1部分遇到问题,似乎无法在没有收到错误消息的情况下绘制县:

    " Javascript错误添加输出! 错误:渲染Bokeh模型时出错:找不到ID为:xxxxxxxxxxx的标记 有关更多详细信息,请参阅浏览器Javascript控制台。"

    其中xxxxx是数字

    from bokeh.io import show
    from bokeh.models import (
    ColumnDataSource,
    HoverTool,
    LogColorMapper)
    from bokeh.palettes import Viridis6 as palette
    from bokeh.plotting import figure
    
    from bokeh.sampledata.us_counties import data as counties
    from bokeh.sampledata.unemployment import data as unemployment
    
    palette.reverse()
    
    va_counties = {
        code: county for code, county in counties.items() if county["state"] == "va"
    }
    md_counties = {
        code: county for code, county in counties.items() if county["state"] == "md"
    }
    dc_counties = {
        code: county for code, county in counties.items() if county["state"] == "dc"
    }
    
    va_county_xs = [county["lons"] for county in va_counties.values()]
    va_county_ys = [county["lats"] for county in va_counties.values()]
    
    md_county_xs = [county["lons"] for county in md_counties.values()]
    md_county_ys = [county["lats"] for county in md_counties.values()]
    
    dc_county_xs = [county["lons"] for county in dc_counties.values()]
    dc_county_ys = [county["lats"] for county in dc_counties.values()]
    
    va_county_names = [county['name'] for county in va_counties.values()]
    md_county_names = [county['name'] for county in md_counties.values()]
    dc_county_names = [county['name'] for county in dc_counties.values()]
    
    
    #county_rates = [unemployment[county_id] for county_id in counties]
    color_mapper = LogColorMapper(palette=palette)
    
    va_source = ColumnDataSource(data=dict(
        x=va_county_xs,
        y=va_county_ys,
        name=va_county_names,
    ))
    
    md_source = ColumnDataSource(data=dict(
         x=md_county_xs,
    y=md_county_ys,
    name=md_county_names,
    ))
    
    dc_source = ColumnDataSource(data=dict(
        x=dc_county_xs,
        y=dc_county_ys,
        name=dc_county_names,
    ))
    
    TOOLS = "pan,wheel_zoom,reset,hover,save"
    
    va = figure(
        title="Texas Unemployment, 2009", tools=TOOLS,
        x_axis_location=None, y_axis_location=None
    )
    va.grid.grid_line_color = None
    
    md = figure(
        title="Texas Unemployment, 2009", tools=TOOLS,
        x_axis_location=None, y_axis_location=None
    )
    md.grid.grid_line_color = None
    
    dc = figure(
        title="Texas Unemployment, 2009", tools=TOOLS,
        x_axis_location=None, y_axis_location=None
    )
    dc.grid.grid_line_color = None
    
    va.patches('x', 'y', source=va_source,
              fill_color={'field': 'rate', 'transform': color_mapper},
              fill_alpha=0.7, line_color="white", line_width=0.5)
    md.patches('x', 'y', source=md_source,
              fill_color={'field': 'rate', 'transform': color_mapper},
              fill_alpha=0.7, line_color="white", line_width=0.5)
    dc.patches('x', 'y', source=dc_source,
              fill_color={'field': 'rate', 'transform': color_mapper},
              fill_alpha=0.7, line_color="white", line_width=0.5)
    
    hover = p.select_one(HoverTool)
    hover.point_policy = "follow_mouse"
    hover.tooltips = [
        ("Name", "@name"),
        ("(Long, Lat)", "($x, $y)"),
    ]
    
    show(va)
    show(md)
    show(dc)
    

1 个答案:

答案 0 :(得分:0)

Bokeh保留了一份隐含的"当前文件"默认情况下,您所做的一切都会添加到其中。这使得生成一个HTML文件的脚本非常简单,特别是在Jupyter笔记本中使用更加简单和透明。一般来说,这是一个净积极的,但它确实使某些其他使用模式需要稍微冗长。特别是在您进行多次show调用的情况下,每次调用show都会获得相同的文档,这可能不是您想要的。解决方案是按顺序创建和显示每个图,并在其间调用reset_output。此外,您确实需要使用output_file为每个单独的输出指定唯一的文件名。这是一个小例子:

from bokeh.io import output_file, reset_output, show
from bokeh.plotting import figure

# create and show one figure
p1 = figure(title="plot 1")
p1.circle([1,2,3], [4,6,5])

output_file("p1.html")
show(p1)

# clear out the "current document"
reset_output()

# create and show another figure
p2 = figure(title="plot 2")
p2.circle([1,2,3], [8,6,7])

output_file("p2.html")
show(p2)

通过自己明确管理Bokeh Document对象还有其他方法可以做到这一点,但我可能会说这是最简单的入门方法。