2017-07-27 91 views
0

我试图用散景创建地图来显示US_cities中的人口,但只要我运行代码,它将返回空地图,框架在那里,但地图不是。我试图做一些像this但是对所有美国城市。散景返回空地图

下面是一个使用我的代码在背景虚化数据“us_cities.json”文件:

import pandas as pd 

from bokeh.io import show 
from bokeh.models import (
    ColumnDataSource, 
    HoverTool, 
    LogColorMapper 
) 
from bokeh.palettes import Viridis6 as palette 
from bokeh.plotting import figure 

palette.reverse() 

new_data = pd.read_json("/home/alvin/.bokeh/data/us_cities.json") 

#Creating random data that I want to show on map 
new_data['pop'] = ((new_data['lat'] * 100) - new_data["lon"])/ 800 

#Converting pd series to array 
xs = new_data['lat'].tolist() 
ys = new_data['lon'].tolist() 
pops = new_data['pop'].tolist() 

#creating ColumnDataSource 
source = ColumnDataSource(data=dict(
    x=xs, 
    y=ys, 
    pop = pops, 
)) 

TOOLS = "pan,wheel_zoom,reset,hover,save" 

p = figure(
    title="Just a US Map", tools=TOOLS, 
    x_axis_location=None, y_axis_location=None 
) 

color_mapper = LogColorMapper(palette=palette) 
p.grid.grid_line_color = None 

p.patches('x', 'y', source=source, 
      fill_color={'field': 'pop', '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 = [ 
    ("population)", "@pop%"), 
    ("(Long, Lat)", "($x, $y)"), 
] 

show(p) 

可能是什么问题就在这里?

我运行python3和背景虚化0.12.6

如果我检查我的数据,它看起来是这样的: enter image description here

回答

0

美国城市数据不包含字形,象个县的数据。您可以显示县的数据,并覆盖城市数据作为顶部的散点图:

import pandas as pd 

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 

palette.reverse() 

counties = { 
    code: county for code, county in counties.items() if county["state"] == "tx" 
} 

county_xs = [county["lons"] for county in counties.values()] 
county_ys = [county["lats"] for county in counties.values()] 


csource = ColumnDataSource(data=dict(
    x=county_xs, 
    y=county_ys, 
)) 


new_data = pd.read_json("/home/tc427/.bokeh/data/us_cities.json")[::100] 

#Creating random data that I want to show on map 
new_data['pop'] = ((new_data['lat'] * 100) - new_data["lon"])/ 800 

#Converting pd series to array 
xs = new_data['lon'].tolist() 
ys = new_data['lat'].tolist() 
pops = new_data['pop'].tolist() 

#creating ColumnDataSource 
source = ColumnDataSource(data=dict(
    x=xs, 
    y=ys, 
    pop = pops, 
)) 

TOOLS = "pan,wheel_zoom,reset,hover,save" 

p = figure(
    title="Just a US Map", tools=TOOLS, 
) 

color_mapper = LogColorMapper(palette=palette, low=0, high=10) 

p.patches('x', 'y', source=csource) 
#p.patches('x', 'y', source=csource, 
#   fill_color={'field': 'pop', 'transform': color_mapper}, 
#   fill_alpha=0.7, color="red", line_width=0.5) 
p.scatter('x', 'y', source=source, color={'field': 'pop', 'transform': color_mapper}) 

hover = p.select_one(HoverTool) 
hover.point_policy = "follow_mouse" 
hover.tooltips = [ 
    ("population)", "@pop%"), 
    ("(Long, Lat)", "($x, $y)"), 
] 

show(p) 
+0

谢谢,这个工程。 –