ptecharts更新到v1了,v0.x的代码都不能使用了,都得重写了,更新后的pyecharts可定制化程度更高了,几乎每一个组件都可以自己定制样式了。并且现在还多了一个BMAP,这个地图可以基于百度地图来进行可视化,效果还是很不错的。下面我就给出我写的一个geo和BAMP的例子,也作为备份,之后画图可以直接调用。pyecharts官网链接。
本文的案例的数据如下所示:
geo的案例所使用的数据为:
BMAP案例的数据为:
{
"A": [120.075231,30.311932],
"B":[120.07656,30.302422],
"C":[120.089388,30.300863],
"D":[120.099161,30.301269],
"E":[120.097832,30.316172],
"F":[120.086118,30.316452]
}
下面直接给出代码,不多说了。
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType
import json
from pyecharts.globals import CurrentConfig,NotebookType
CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB
df = pd.read_excel("信号机销量统计20190807(2002-2019).xlsx",sheet_name='2002年-2019年累计',encoding = 'utf-8')
df
行标签 | 2019年 | |
---|---|---|
0 | 菏泽 | 100 |
1 | 日照 | 27 |
2 | 建瓯 | 1356 |
3 | 台州 | 13 |
4 | 玉溪 | 1 |
5 | 桂林 | 5122 |
6 | 中山 | 122 |
7 | 广州 | 2589 |
8 | 遂宁 | 60 |
cities = df['行标签']
values = df['2019年']
values.fillna(0 , inplace = True)
values
0 100
1 27
2 1356
3 13
4 1
5 5122
6 122
7 2589
8 60
Name: 2019年, dtype: int64
GEO
# 热点图
data = [list(z) for z in zip(cities, values)]
datamap = (
Geo(init_opts=opts.InitOpts(
# 设置宽度、高度
width='1200px',
height='700px',
bg_color = 'grey',
theme='white'
)
)
.add_schema(maptype="china")
.add_coordinate_json("citiesnotinmap.json")
.add(
"销售量", #图例的名称
data,
type_='effectScatter', #地图类型
symbol = 'circle',
symbol_size = 8
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(is_show=True,type_='color',
# min_=0,max_=3000,),
is_piecewise=True,
pieces=[
# {"min": 1500}, // 不指定 max,表示 max 为无限大(Infinity)。
{"min": 2001, "max": 3000, "label": '2001-3000'},
{"min": 1001, "max": 2000, "label": '1001-2000'},
{"min": 501, "max": 1000, "label": '501-1000'},
{"min": 301, "max": 500, "label": '301-500'},
{"min": 101, "max": 300, "label": '101-300'},
{"min": 0, "max": 100, "label": '0-100'},
# {"value": 123, "label": '123(自定义特殊颜色)', "color": 'grey'}, //表示 value 等于 123 的情况
# {"max": 5} // 不指定 min,表示 min 为无限大(-Infinity)。
]),
title_opts=opts.TitleOpts(title="销售数量可视化",pos_left='center',subtitle="machine",
title_textstyle_opts=opts.TextStyleOpts(color="red",font_size=40),
subtitle_textstyle_opts=opts.TextStyleOpts(color='black',font_size=20)),
legend_opts=opts.LegendOpts(is_show = False),
)
)
datamap.render("销售数量可视化.html")
'C:\\Users\\MI\\Desktop\\pyecharts-geo\\销售数量可视化.html'
BMAP
from pyecharts import options as opts
from pyecharts.charts import BMap
from pyecharts.faker import Faker
from pyecharts.globals import BMapType, ChartType
name = ['A','B','C','D','E','F']
values = [1,1,1,1,1,1]
datamap = (
BMap(init_opts=opts.InitOpts(width="1400px", height="800px"))
.add_schema(baidu_ak="your baidumap ak",
center=[120.088058,30.307723],
zoom=15,
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_coordinate_json("BMAP.json")
.add(
"bmap",
[list(z) for z in zip(name, values)],
type_="effectScatter",
label_opts=opts.LabelOpts(formatter="{b}", position="right", is_show=True),
itemstyle_opts=opts.ItemStyleOpts(color="purple"),
)
.set_global_opts(
title_opts=opts.TitleOpts(
title="紫金港打几个点",
subtitle="just see the bmap's function",
subtitle_link="http://www.pm25.in",
pos_left="center",
title_textstyle_opts=opts.TextStyleOpts(color="#fff"),
),
tooltip_opts=opts.TooltipOpts(trigger="item"),
)
.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(),
)
.render("bmap_base.html")
)
效果如下所示:
geo画出来的图
BMAP画出来的图
不存在map_style时的情况
存在map_style时的情况