用bokeh在地图上绘图。

时间:2022-01-07 00:35:55

So i'm trying to use bokeh to plot a set of geographical points (school locations to be exact). I can plot them using the lats and longs, but how can I overlay these points over an actual map? Is there a way to integrate bokeh with google maps or something?

所以我试着用bokeh来画一组地理位置(精确的学校位置)。我可以用lats和longs来绘制它们,但是如何在实际地图上覆盖这些点呢?有什么方法可以把bokeh和谷歌映射集成在一起吗?

Thanks

谢谢

3 个解决方案

#1


9  

I recognize that this thread is old, but I haven't found a super clear answer anywhere else, so hopefully this will help.

我认识到这个线程是旧的,但是我还没有找到一个非常清晰的答案,所以希望这能有所帮助。

from bokeh.sampledata import us_states
from bokeh.plotting import *

us_states = us_states.data.copy()

del us_states["HI"]
del us_states["AK"]

# separate latitude and longitude points for the borders
#   of the states.
state_xs = [us_states[code]["lons"] for code in us_states]
state_ys = [us_states[code]["lats"] for code in us_states]

# init figure
p = figure(title="Plotting Points Example: The 5 Largest Cities in Texas", 
           toolbar_location="left", plot_width=1100, plot_height=700)

# Draw state lines
p.patches(state_xs, state_ys, fill_alpha=0.0,
    line_color="#884444", line_width=1.5)

#  Latitude and Longitude of 5 Cities
# ------------------------------------
# Austin, TX -------30.26° N, 97.74° W
# Dallas, TX -------32.77° N, 96.79° W
# Fort Worth, TX ---32.75° N, 97.33° W
# Houston, TX ------29.76° N, 95.36° W
# San Antonio, TX --29.42° N, 98.49° W

# Now group these values together into a lists of x (longitude) and y (latitude)
x = [-97.7431, -96.79, -97.33, -95.36, -98.49]
y = [30.26, 32.77, 32.75, 29.76, 29.42] 

# The scatter markers
p.circle(x, y, size=8, color='navy', alpha=1)

# output to static HTML file
output_file("texas.html")

# show results
show(p)

Plotting points like these are called "Scatter Markers" in Bokeh. For more information, see http://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html#scatter-markers

像这样的点在Bokeh中被称为“散点标记”。有关更多信息,请参见http://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html#scatter-marker

#2


3  

There is an example of integrating Bokeh with Google maps here:

这里有一个将Bokeh与谷歌映射积分的例子:

http://bokeh.pydata.org/en/latest/docs/user_guide/geo.html#google-maps-support

http://bokeh.pydata.org/en/latest/docs/user_guide/geo.html google-maps-support

Right now (as of 0.7) you have to use the lower level interface but will be adding GMap options to the higher level APIs soon.

现在(从0.7开始),您必须使用较低级别的接口,但很快将向更高级别的api添加GMap选项。

#3


2  

Here's a link you might want to check:

这里有一个链接你可能想要检查:

http://bokeh.pydata.org/en/latest/docs/gallery/choropleth.html

http://bokeh.pydata.org/en/latest/docs/gallery/choropleth.html

from bokeh.sampledata import us_states, us_counties, unemployment
from bokeh.plotting import *

us_states = us_states.data.copy()
us_counties = us_counties.data.copy()
unemployment = unemployment.data

del us_states["HI"]
del us_states["AK"]

state_xs = [us_states[code]["lons"] for code in us_states]
state_ys = [us_states[code]["lats"] for code in us_states]

county_xs=[us_counties[code]["lons"] for code in us_counties if us_counties[code]["state"] not in ["ak", "hi", "pr", "gu", "vi", "mp", "as"]]
county_ys=[us_counties[code]["lats"] for code in us_counties if us_counties[code]["state"] not in ["ak", "hi", "pr", "gu", "vi", "mp", "as"]]

colors = ["#F1EEF6", "#D4B9DA", "#C994C7", "#DF65B0", "#DD1C77", "#980043"]

county_colors = []
for county_id in us_counties:
    if us_counties[county_id]["state"] in ["ak", "hi", "pr", "gu", "vi", "mp", "as"]:
        continue
    try:
        rate = unemployment[county_id]
        idx = min(int(rate/2), 5)
        county_colors.append(colors[idx])
    except KeyError:
        county_colors.append("black")

output_file("choropleth.html", title="choropleth.py example")

p = figure(title="US Unemployment 2009", toolbar_location="left",
    plot_width=1100, plot_height=700)

p.patches(county_xs, county_ys, fill_color=county_colors, fill_alpha=0.7,
    line_color="white", line_width=0.5)
p.patches(state_xs, state_ys, fill_alpha=0.0,
    line_color="#884444", line_width=2)

show(p)

I haven't tried it yet but I guess you can use shape files to have more accurate maps if you want to.

我还没有尝试过,但是我想如果你想要的话,你可以使用形状文件来获得更精确的地图。

What I've tried before is the basemap module..

我之前试过的是basemap模块。

#1


9  

I recognize that this thread is old, but I haven't found a super clear answer anywhere else, so hopefully this will help.

我认识到这个线程是旧的,但是我还没有找到一个非常清晰的答案,所以希望这能有所帮助。

from bokeh.sampledata import us_states
from bokeh.plotting import *

us_states = us_states.data.copy()

del us_states["HI"]
del us_states["AK"]

# separate latitude and longitude points for the borders
#   of the states.
state_xs = [us_states[code]["lons"] for code in us_states]
state_ys = [us_states[code]["lats"] for code in us_states]

# init figure
p = figure(title="Plotting Points Example: The 5 Largest Cities in Texas", 
           toolbar_location="left", plot_width=1100, plot_height=700)

# Draw state lines
p.patches(state_xs, state_ys, fill_alpha=0.0,
    line_color="#884444", line_width=1.5)

#  Latitude and Longitude of 5 Cities
# ------------------------------------
# Austin, TX -------30.26° N, 97.74° W
# Dallas, TX -------32.77° N, 96.79° W
# Fort Worth, TX ---32.75° N, 97.33° W
# Houston, TX ------29.76° N, 95.36° W
# San Antonio, TX --29.42° N, 98.49° W

# Now group these values together into a lists of x (longitude) and y (latitude)
x = [-97.7431, -96.79, -97.33, -95.36, -98.49]
y = [30.26, 32.77, 32.75, 29.76, 29.42] 

# The scatter markers
p.circle(x, y, size=8, color='navy', alpha=1)

# output to static HTML file
output_file("texas.html")

# show results
show(p)

Plotting points like these are called "Scatter Markers" in Bokeh. For more information, see http://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html#scatter-markers

像这样的点在Bokeh中被称为“散点标记”。有关更多信息,请参见http://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html#scatter-marker

#2


3  

There is an example of integrating Bokeh with Google maps here:

这里有一个将Bokeh与谷歌映射积分的例子:

http://bokeh.pydata.org/en/latest/docs/user_guide/geo.html#google-maps-support

http://bokeh.pydata.org/en/latest/docs/user_guide/geo.html google-maps-support

Right now (as of 0.7) you have to use the lower level interface but will be adding GMap options to the higher level APIs soon.

现在(从0.7开始),您必须使用较低级别的接口,但很快将向更高级别的api添加GMap选项。

#3


2  

Here's a link you might want to check:

这里有一个链接你可能想要检查:

http://bokeh.pydata.org/en/latest/docs/gallery/choropleth.html

http://bokeh.pydata.org/en/latest/docs/gallery/choropleth.html

from bokeh.sampledata import us_states, us_counties, unemployment
from bokeh.plotting import *

us_states = us_states.data.copy()
us_counties = us_counties.data.copy()
unemployment = unemployment.data

del us_states["HI"]
del us_states["AK"]

state_xs = [us_states[code]["lons"] for code in us_states]
state_ys = [us_states[code]["lats"] for code in us_states]

county_xs=[us_counties[code]["lons"] for code in us_counties if us_counties[code]["state"] not in ["ak", "hi", "pr", "gu", "vi", "mp", "as"]]
county_ys=[us_counties[code]["lats"] for code in us_counties if us_counties[code]["state"] not in ["ak", "hi", "pr", "gu", "vi", "mp", "as"]]

colors = ["#F1EEF6", "#D4B9DA", "#C994C7", "#DF65B0", "#DD1C77", "#980043"]

county_colors = []
for county_id in us_counties:
    if us_counties[county_id]["state"] in ["ak", "hi", "pr", "gu", "vi", "mp", "as"]:
        continue
    try:
        rate = unemployment[county_id]
        idx = min(int(rate/2), 5)
        county_colors.append(colors[idx])
    except KeyError:
        county_colors.append("black")

output_file("choropleth.html", title="choropleth.py example")

p = figure(title="US Unemployment 2009", toolbar_location="left",
    plot_width=1100, plot_height=700)

p.patches(county_xs, county_ys, fill_color=county_colors, fill_alpha=0.7,
    line_color="white", line_width=0.5)
p.patches(state_xs, state_ys, fill_alpha=0.0,
    line_color="#884444", line_width=2)

show(p)

I haven't tried it yet but I guess you can use shape files to have more accurate maps if you want to.

我还没有尝试过,但是我想如果你想要的话,你可以使用形状文件来获得更精确的地图。

What I've tried before is the basemap module..

我之前试过的是basemap模块。