小白教程

 找回密码
 立即注册
小白教程 首页 Python技术文章 查看内容

带你掌握Pyecharts地理数据可视化

发布者: 小白教程

一、Pyecharts简介和安装

1. 简介

Ecarts是百度开发的一个数据可视化开源软件,具有良好的交互性,精美的图表设计,得到了众多开发者的认可。Python是一种表达性语言,适合于数据处理。当数据分析遇到数据可视化时,pyecharts就诞生了。
简单的API设计,使用流畅,支持链式调用
它包括30种常用图表
支持主流笔记本环境,朱庇特笔记本和朱庇特实验室
它可以很容易地集成到主流的web框架中,如flame, sanic和Django
高度灵活的配置项目,可以轻松匹配美丽的图表
帮助开发人员更快启动项目的详细文档和示例
多达400 +地图文件,并支持原始百度地图,为地理数据可视化提供强大支持
v0.5 Pyecarts版本。X与v1不兼容。V1是一个新版本,语法也非常不同。

2. 安装

安装pyecharts


pip install pyecharts -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
import pyecharts

print(pyecharts.__version__)         # 查看当前pyecharts版本
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple echarts-countries-pypkg   		# 全球国家地图
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple echarts-china-provinces-pypkg   # 中国省级地图
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple echarts-china-cities-pypkg      # 中国市级地图
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple echarts-china-counties-pypkg    # 中国县区级地图

二、地图可视化

1. 世界地图

使用Starbucks .csv中的数据,我们首先计算每个国家的门店数量,然后使用世界地图来可视化星巴克门店的全球分布。

# -*- coding: UTF-8 -*-
"""
@File    :demo1.py
@Author  :叶庭云
@CSDN    :https://yetingyun.blog.csdn.net/
"""
import pandas as pd
from pyecharts.charts import Map
from pyecharts import options as opts
from pyecharts.globals import ThemeType, CurrentConfig

CurrentConfig.ONLINE_HOST = 'D:/python/pyecharts-assets-master/assets/'

# pandas读取csv文件里的数据
df = pd.read_csv("Starbucks.csv")['Country']
# 统计各个地区星巴克门店数量
data = df.value_counts()
datas = [(i, int(j)) for i, j in zip(data.index, data.values)]


# 实例化一个Map对象
map_ = Map(init_opts=opts.InitOpts(theme=ThemeType.PURPLE_PASSION))
# 世界地图
map_.add("门店数量", data_pair=datas, maptype="world")
map_.set_series_opts(label_opts=opts.LabelOpts(is_show=False))   # 不显示label
map_.set_global_opts(
     title_opts=opts.TitleOpts(title="星巴克门店数量在全球分布", pos_left='40%', pos_top='10'),   # 调整title位置
     legend_opts=opts.LegendOpts(is_show=False),
     visualmap_opts=opts.VisualMapOpts(max_=13608, min_=1, is_piecewise=True,
     pieces=[{"max": 9, "min": 1, "label": "1-9", "color": "#00FFFF"},        # 分段  添加图例注释和颜色
          {"max": 99, "min": 10, "label": "10-99", "color": "#A52A2A"},
          {"max": 499, "min": 100, "label": "100-499", "color": "#0000FF	"},
          {"max": 999, "min": 500, "label": "500-999", "color": "#FF00FF"},
          {"max": 2000, "min": 1000, "label": "1000-2000", "color": "#228B22"},
          {"max": 3000, "min": 2000, "label": "2000-3000", "color": "#FF0000"},
          {"max": 20000, "min": 10000, "label": ">=10000", "color": "#FFD700"}
             ])
     )

# 渲染在网页上
map_.render('星巴克门店在全球的分布.html')

涟漪散点图

利用China - csv中的数据,首先计算出每个城市对应的门店数量,然后利用pyecharts包中的geo模块绘制出中国星巴克立面门店数量分布的ripple scatter map。

import pandas as pd
from pyecharts.globals import ThemeType, CurrentConfig, GeoType
from pyecharts import options as opts
from pyecharts.charts import Geo

CurrentConfig.ONLINE_HOST = 'D:/python/pyecharts-assets-master/assets/'
# pandas读取csv文件数据
df = pd.read_csv("china.csv")['City']
data = df.value_counts()

datas = [(i, int(j)) for i, j in zip(data.index, data.values)]
print(datas)

geo = Geo(init_opts=opts.InitOpts(width='1000px', height='600px', theme=ThemeType.DARK))
geo.add_schema(maptype='china', label_opts=opts.LabelOpts(is_show=True))   # 显示label  省名
geo.add('门店数量', data_pair=datas, type_=GeoType.EFFECT_SCATTER, symbol_size=8)
geo.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
geo.set_global_opts(title_opts=opts.TitleOpts(title='星巴克门店在中国的分布'),
                    visualmap_opts=opts.VisualMapOpts(max_=550, is_piecewise=True,
                    pieces=[{"max": 50, "min": 0, "label": "0-50", "color": "#708090"},        # 分段  添加图例注释  和颜色
                              {"max": 100, "min": 51, "label": "51-100", "color": "#00FFFF"},
                              {"max": 200, "min": 101, "label": "101-200", "color": "#00008B"},
                              {"max": 300, "min": 201, "label": "201-300", "color": "#8B008B"},
                              {"max": 600, "min": 500, "label": "500-600", "color": "#FF0000"},
                                 ])
                    )

geo.render("星巴克门店在中国的分布.html")

Archiver|手机版|小黑屋|小白教程 ( 粤ICP备20019910号 )

GMT+8, 2024-9-19 12:21 , Processed in 0.017790 second(s), 15 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

返回顶部