def bdapi(): friends = bot.friends(update=True) dd1 = defaultdict(int) # 如果调用字典的key不存在的时候,默认value为0 for hy in friends: # print(hy.city) if '\u9fa5' >= hy.city >= '\u4e00': # 如果省份是汉字就保留,否则就舍弃,因为在中国地图上也不会显示 city = hy.city + "市" # 必须要加,不加会名字识别不到 dd1[city] += 1 # 每次进来如果key相同则进行累加 sj = [list(a) for a in zip(dd1.keys(), dd1.values())] # 地图部分 BD_AK = "xC9wlIKWG31GZwZ87jFBGO6RycDZf7Ue" MR_ZX = [108.9550557300, 34.3247934100] # 定义地图的默认中心位置 m3 = BMap() # 进去的地图中心点 可以在网址查询 http://www.gpsspg.com/maps.htm 但是这里有一点需要注意,地图给出来的维度和经度,这里的顺序是经纬度,需要调整下 m3.add_schema( baidu_ak=BD_AK, center=MR_ZX, zoom=8 # 缩放程度 ) m3.add( series_name="", data_pair=sj, label_opts=opts.LabelOpts("{b}"), ) m3.set_global_opts( title_opts=opts.TitleOpts("百度API展示微信好友分布"), visualmap_opts=opts.VisualMapOpts( is_piecewise=True, max_=180, pos_bottom=40), # 最后一个是设置提示距离底端的距离,防止百度的log将数据挡住 ) m3.render("./kshwy/map_bdapi.html") webbrowser.open("E:\study\kshwy\map_bdapi.html", autoraise=True)
def test_bmap_map_control_panel(fake_writer): bmap = (BMap().add_schema( baidu_ak=FAKE_API_KEY, center=[ -0.118092, 51.509865 ]).add_coordinate("London", -0.118092, 51.509865).add( "bmap", [list(z) for z in zip(TEST_LOCATION, TEST_VALUE)], type_=ChartType.LINES, label_opts=opts.LabelOpts(formatter="{b}"), ).add_control_panel( copyright_control_opts=opts.BMapCopyrightTypeOpts(position=3), maptype_control_opts=opts.BMapTypeControlOpts( type_=BMapType.MAPTYPE_CONTROL_DROPDOWN), scale_control_opts=opts.BMapScaleControlOpts(), overview_map_opts=opts.BMapOverviewMapControlOpts(is_open=True), navigation_control_opts=opts.BMapNavigationControlOpts(), geo_location_control_opts=opts.BMapGeoLocationControlOpts(), )) bmap.render() content = fake_writer.call_args[0][1] assert_in("new BMap.CopyrightControl", content) assert_in("new BMap.MapTypeControl", content) assert_in("new BMap.ScaleControl", content) assert_in("new BMap.OverviewMapControl", content) assert_in("new BMap.NavigationControl", content) assert_in("new BMap.GeolocationControl", content)
def map_base(self) -> BMap: start = datetime.datetime.now() location = [] c = ( BMap() .add_schema(baidu_ak=BAIDU_MAP_AK, center=[114.34632, 22.69084], zoom=13) .add( "bmap", location, type_='line', label_opts=opts.LabelOpts(formatter="{b}"), ) .set_global_opts( visualmap_opts=opts.VisualMapOpts(), ) ) for community in self.community_list: if community.number: c.add_coordinate(community.name, community.long, community.lat) c.add( "heat", [(community.name, community.number)], label_opts=opts.LabelOpts(formatter="{d}"), color="red", ) end = datetime.datetime.now() print("Map: " + str(end - start)) return c.dump_options_with_quotes()
def bmap_route_map() -> BMap: c = ( BMap().add_schema(baidu_ak=Baidu_AK, center=[110.3014600000, 20.0132350000], zoom=13) #缩放比例12-14之间可行 .set_global_opts( title_opts=opts.TitleOpts( title="09/20 17:30-17:31"), # 更改title==================== visualmap_opts=opts.VisualMapOpts(is_show=False), )) # 增加路径线 c.add( "行车路径模拟", type_="lines", is_polyline=True, data_pair=taix_lines, linestyle_opts=opts.LineStyleOpts(opacity=0.2, width=0.5), # 如果不是最新版本的话可以注释下面的参数(效果差距不大) # progressive=200, # progressive_threshold=500 ) c.add_control_panel( navigation_control_opts=opts.BMapNavigationControlOpts( offset_height=30), scale_control_opts=opts.BMapScaleControlOpts(), ) return c
def scattermap(data): jscode = """ function (val) { return val[2]/15; } """ bmap = (BMap().add_schema(baidu_ak=BAIDU_AK, center=[104.114129, 37.550339], zoom=5, is_roam=False, map_style={ 'styleJson': MAPTYPE }).add('城市分布', [ list(x) for x in zip( data['content']['name'], data['content']['item'], ) ], type_='scatter', symbol_size=JsCode(jscode) ) .add('top5', [list(x) for x in zip(data['content']['name'][:5],data['content']['item'][:5])], type_='effectScatter', symbol_size=JsCode(jscode) ).set_global_opts(title_opts=opts.TitleOpts(title='城市分布')).dump_options_with_quotes() ) return bmap
def test_bmap_ignore_nonexistent_coord_exception(): (BMap(is_ignore_nonexistent_coord=True).add_schema( baidu_ak=FAKE_API_KEY, center=[-0.118092, 51.509865]).add( "bmap", [["NonexistentLocation", 123]], label_opts=opts.LabelOpts(formatter="{b}"), ))
def bmap_heatmap(index_lngLat, index_value, title='', width="1000px", height="600px") -> BMap: c = ( BMap( init_opts=opts.InitOpts(theme="white", width=width, height=height)) # .add_schema(baidu_ak=Baidu_AK, center=[110.3131940000, 20.0274250000], zoom=13, map_style=get_map_style()) #缩放比例12-14之间可行 .add_schema(baidu_ak=Baidu_AK, center=[110.3131940000, 20.0274250000], zoom=13).set_global_opts( title_opts=opts.TitleOpts(title=title), visualmap_opts=opts.VisualMapOpts(pos_top='5%', pos_right='5%'))) # 增加坐标点信息 for key, value in index_lngLat.items(): c.add_coordinate(key, value[0], value[1]) # 热力图 c.add( "出发地热力图", index_value[0], type_="heatmap", label_opts=opts.LabelOpts(formatter="{b}"), ) # 点图 c.add( "出发地位置", index_value[0], type_="scatter", symbol_size=3, # color='rgb(1, 1, 1)', # itemstyle_opts= opts.ItemStyleOpts(color='rgb(1, 1, 1)'), label_opts=opts.LabelOpts(is_show=False), ) # 热力图 c.add( "到达地热力图", index_value[1], type_="heatmap", label_opts=opts.LabelOpts(formatter="{b}"), ) # 点图 c.add( "到达地位置", index_value[1], type_="scatter", symbol_size=3, label_opts=opts.LabelOpts(is_show=False), ) c.add_control_panel( navigation_control_opts=opts.BMapNavigationControlOpts( offset_height=30), scale_control_opts=opts.BMapScaleControlOpts(), ) return c
def bmap_base() -> BMap: c = (BMap().add_schema( baidu_ak=BAIDU_MAP_AK, center=[120.13066322374, 30.240018034923]).add( "bmap", [list(z) for z in zip(Faker.provinces, Faker.values())], label_opts=opts.LabelOpts(formatter="{b}"), ).set_global_opts(title_opts=opts.TitleOpts(title="BMap-基本示例"))) return c
def test_bmap_catch_nonexistent_coord_exception(): try: (BMap().add_schema(baidu_ak=FAKE_API_KEY, center=[-0.118092, 51.509865]).add( "bmap", [["NonexistentLocation", 123]], label_opts=opts.LabelOpts(formatter="{b}"), )) except NonexistentCoordinatesException as err: assert_equal(type(err), NonexistentCoordinatesException)
def bmap_heatmap() -> BMap: c = ( BMap(init_opts = opts.InitOpts(theme = "white")) .add_schema(baidu_ak=Baidu_AK, center=[110.3014600000, 20.0132350000], zoom=13) #缩放比例12-14之间可行 .set_global_opts( title_opts=opts.TitleOpts(title="10/04 中秋节 18:00-22:00"), # 更改title==================== visualmap_opts=opts.VisualMapOpts(pos_top='5%', pos_right='5%'), ) ) # 增加坐标点信息 for key, value in index_lngLat.items(): c.add_coordinate(key, value[0], value[1]) # 热力图 c.add( "出发地热力图", index_value[0], type_="heatmap", label_opts=opts.LabelOpts(formatter="{b}"), ) # 点图 c.add( "出发地位置", index_value[0], type_="scatter", symbol_size=3, # color='rgb(1, 1, 1)', # itemstyle_opts= opts.ItemStyleOpts(color='rgb(1, 1, 1)'), label_opts=opts.LabelOpts(is_show=False), ) # 热力图 c.add( "到达地热力图", index_value[1], type_="heatmap", label_opts=opts.LabelOpts(formatter="{b}"), ) # 点图 c.add( "到达地位置", index_value[1], type_="scatter", symbol_size=3, label_opts=opts.LabelOpts(is_show=False), ) c.add_control_panel(navigation_control_opts=opts.BMapNavigationControlOpts(offset_height=30), scale_control_opts=opts.BMapScaleControlOpts(), ) return c
def test_bmap(fake_writer): bmap = (BMap().add_schema( baidu_ak=FAKE_API_KEY, center=[-0.118092, 51.509865]).add_coordinate("London", -0.118092, 51.509865).add( "bmap", [list(z) for z in zip(TEST_LOCATION, TEST_VALUE)], label_opts=opts.LabelOpts(formatter="{b}"), )) bmap.render() content = fake_writer.call_args[0][1] assert_in(f'src="{BAIDU_MAP_API_PREFIX}&ak={FAKE_API_KEY}"', content) assert_in('"coordinateSystem": "bmap"', content, "non bmap found")
def test_bmap_heatmap(): bmap = (BMap().add_schema( baidu_ak=FAKE_API_KEY, center=[-0.118092, 51.509865]).add_coordinate("London", -0.118092, 51.509865).add( "bmap", [list(z) for z in zip(TEST_LOCATION, TEST_VALUE)], label_opts=opts.LabelOpts(formatter="{b}"), )) data = bmap.options.get("series")[0]["data"] for item in data: assert_in("name", item) assert_in("value", item)
def test_bmap_effect_trail_length(fake_writer): bmap = (BMap().add_schema( baidu_ak=FAKE_API_KEY, center=[-0.118092, 51.509865]).add_coordinate("London", -0.118092, 51.509865).add( "bmap", [list(z) for z in zip(TEST_LOCATION, TEST_VALUE)], type_=ChartType.LINES, effect_opts=opts.EffectOpts(trail_length=0.5), label_opts=opts.LabelOpts(formatter="{b}"), )) bmap.render("render.html") content = fake_writer.call_args[0][1] assert_in('"trailLength": 0.5', content, "trainLength parameter is error")
def test_bmap_progressive_options(fake_writer): bmap = (BMap().add_schema( baidu_ak=FAKE_API_KEY, center=[-0.118092, 51.509865]).add_coordinate( "London", -0.118092, 51.509865).add( "bmap", type_="lines", data_pair=[list(z) for z in zip(TEST_LOCATION, TEST_VALUE)], label_opts=opts.LabelOpts(formatter="{b}"), progressive=200, progressive_threshold=500, )) bmap.render() content = fake_writer.call_args[0][1] assert_in("progressive", content) assert_in("progressiveThreshold", content)
def test_bmap_polyline_and_large(fake_writer): bmap = (BMap().add_schema( baidu_ak=FAKE_API_KEY, center=[-0.118092, 51.509865]).add_coordinate("London", -0.118092, 51.509865).add( "bmap", [list(z) for z in zip(TEST_LOCATION, TEST_VALUE)], is_polyline=True, is_large=True, type_=ChartType.LINES, label_opts=opts.LabelOpts(formatter="{b}"), )) bmap.render() content = fake_writer.call_args[0][1] assert_in('"polyline": true', content, "polyline parameter is error") assert_in('"large": true', content, "large parameter is error")
def test_bmap_heatmap(): provinces = ["London"] values = [1] bmap = (BMap().add_schema( baidu_ak=FAKE_API_KEY, center=[-0.118092, 51.509865]).add_coordinate("London", -0.118092, 51.509865).add( "bmap", [list(z) for z in zip(provinces, values)], label_opts=opts.LabelOpts(formatter="{b}"), )) bmap.render() data = bmap.options.get("series")[0]["data"] for item in data: assert_in("name", item) assert_in("value", item)
def bmap_linemap() -> BMap: c = ( BMap(init_opts=opts.InitOpts( theme="white", width="1000px", height="600px")).add_schema( baidu_ak=Baidu_AK, center=[110.3131940000, 20.0274250000], zoom=13) #缩放比例12-14之间可行 .set_global_opts( title_opts=opts.TitleOpts( title="10/01 国庆节 机场出发"), # 更改title==================== visualmap_opts=opts.VisualMapOpts(), )) # 增加坐标点 for key, value in index_lngLat.items(): c.add_coordinate(key, value[0], value[1]) # 增加坐标点的值 c.add( "", index_value, type_=ChartType.EFFECT_SCATTER, symbol_size=4, label_opts=opts.LabelOpts(is_show=False), color="white", ) # 增加连线图 c.add( "行程起始-终止位置", route_pair, type_=ChartType.LINES, is_large=True, large_threshold=100, effect_opts=opts.EffectOpts(symbol=SymbolType.ARROW, symbol_size=5, color="blue"), linestyle_opts=opts.LineStyleOpts(curve=0.1, opacity=0.7), ) # c.set_series_opts(label_opts=opts.LabelOpts(is_show=False)) c.add_control_panel( navigation_control_opts=opts.BMapNavigationControlOpts( offset_height=30), scale_control_opts=opts.BMapScaleControlOpts(), ) return c
def bmap_base(BAIDU_AK, geofile=geofile) -> BMap: with open(geofile, 'r', encoding='utf-8') as f: j = json.loads(f.read()) location = [[k, 1] for k in j.keys()] c = ( BMap().add_schema( baidu_ak=BAIDU_AK, center=[121.497739, 31.242029], zoom=11, map_style={ "styleJson": [ { "featureType": "manmade", "elementType": "all", "stylers": { "visibility": "off" }, }, { "featureType": "highway", "elementType": "labels", "stylers": { "visibility": "off" }, }, # { # "featureType": "label", # "elementType": "all", # "stylers": {"visibility": "off"}, # }, { "featureType": "building", "elementType": "all", "stylers": { "visibility": "off" }, }, ] }).add_coordinate_json(json_file=geofile).add( '', location, # type_='heatmap', symbol_size=9, label_opts=LabelOpts(is_show=False)).set_global_opts( title_opts=TitleOpts())) return c
def get_baidu_map_line(new_location_line) -> BMap: baidu_map_key = config.get('baidu_ak') c = ( BMap().add_schema(baidu_ak=baidu_map_key, center=[120.13066322374, 30.240018034923]). add( "bmap", new_location_line, # [list(z) for z in zip(Faker.provinces, Faker.values())], # label_opts=opts.LabelOpts(formatter="{b}"), type_=GeoType.LINES, effect_opts=opts.EffectOpts(symbol=SymbolType.ARROW, symbol_size=6, color="purple"), linestyle_opts=opts.LineStyleOpts(curve=0.2), ).set_global_opts(title_opts=opts.TitleOpts(title="BMap-网络"))) # return c.render_embed() return c.dump_options_with_quotes()
def bmap_heatmap() -> BMap: c = ( BMap(init_opts=opts.InitOpts( theme="white", width="1000px", height="600px")).add_schema( baidu_ak=Baidu_AK, center=[110.3131940000, 20.0274250000], zoom=13) #缩放比例12-14之间可行 .set_global_opts( title_opts=opts.TitleOpts( title="POI分布"), # 更改title==================== visualmap_opts=opts.VisualMapOpts(pos_top='5%', pos_right='5%'))) # 增加坐标点信息 for key, value in index_lngLat.items(): c.add_coordinate(key, value[0], value[1]) # # 不同类数据的点图 # for label, data in index_value.items(): # c.add( # label, # data, # type_="scatter", # symbol_size=3, # # color='rgb(1, 1, 1)', # # itemstyle_opts= opts.ItemStyleOpts(color='rgb(1, 1, 1)'), # label_opts=opts.LabelOpts(is_show=False), # ) # 不同类数据的热力图 for label, data in index_value.items(): c.add( label, data, type_="heatmap", label_opts=opts.LabelOpts(formatter="{b}"), ) c.add_control_panel( navigation_control_opts=opts.BMapNavigationControlOpts( offset_height=30), scale_control_opts=opts.BMapScaleControlOpts(), ) return c
def bmap_heatmap() -> BMap: c = ( BMap() .add_schema(baidu_ak=Baidu_AK, center=[110.3014600000, 20.0132350000], zoom=13) #缩放比例12-14之间可行 .set_global_opts( title_opts=opts.TitleOpts(title="BMap-热力图"), visualmap_opts=opts.VisualMapOpts(), ) ) # 增加坐标点 c.add_coordinate("海南海口秀英秀英向荣", 110.263727, 20.001732) c.add_coordinate("海南海口龙华海垦海秀", 110.328492, 20.031007) # 增加坐标点之间的值 c.add( "bmap", [['海南海口秀英秀英向荣', 100], ['海南海口龙华海垦海秀', 50]], type_="heatmap", label_opts=opts.LabelOpts(formatter="{b}"), ) return c
def get_gridbmap(message) -> BMap: BAIDU_AK = "HOTBRAfU1jGcQKHBX15ucKsfZO722eyN" pos = (117.20, 39.12) #jscode = "console.log('hi hi');"#可嵌入jscode #jscode = "functions(param){bmap.addEventListener('click', function(e){alert(e.point.lng + ','+ e.point.lat);});}" #每两个点画一条线 j = [[{ "coord": [117.20, 39.12] }, { "coord": [118.21, 41.12] }], [{ "coord": [119.21, 41.12] }, { "coord": [115.21, 41.12] }]] c = ( BMap().add_schema( baidu_ak=BAIDU_AK, center=[117.20, 39.12], zoom=12).add( "", type_="lines", data_pair=j, is_polyline=True, is_large=False, linestyle_opts=opts.LineStyleOpts(color="purple", opacity=0.6, width=1), ).set_series_opts(effect_opts=opts.EffectOpts(is_show=False), label_opts=opts.LabelOpts(is_show=False)). add_control_panel( scale_control_opts=opts.BMapScaleControlOpts(), navigation_control_opts=opts.BMapNavigationControlOpts(), maptype_control_opts=opts.BMapTypeControlOpts(), #copyright_control_opts=opts.BMapCopyrightTypeOpts(copyright_="我的") #geo_location_control_opts=opts.BMapGeoLocationControlOpts() #overview_map_opts=opts.BMapOverviewMapControlOpts(is_open=True), ) #.add_js_funcs(jscode) #.set_global_opts(title_opts=opts.TitleOpts(title="BMap-基本示例")) ) return c
def timeline_bmap() -> Timeline: tl = Timeline() tl.add_schema(pos_left="50%", pos_right="10px", pos_bottom="15px") for i in range(2015, 2020): bmap = ( BMap() .add_schema(baidu_ak="FAKE_AK", center=[120.13066322374, 30.240018034923]) .add( "bmap", [list(z) for z in zip(Faker.provinces, Faker.values())], type_="heatmap", ) .set_global_opts( title_opts=opts.TitleOpts(title="Timeline-BMap-热力图-{}年".format(i)), visualmap_opts=opts.VisualMapOpts( pos_bottom="center", pos_right="10px" ), tooltip_opts=opts.TooltipOpts(formatter=None), ) ) tl.add(bmap, "{}年".format(i)) return tl
def bmap_custom() -> BMap: with open( os.path.join("fixtures", "bmap-custom-data.json"), "r", encoding="utf-8" ) as f: j = json.load(f) color_list = ["#070093", "#1c3fbf", "#1482e5", "#70b4eb", "#b4e0f3", "#ffffff"] c = ( BMap() .add_schema( baidu_ak=BAIDU_MAP_AK, center=[116.46, 39.92], zoom=11.8, is_roam=True, map_style={ "styleJson": [ { "featureType": "water", "elementType": "all", "stylers": {"color": "#d1d1d1"}, }, { "featureType": "land", "elementType": "all", "stylers": {"color": "#f3f3f3"}, }, { "featureType": "railway", "elementType": "all", "stylers": {"visibility": "off"}, }, { "featureType": "highway", "elementType": "all", "stylers": {"color": "#999999"}, }, { "featureType": "highway", "elementType": "labels", "stylers": {"visibility": "off"}, }, { "featureType": "arterial", "elementType": "geometry", "stylers": {"color": "#fefefe"}, }, { "featureType": "arterial", "elementType": "geometry.fill", "stylers": {"color": "#fefefe"}, }, { "featureType": "poi", "elementType": "all", "stylers": {"visibility": "off"}, }, { "featureType": "green", "elementType": "all", "stylers": {"visibility": "off"}, }, { "featureType": "subway", "elementType": "all", "stylers": {"visibility": "off"}, }, { "featureType": "manmade", "elementType": "all", "stylers": {"color": "#d1d1d1"}, }, { "featureType": "local", "elementType": "all", "stylers": {"color": "#d1d1d1"}, }, { "featureType": "arterial", "elementType": "labels", "stylers": {"visibility": "off"}, }, { "featureType": "boundary", "elementType": "all", "stylers": {"color": "#fefefe"}, }, { "featureType": "building", "elementType": "all", "stylers": {"color": "#d1d1d1"}, }, { "featureType": "label", "elementType": "labels.text.fill", "stylers": {"color": "rgba(0,0,0,0)"}, }, ] }, ) .add_js_funcs( """ var lngExtent = [39.5, 40.6]; var latExtent = [115.9, 116.8]; var cellCount = [50, 50]; var cellSizeCoord = [ (lngExtent[1] - lngExtent[0]) / cellCount[0], (latExtent[1] - latExtent[0]) / cellCount[1] ]; var gapSize = 0; function renderItem(params, api) { var lngIndex = api.value(0); var latIndex = api.value(1); var pointLeftTop = getCoord(params, api, lngIndex, latIndex); var pointRightBottom = getCoord(params, api, lngIndex + 1, latIndex + 1); return { type: 'rect', shape: { x: pointLeftTop[0], y: pointLeftTop[1], width: pointRightBottom[0] - pointLeftTop[0], height: pointRightBottom[1] - pointLeftTop[1] }, style: api.style({ stroke: 'rgba(0,0,0,0.1)' }), styleEmphasis: api.styleEmphasis() }; } function getCoord(params, api, lngIndex, latIndex) { var coords = params.context.coords || (params.context.coords = []); var key = lngIndex + '-' + latIndex; return coords[key] || (coords[key] = api.coord([ +(latExtent[0] + lngIndex * cellSizeCoord[0]).toFixed(6), +(lngExtent[0] + latIndex * cellSizeCoord[1]).toFixed(6) ])); } """ ) .add( series_name="", data_pair=j["data"], type_=ChartType.CUSTOM, render_item=JsCode("renderItem"), itemstyle_opts=opts.ItemStyleOpts(color="yellow"), encode={"tooltip": 2}, ) .set_global_opts( title_opts=opts.TitleOpts(title="BMap-Custom 图"), tooltip_opts=opts.TooltipOpts(is_show=True, formatter=None), visualmap_opts=opts.VisualMapOpts( is_piecewise=True, pos_top="10", pos_left="10", is_inverse=True, pieces=[ {"value": i, "color": color_list[i]} for i in range(len(color_list)) ], dimension=2, border_color="#ccc", border_width=2, background_color="#eee", range_opacity=0.7, ), graphic_opts=[ opts.GraphicGroup( graphic_item=opts.GraphicItem( rotation=JsCode("Math.PI / 4"), bounding="raw", right=110, bottom=110, z=100, ), children=[ opts.GraphicRect( graphic_item=opts.GraphicItem( left="center", top="center", z=100 ), graphic_shape_opts=opts.GraphicShapeOpts( width=400, height=50 ), graphic_basicstyle_opts=opts.GraphicBasicStyleOpts( fill="rgba(0,0,0,0.3)" ), ), opts.GraphicText( graphic_item=opts.GraphicItem( left="center", top="center", z=100 ), graphic_textstyle_opts=opts.GraphicTextStyleOpts( text="Made by pyecharts", font="bold 26px Microsoft YaHei", graphic_basicstyle_opts=opts.GraphicBasicStyleOpts( fill="#fff" ), ), ), ], ) ], ) ) return c
(BMap(init_opts=opts.InitOpts(width="1600px", height="800px")).add_schema( baidu_ak="Uf1rIjuIVVXxDwEy0iEU0tApwdoqGeGn", center=[120.13066322374, 30.240018034923], zoom=14, is_roam=True, map_style={ "styleJson": [{ "featureType": "water", "elementType": "all", "stylers": { "color": "#d1d1d1" } }, { "featureType": "land", "elementType": "all", "stylers": { "color": "#f3f3f3" } }, { "featureType": "railway", "elementType": "all", "stylers": { "visibility": "off" } }, { "featureType": "highway", "elementType": "all", "stylers": { "color": "#fdfdfd" } }, { "featureType": "highway", "elementType": "labels", "stylers": { "visibility": "off" } }, { "featureType": "arterial", "elementType": "geometry", "stylers": { "color": "#fefefe" } }, { "featureType": "arterial", "elementType": "geometry.fill", "stylers": { "color": "#fefefe" } }, { "featureType": "poi", "elementType": "all", "stylers": { "visibility": "off" } }, { "featureType": "green", "elementType": "all", "stylers": { "visibility": "off" } }, { "featureType": "subway", "elementType": "all", "stylers": { "visibility": "off" } }, { "featureType": "manmade", "elementType": "all", "stylers": { "color": "#d1d1d1" } }, { "featureType": "local", "elementType": "all", "stylers": { "color": "#d1d1d1" } }, { "featureType": "arterial", "elementType": "labels", "stylers": { "visibility": "off" } }, { "featureType": "boundary", "elementType": "all", "stylers": { "color": "#fefefe" } }, { "featureType": "building", "elementType": "all", "stylers": { "color": "#d1d1d1" } }, { "featureType": "label", "elementType": "labels.text.fill", "stylers": { "color": "#999999" } }] }).add(series_name="", type_=ChartType.LINES, data_pair=map_data, is_polyline=True, is_large=True, linestyle_opts=opts.LineStyleOpts(color="purple", opacity=0.6, width=1), effect_opts=opts.EffectOpts(trail_length=0.5)).add_control_panel( copyright_control_opts=opts.BMapCopyrightType(position=3), maptype_control_opts=opts.BMapTypeControl( type_=BMapType.MAPTYPE_CONTROL_DROPDOWN), scale_control_opts=opts.BMapScaleControlOpts(), overview_map_opts=opts.BMapOverviewMapControlOpts(is_open=True), navigation_control_opts=opts.BMapNavigationControlOpts(), geo_location_control_opts=opts.BMapGeoLocationControlOpts(), ).render("hiking_trail_in_hangzhou.html"))
def bmap_lines() -> BMap: with open( os.path.join("fixtures", "hangzhou-tracks.json"), "r", encoding="utf-8" ) as f: j = json.load(f) c = ( BMap() .add_schema( baidu_ak=BAIDU_MAP_AK, center=[120.13066322374, 30.240018034923], zoom=14, is_roam=True, map_style={ "styleJson": [ { "featureType": "water", "elementType": "all", "stylers": {"color": "#d1d1d1"}, }, { "featureType": "land", "elementType": "all", "stylers": {"color": "#f3f3f3"}, }, { "featureType": "railway", "elementType": "all", "stylers": {"visibility": "off"}, }, { "featureType": "highway", "elementType": "all", "stylers": {"color": "#fdfdfd"}, }, { "featureType": "highway", "elementType": "labels", "stylers": {"visibility": "off"}, }, { "featureType": "arterial", "elementType": "geometry", "stylers": {"color": "#fefefe"}, }, { "featureType": "arterial", "elementType": "geometry.fill", "stylers": {"color": "#fefefe"}, }, { "featureType": "poi", "elementType": "all", "stylers": {"visibility": "off"}, }, { "featureType": "green", "elementType": "all", "stylers": {"visibility": "off"}, }, { "featureType": "subway", "elementType": "all", "stylers": {"visibility": "off"}, }, { "featureType": "manmade", "elementType": "all", "stylers": {"color": "#d1d1d1"}, }, { "featureType": "local", "elementType": "all", "stylers": {"color": "#d1d1d1"}, }, { "featureType": "arterial", "elementType": "labels", "stylers": {"visibility": "off"}, }, { "featureType": "boundary", "elementType": "all", "stylers": {"color": "#fefefe"}, }, { "featureType": "building", "elementType": "all", "stylers": {"color": "#d1d1d1"}, }, { "featureType": "label", "elementType": "labels.text.fill", "stylers": {"color": "#999999"}, }, ] }, ) .add( "", type_="lines", data_pair=j, is_polyline=True, is_large=True, linestyle_opts=opts.LineStyleOpts(color="purple", opacity=0.6, width=1), ) .add_control_panel( maptype_control_opts=opts.BMapTypeControlOpts( type_=BMapType.MAPTYPE_CONTROL_DROPDOWN ), scale_control_opts=opts.BMapScaleControlOpts(), overview_map_opts=opts.BMapOverviewMapControlOpts(is_open=True), ) .set_global_opts(title_opts=opts.TitleOpts(title="BMap-杭州热门步行路线")) ) return c
BMap( init_opts=opts.InitOpts( width="1400px", height="800px"), is_ignore_nonexistent_coord=True, # 是否忽略不存在的坐标,默认值为 False,即不忽略 ) .add( series_name="pm2.5", # 系列名称,用于 tooltip 的显示,legend 的图例筛选。 data_pair=convert_data(), # 坐标点经纬度以及数值数据 type_="effectScatter", # 图类型————————散射效应图 is_selected=True, # 是否选中图例 # symbol = "roundRect", # 标记类型包括 ‘circle’, ‘rect’, ‘roundRect’, ‘triangle’,‘diamond’, ‘pin’, ‘arrow’, ‘none’, symbol_size=5, # 散点大小 color="red", # 系列label的颜色 is_polyline=False, # 是否是多段线,在画 lines 图情况下 is_large=True, # 是否启用大规模线图的优化,在数据图形特别多的时候(>=5k)可以开启 large_threshold=2000, # 开启绘制优化的阈值 # label_opts=opts.LabelOpts(formatter="{b}:{c}\n({d}%)", position="left", is_show=True), # a/b/c,分别显示pm25/城市名/经纬度/ label_opts=opts.LabelOpts(is_show=False), # 还没搞懂formatter effect_opts=opts.EffectOpts(False), # 涟漪特效功能 linestyle_opts=opts.LineStyleOpts(is_show=True), # 线样式配置项 tooltip_opts=opts.TooltipOpts(is_show=True,trigger="item",trigger_on="mousemove|click"), itemstyle_opts=opts.ItemStyleOpts(color="yellow"), # 图元样式配置项 ) .add_schema( baidu_ak="TV72R1obhPKWvnWvrlpclPYdlFExbCb1", # 百度申请,打开最后保存的网页会提示你如何申请,这里我申请的是浏览器端地图的使用权限,“”内为百度AK码 center=[107.114129, 33.550339], # 不进行任何移动缩放之前的视角的中心点,用经纬度表示 zoom=5, # 不进行任何缩放之前能看到的网页面积,越小看到区域越大(远超过中国区域),越大看到区域越小(可能进显示一个城市),貌似仅能为整数,如5.5相当于5 is_roam=True, # True,网页可用鼠标滚轮缩放,False则不行 map_style={ "styleJson": [ { "featureType": "water", # 这块儿里面的所有color建议都默认 "elementType": "all", "stylers": {"color": "#044161"}, # stylers 网页有底色,styles网页没有底色(例图如直接百度地图) }, # 建议有底色 414行设置为黄色,没有底色的409设为红色 { "featureType": "land", "elementType": "all", "stylers": {"color": "#004981"}, # python控制台设置颜色 }, { "featureType": "boundary", "elementType": "geometry", "stylers": {"color": "#064f85"}, }, { "featureType": "railway", "elementType": "all", "stylers": {"visibility": "off"}, }, { "featureType": "highway", "elementType": "geometry", "stylers": {"color": "#004981"}, }, { "featureType": "highway", "elementType": "geometry.fill", "stylers": {"color": "#005b96", "lightness": 1}, }, { "featureType": "highway", "elementType": "labels", "stylers": {"visibility": "off"}, }, { "featureType": "arterial", # 城市主干道 "elementType": "geometry", "stylers": {"color": "#004981"}, }, { "featureType": "arterial", "elementType": "geometry.fill", "stylers": {"color": "#00508b"}, }, { "featureType": "poi", # 某个地理位置周边的信息 "elementType": "all", "stylers": {"visibility": "on"}, }, { "featureType": "green", # 绿色下垫面 "elementType": "all", "stylers": {"color": "#056197", "visibility": "off"}, }, { "featureType": "subway", "elementType": "all", "stylers": {"visibility": "off"}, }, { "featureType": "manmade", # 人造的,不知道是啥??? "elementType": "all", "stylers": {"visibility": "off"}, }, { "featureType": "local", "elementType": "all", "stylers": {"visibility": "off"}, }, { "featureType": "arterial", "elementType": "labels", "stylers": {"visibility": "off"}, }, { "featureType": "boundary", "elementType": "geometry.fill", "stylers": {"color": "#029fd4"}, }, { "featureType": "building", "elementType": "all", "stylers": {"color": "#1a5787"}, }, { "featureType": "label", "elementType": "all", "stylers": {"visibility": "off"}, }, ] }, ) .set_global_opts( title_opts=opts.TitleOpts( title="全国主要城市空气质量", subtitle="df from PM25.in", subtitle_link="http://www.pm25.in", # 点击网页subtitle,可以进这个网站 pos_left="left", # 主副标题位置,最开始为center,会挡住PM25开关,改为左更好 title_textstyle_opts=opts.TextStyleOpts(color="yellow"), # 最开始颜色为#fff,但改为黄色更好看 ) ) .add_control_panel( # navigation_control_opts=opts.BMapNavigationControlOpts(position=BMapType.ANCHOR_TOP_RIGHT), opts.BMapNavigationControlOpts( # 地图的平移缩放控件 position=0, # 等同于anchor_top_left,1、2、3分别为右上,左下,右下 offset_height=50, # 距离上部偏移量 offset_width=10, # 距离左右偏移量 type_=0, # 0-4表示NAVIGATION_CONTROL_LARGE/SMALL/PAN/ZOOM,又标准平移缩放滑块/平移缩放/仅平移/仅缩放 is_show_zoom_info=True, # 是否显示级别提示信息 is_enable_geo_location=True, # 控件是否集成定位功能 ), # 若下面还有add_contorl_panel里面的空间调整,这里必须有“,”,若没有,可以没有逗号 opts.BMapOverviewMapControlOpts( # 缩略地图控件 # position=0, # 等同于anchor_top_left,1、2、3分别为右上,左下,右下 # offset_height=50, # 距离上部偏移量 # offset_width=10, # 距离左右偏移量 is_open=True, ), opts.BMapScaleControlOpts( # 比例尺控件 # position=0, # 等同于anchor_top_left,1、2、3分别为右上,左下,右下 # offset_height=50, # 距离上部偏移量 # offset_width=10, # 距离左右偏移量 ), opts.BMapTypeControlOpts( # 切换地图类型的控件 # position=0, type_=2, # MAPTYPE_CONTROL_HORIZONTAL/DROPDOWN/MAP,按钮水平方式展示为0,下拉列表为1,图片方式为2(设置该类型后无法指定 maptypes 属性) ), opts.BMapCopyrightTypeOpts( # 版权控件,您可以在地图上添加自己的版权信息 # position=0, # offset_height=50, # 距离上部偏移量 # offset_width=10, # 距离左右偏移量 copyright_="made by shh", # Copyright 的文本内容, 可以放入 HTML 标签 ), opts.BMapGeoLocationControlOpts( # 地图定位的控件,使用 HTML5 浏览器定位功能 # position=0, # offset_height=50, # 距离上部偏移量 # offset_width=10, # 距离左右偏移量 is_show_address_bar=True, # 是否显示定位信息面板。默认显示定位信息面板 is_enable_auto_location=True, # 添加控件时是否进行定位。默认添加控件时不进行定位 ) ) .render("e:/python_out/air_quality_baidu_map.html")
from pywebio.output import put_html from pyecharts import options as opts from pyecharts.charts import BMap from pyecharts.faker import Faker c = ( BMap() .add_schema(baidu_ak="FAKE_AK", center=[120.13066322374, 30.240018034923]) .add( "bmap", [list(z) for z in zip(Faker.provinces, Faker.values())], type_="heatmap", label_opts=opts.LabelOpts(formatter="{b}"), ) .set_global_opts( title_opts=opts.TitleOpts(title="BMap-热力图"), visualmap_opts=opts.VisualMapOpts() ) ) c.width = "100%" put_html(c.render_notebook())
return coord def get_data_pair(speed, n): data = [] for street in speed: spd = speed[street][n] if isnan(spd): spd = 0 data.append([street, spd]) return data if __name__ == '__main__': info = load_street_pickle() m = BMap(init_opts=opts.InitOpts(width="1600px", height="800px")) m.add_schema(baidu_ak="Uf1rIjuIVVXxDwEy0iEU0tApwdoqGeGn", center=[108.953457, 34.26949], zoom=15, is_roam=True, map_style={ "styleJson": [{ "featureType": "water", "elementType": "all", "stylers": { "color": "#d1d1d1" } }, { "featureType": "land", "elementType": "all", "stylers": {
from pyecharts import options as opts from pyecharts.charts import BMap from pyecharts.faker import Faker (BMap(init_opts=opts.InitOpts(width='900px', height='600px')).add( series_name='虚假数据', data_pair=[list(z) for z in zip(Faker.provinces, Faker.values())], label_opts=opts.LabelOpts(formatter='{b}')).add_schema( baidu_ak='P7TXy8G74PrSxmfq0L0DQ6raaN38yOWG', center=[120.13066322374, 30.240018034923]).set_global_opts( title_opts=opts.TitleOpts(title='例子')).render('my_base.html'))