为什么我不能在python的散景图上显示点?

时间:2017-07-01 16:20:18

标签: python dictionary geospatial bokeh

我正在尝试在Bokeh的地图上绘制一些数据点,但不知何故没有显示,只有地图背景。

import pandas as pd
from IPython.core.display import HTML, display
%matplotlib inline
sample = pd.DataFrame({'Lat': [40.7260,40.7209], 'Lon': [-73.991,-74.0507], 'Count': 1})
from bokeh.plotting import figure, output_notebook, show
output_notebook()
from bokeh.tile_providers import STAMEN_TERRAIN

x_range, y_range = ((-8242000,-8210000), (4965000,4990000))
plot_width  = int(750)
plot_height = int(plot_width//1.2)

def base_plot(tools='pan,wheel_zoom,reset',plot_width=plot_width, plot_height=plot_height, **plot_args):
p = figure(tools=tools, plot_width=plot_width, plot_height=plot_height,
    x_range=x_range, y_range=y_range, outline_line_color=None,
    min_border=0, min_border_left=0, min_border_right=0,
    min_border_top=0, min_border_bottom=0, **plot_args)
p.axis.visible = False
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None
return p    
p = base_plot()
p.add_tile(STAMEN_TERRAIN)
p.circle(x=samples['Lat'], y=samples['Lon'], **options)
show(p)

感谢您的建议。

1 个答案:

答案 0 :(得分:1)

绘图范围采用Web Mercator单位:

((-8242000,-8210000), (4965000,4990000))

sample DataFrame中的数据点以纬度/经度为单位。你可以:

  • 添加"额外范围"在lat / lon单位(匹配!)并p.circle引用额外范围而不是默认范围。

  • 将您的圆圈坐标转换为Web Mercator

后者可能更容易。 This page有一个可以进行转换的功能。使用它,你得到

sample = pd.DataFrame({
    'easting': [-8236640.443285105, -8243286.216885463], 
    'northing': [4972010.345629457, 4971261.231184175]
})

更新您的代码以使用它:

import pandas as pd

from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.tile_providers import STAMEN_TERRAIN

samples = pd.DataFrame({
    'easting':  [-8236640.443285105, -8243286.216885463],
    'northing': [4972010.345629457, 4971261.231184175]
})

x_range, y_range = ((-8242000,-8210000), (4965000,4990000))
plot_width  = int(750)
plot_height = int(plot_width//1.2)

def base_plot(tools='pan,wheel_zoom,reset',plot_width=plot_width, plot_height=plot_height, **plot_args):
    p = figure(tools=tools, plot_width=plot_width, plot_height=plot_height,
        x_range=x_range, y_range=y_range, outline_line_color=None,
        min_border=0, min_border_left=0, min_border_right=0,
        min_border_top=0, min_border_bottom=0, **plot_args)
    p.axis.visible = False
    p.xgrid.grid_line_color = None
    p.ygrid.grid_line_color = None
    return p
p = base_plot()

p.add_tile(STAMEN_TERRAIN)

p.circle(x=samples['easting'], y=samples['northing'], size=20, color="red")

output_file("map.html")
show(p)

得出这个情节:

enter image description here