Python绘制地图及其它图表

Python绘制地图及其它图表

pyecharts 分为 v0.5 和 v1 两个大版本,v0.5 和 v1 间不兼容。最早写这篇文章的时候还是0.5版本,里面的代码都运行正常。但是最近大家反馈说代码不能正常使用,因为我也有一段时间没有使用pyecharts,所以今天就特意去官网研究了一下,顺便也把旧的代码更新掉。

由于0.5.x 版本将不再进行维护,建议大家都用最新版本。官网和Github地址如下,大家直接有问题,可以先上这三个网址看看。第三个网址是所有的案例,里面有实例代码。

A Python Echarts Plotting Librarypyecharts.org
pyecharts/pyechartsgithub.com图标Documentgallery.pyecharts.org

(1)中国人口地图

pyecharts在v1.x之后支持链式调用

from pyecharts.charts import Map,Geo
from pyecharts import options as opts
data=[("广东",10430.03),("山东",9579.31),("河南",9402.36),("四川",8041.82),("江苏",7865.99),("河北",7185.42),("湖南",6568.37),("安徽",5950.1),("浙江",5442),("湖北",5723.77),("广西",4602.66),("云南",4596.6),("江西",4456.74),("辽宁",4374.63),("黑龙江",3831.22),("陕西",3732.74),("山西",3571.21),("福建",3552),("重庆",2884),("贵州",3476.65),("吉林",2746.22),("甘肃",2557.53),("内蒙古",2470.63),("上海",2301.391),("台湾",2316.2),("新疆",2181.33),("北京",1961.2),("天津",1293.82),("海南",867.15),("香港",709.76),("青海",562.67),("宁夏",630.14),("西藏",300.21),("澳门",55.23)]
map=(
    Map()
    .add("",data,"china")
    .set_global_opts(
        title_opts=opts.TitleOpts(title="各省市人口数",subtitle="数据来源:中国统计年鉴(万人)",pos_right="center",pos_top="5%"),
        visualmap_opts=opts.VisualMapOpts(max_=12000),
    )  
)
map.render_notebook()

不习惯链式调用的话,你也可以单独调用

from pyecharts.charts import Map,Geo
from pyecharts import options as opts
data=[("广东",10430.03),("山东",9579.31),("河南",9402.36),("四川",8041.82),("江苏",7865.99),("河北",7185.42),("湖南",6568.37),("安徽",5950.1),("浙江",5442),("湖北",5723.77),("广西",4602.66),("云南",4596.6),("江西",4456.74),("辽宁",4374.63),("黑龙江",3831.22),("陕西",3732.74),("山西",3571.21),("福建",3552),("重庆",2884),("贵州",3476.65),("吉林",2746.22),("甘肃",2557.53),("内蒙古",2470.63),("上海",2301.391),("台湾",2316.2),("新疆",2181.33),("北京",1961.2),("天津",1293.82),("海南",867.15),("香港",709.76),("青海",562.67),("宁夏",630.14),("西藏",300.21),("澳门",55.23)]
map=Map()
map.add("",data,"china")
map.set_global_opts(
    title_opts=opts.TitleOpts(title="各省市人口数",subtitle="数据来源:中国统计年鉴(万人)",pos_right="center",pos_top="5%"),
    visualmap_opts=opts.VisualMapOpts(max_=12000),
    ) 
map.render_notebook()

(2)世界地图

from pyecharts import options as opts
from pyecharts.charts import Map
from pyecharts.faker import Faker
map = (
    Map()
    .add("", [list(z) for z in zip(Faker.country, Faker.values())], "world")
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
    .set_global_opts(
        title_opts=opts.TitleOpts(title="Map-世界地图",pos_right='center'),
        visualmap_opts=opts.VisualMapOpts(max_=200),)
)
# 单独调用
# map = Map()
# map.add("商家A", [list(z) for z in zip(Faker.country, Faker.values())], "world")
# map.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
# map.set_global_opts(
#         title_opts=opts.TitleOpts(title="Map-世界地图"),
#         visualmap_opts=opts.VisualMapOpts(max_=200),)

map.render_notebook()

(3)热力图

from pyecharts import options as opts 
from pyecharts.charts import Geo 
from pyecharts.globals import GeoType #Geo图的类型
data=[ ("北京","2171"),("天津","1557"),("河北","7520"),("内蒙古","2529"),("辽宁","4369"),("吉林","2717"), ("黑龙江","3789"),("上海","2418"),("江苏","8029"),("浙江","5657"),("安徽","6255"),("福建","3911"), ("江西","4622"),("山东","10006"),("河南","9559"),("湖北","5902"),("湖南","6860"),("广东","11169"), ("广西","4885"),("海南","926"),("重庆","3075"),("四川","8302"),("贵州","3580"),("云南","4801"), ("西藏","337"),("陕西","3835"),("甘肃","2626"),("青海","598"),("宁夏","682"),("新疆","2445"),]
pop_geo = ( 
    Geo() .add_schema(maptype="china") 
    .add("",data,type_=GeoType.HEATMAP) #热度效果HEATMAP
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False))  
    .set_global_opts(title_opts=opts.TitleOpts(title="全国年度人口数",subtitle="数据来源:中国统计年鉴(万人)",pos_right="center",pos_top="5%"), 
                     visualmap_opts=opts.VisualMapOpts(max_=11169,pos_left="8%",is_piecewise=True))
)
pop_geo.render_notebook()

(4)散点图

from pyecharts import options as opts 
from pyecharts.charts import Geo
data=[ ("北京","2171"),("天津","1557"),("河北","7520"),("内蒙古","2529"),("辽宁","4369"),("吉林","2717"), ("黑龙江","3789"),("上海","2418"),("江苏","8029"),("浙江","5657"),("安徽","6255"),("福建","3911"), ("江西","4622"),("山东","10006"),("河南","9559"),("湖北","5902"),("湖南","6860"),("广东","11169"), ("广西","4885"),("海南","926"),("重庆","3075"),("四川","8302"),("贵州","3580"),("云南","4801"), ("西藏","337"),("陕西","3835"),("甘肃","2626"),("青海","598"),("宁夏","682"),("新疆","2445"),]
pop_geo = ( 
    Geo() 
    .add_schema(maptype="china") 
    .add("",data) 
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False)) #标签显示即为每个地区对应的人口数 
    .set_global_opts(title_opts=opts.TitleOpts(title="全国年度人口数",subtitle="数据来源:中国统计年鉴(万人)",pos_right="center",pos_top="5%"), 
                     visualmap_opts=opts.VisualMapOpts(max_=11169,pos_left="8%",is_piecewise=True))
)
pop_geo.render_notebook()

(5)航线动态图

from pyecharts import options as opts 
from pyecharts.charts import Geo # 地理坐标系绘制方法
from pyecharts.globals import GeoType,ThemeType,SymbolType #Geo图的类型 主题 涟漪图形符号
data=[("四川","8302"),("山东","10006"),("河南","9559"),("湖南","6860"),("广东","11169")]
geo = (
    Geo(init_opts=opts.InitOpts(width="600px",height="500px",theme=ThemeType.DARK))  
    .add_schema(maptype="china",
                #itemstyle_opts=opts.ItemStyleOpts(color='white',border_color="#EE2C2C")#地图背景色
                #zoom=1.5,
                #is_roam=True,
                #center=Geo().get_coordinate('广东'),  # 视角中心
                emphasis_itemstyle_opts=opts.ItemStyleOpts(color="#31708f"),  # 高亮颜色
                # emphasis_label_opts=opts.ItemStyleOpts(color="white", )  # 字体颜色
               )
    
    .add("",
         data,
         type_=GeoType.EFFECT_SCATTER,
         symbol_size=6
        )
    
    .add("",
         [("广东","四川"),("广东","山东"),("广东","湖南"),("广东","河南")],
         type_=GeoType.LINES, 
         effect_opts=opts.EffectOpts(symbol=SymbolType.ARROW,symbol_size=6,color='#5f99bb'),
         linestyle_opts=opts.LineStyleOpts(curve=0.2,color="#B0E2FF"))
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False))  
    .set_global_opts(title_opts=opts.TitleOpts(title="广东出发的航班",subtitle="数据来源:中国民航局官方网站",pos_right="center",pos_top="5%")) 
)
geo.render_notebook()

(6)其他图表比如组合图

pyecharts能实现非常多的图表,基本上你日常用到的图表,都能实现。有兴趣的话,自己去官网下载案例研究。官网的案例非常全面。

from pyecharts import options as opts
from pyecharts.charts import Map, Bar, Grid
from pyecharts.globals import ChartType, ThemeType
import random

province = ['武汉', '十堰', '鄂州', '宜昌', '荆州', '孝感', '黄石', '咸宁', '仙桃']
data = [100,150 ,123 ,210 ,168 ,234 ,350 ,435 ,342 ]
bar = (Bar()
       .add_xaxis(province)
       .add_yaxis('营业额', data)
       .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
       .set_global_opts(
            title_opts=opts.TitleOpts(title="Grid-Bar")
        )
      )

line = (Line()
       .add_xaxis(province)
       .add_yaxis('营业额', data, 
                  markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")]))
       .set_global_opts(title_opts=opts.TitleOpts(title="Grid-Line", pos_top="48%"))
      )

grid = (
        Grid()
        .add(bar, grid_opts=opts.GridOpts(pos_bottom="60%"))
        .add(line, grid_opts=opts.GridOpts(pos_top="60%"))
    )

grid.render_notebook()

编辑于 04-20

文章被以下专栏收录