我正在尝试使用geopandas读取存储在CSV文件中的地理数据,并创建用于大学目的的欧洲地图。尽管显然需要使用geojson文件,但我从geopandas DB中提取了几何值并将其添加到df中。 尽管我没有设法链接它,但我实际上花了一天时间浏览一些教程和示例。 如果有人可以提供帮助,将不胜感激。 目的是添加一个带有绿色的字形,以根据平均值栏显示每个国家在欧洲的表现。
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
europe = (world.loc[world['continent'] == 'Europe'])
europe.head()
geo_source = GeoJSONDataSource(geojson=europe.to_json())
palette = ['#b9ef96', '#9ae968', '#7be23a', '#6cdf23', '#64dd17']
color_mapper = LogColorMapper(palette=palette)
p = figure(plot_height=600, title='Europe', x_range=(-30,60), y_range=
(30,85))
p.patches('xs', 'ys', fill_alpha=0.7,
fill_color='green', line_color='black', line_width=0.5,
source=geo_source)
show(p)
df_map1 = pd.read_csv('countries_geom.csv', delimiter='\t', index_col=0)
df_map1
df_source = ColumnDataSource(df_map1)
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
europe = (world.loc[world['continent'] == 'Europe'])
europe.head()
geo_source = GeoJSONDataSource(geojson=europe.to_json())
palette = ['#b9ef96', '#9ae968', '#7be23a', '#6cdf23', '#64dd17']
color_mapper = LogColorMapper(palette=palette)
p = figure(plot_height=600, title='Europe', x_range=(-30,60), y_range=
(30,85))
p.patches('xs', 'ys', fill_alpha=0.7,
fill_color='green', line_color='black', line_width=0.5,
source=geo_source)
show(p)
df_map1 = pd.read_csv('countries_geom.csv', delimiter='\t', index_col=0)
df_map1
df_source = ColumnDataSource(df_map1)[![df_map_image][1]][1]
答案 0 :(得分:1)
如果您只想绘制欧洲地图,那么这就是代码(适用于Bokeh v1.1.0):
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show
from shapely.geometry import Polygon
import geopandas as gp
world = gp.read_file(gp.datasets.get_path('naturalearth_lowres'))
europe = (world.loc[world['continent'] == 'Europe'])
names = [country for country in europe.name]
countries = []
[countries.append(country) if type(item) == Polygon else [countries.append(country) for i in list(item)] for item, country in zip(europe.geometry, names)]
polygons = []
[polygons.append(item) if type(item) == Polygon else [polygons.append(i) for i in list(item)] for item in europe.geometry]
xs, ys = [], []
xs = [list(polygon.boundary.coords.xy[0]) for polygon in polygons]
ys = [list(polygon.boundary.coords.xy[1]) for polygon in polygons]
source = ColumnDataSource(dict(xs = xs, ys = ys, countries = countries))
p = figure(title = 'Europe', tools = 'pan, wheel_zoom, box_zoom, reset, hover, save', tooltips = [('Countries', '@countries')],
x_range = (-30, 60), y_range = (30, 85), x_axis_location = None, y_axis_location = None)
p.patches('xs', 'ys', fill_alpha = 0.7, fill_color = 'green', line_color = 'black', line_width = 0.5, source = source)
show(p)