This article introduces Pyecharts geographic data visualization, which is shared as follows:
I. Introduction and Installation of Pyecharts
1. Introduction
Echarts is an open source data visualization by Baidu, which has been recognized by many developers for its good interactivity and sophisticated chart design. And Python is an expressive language, very suitable for data processing. When data analysis meets data visualization, pyecharts is born.
- Simple API design, silky smooth use, support for chained calls
- Includes 30+ common charts and everything in between
- Supports major Notebook environments, Jupyter Notebook and JupyterLab.
- Easily integrates with major web frameworks such as Flask, Sanic, Django, etc.
- Highly flexible configuration items that can be easily paired with beautiful charts
- Detailed documentation and examples to help developers get started with their projects faster
- Up to 400+ map files and native Baidu map support, providing strong support for geographic data visualization
Incompatibility between pyecharts versions v0. and v1.The syntax of v1 is quite different.
2. Installation
Install pyecharts
pip install pyecharts -i /simple --trusted-host
import pyecharts print(pyecharts.__version__) # View currentpyechartsreleases
Installation of relevant map expansion packs
pip install -i /simple echarts-countries-pypkg # Global country maps pip install -i /simple echarts-china-provinces-pypkg # Provincial Maps of China pip install -i /simple echarts-china-cities-pypkg # China Municipal Maps pip install -i /simple echarts-china-counties-pypkg # County and district level map of China
II. Map visualization
1. World map
Using the data in this study, we first calculate the number of stores corresponding to each country, and then use a world map to visualize the distribution of Starbucks stores around the world.
# -*- coding: UTF-8 -*- """ @File : @Author: Tingyun Ye @CSDN :/ """ import pandas as pd from import Map from pyecharts import options as opts from import ThemeType, CurrentConfig CurrentConfig.ONLINE_HOST = 'D:/python/pyecharts-assets-master/assets/' # pandas reads data from a csv file df = pd.read_csv("")['Country'] # of Starbucks stores by region data = df.value_counts() datas = [(i, int(j)) for i, j in zip(, )] # Instantiate a Map object map_ = Map(init_opts=(theme=ThemeType.PURPLE_PASSION)) # Map of the world map_.add("Number of stores", data_pair=datas, maptype="world") map_.set_series_opts(label_opts=(is_show=False)) # Do not show the label map_.set_global_opts( title_opts=(title="Global Distribution of Starbucks Store Numbers.", pos_left='40%', pos_top='10'), # Adjust title position legend_opts=(is_show=False), visualmap_opts=(max_=13608, min_=1, is_piecewise=True, pieces=[{"max": 9, "min": 1, "label": "1-9", "color": "#00FFFF"}, # Segmentation Add legend comments and colors {"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"} ]) ) # Rendered on the web map_.render('Starbucks stores worldwide.html')
The running effect is as follows:
2. Country maps
Ripple Scatterplot
Using the data in pyecharts, we first calculate the number of stores in each city, and then use the Geo module in the pyecharts package to create a ripple scatter map of the number of Starbucks stores in each city in China.
import pandas as pd from import ThemeType, CurrentConfig, GeoType from pyecharts import options as opts from import Geo CurrentConfig.ONLINE_HOST = 'D:/python/pyecharts-assets-master/assets/' # pandas reads csv file data df = pd.read_csv("")['City'] data = df.value_counts() datas = [(i, int(j)) for i, j in zip(, )] print(datas) geo = Geo(init_opts=(width='1000px', height='600px', theme=)) geo.add_schema(maptype='china', label_opts=(is_show=True)) # show label Province name ('Number of stores', data_pair=datas, type_=GeoType.EFFECT_SCATTER, symbol_size=8) geo.set_series_opts(label_opts=(is_show=False)) geo.set_global_opts(title_opts=(title='Distribution of Starbucks stores in China'), visualmap_opts=(max_=550, is_piecewise=True, pieces=[{"max": 50, "min": 0, "label": "0-50", "color": "#708090"}, # Segmentation Add legend comments and colors {"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"}, ]) ) ("Distribution of Starbucks stores in China.html")
The running effect is as follows:
Dynamic trajectory diagram
# -*- coding: UTF-8 -*- """ @File : @Author: Tingyun Ye @CSDN :/ """ from pyecharts import options as opts from import Geo from import ChartType, SymbolType, CurrentConfig, ThemeType CurrentConfig.ONLINE_HOST = 'D:/python/pyecharts-assets-master/assets/' # Chained calls c = ( Geo() .add_schema( maptype="china", itemstyle_opts=(color="#323c48", border_color="#111"), label_opts=(is_show=True) ) .add( "", [("Guangzhou.", 55), ("Beijing.", 66), ("Hangzhou", 77), ("Chongqing.", 88), ('Chengdu', 100), ('Haikou', 80)], type_=ChartType.EFFECT_SCATTER, color="white", ) .add( "", [("Guangzhou.", "Shanghai."), ("Guangzhou.", "Beijing."), ("Guangzhou.", "Hangzhou"), ("Guangzhou.", "Chongqing."), ('Chengdu', 'Haikou'), ('Haikou', 'Beijing'), ('Haikou', 'Chongqing'), ('Chongqing', 'Shanghai') ], type_=, effect_opts=( symbol=, symbol_size=6, color="blue" # Trajectory line blue ), linestyle_opts=(curve=0.2), # Trajectory line curvature ) .set_series_opts(label_opts=(is_show=False)) .set_global_opts(title_opts=(title="Dynamic trajectory map")) .render("geo_lines_background.html") )
The running effect is as follows:
3. Provincial and municipal maps
heat map
# -*- coding: UTF-8 -*- """ @File : @Author: Tingyun Ye @CSDN :/ """ from pyecharts import options as opts from import Geo from import Faker from import GeoType, CurrentConfig CurrentConfig.ONLINE_HOST = 'D:/python/pyecharts-assets-master/assets/' c = ( Geo() .add_schema(maptype="Guangdong", label_opts=(is_show=True)) .add( "Heat map.", [list(z) for z in zip(Faker.guangdong_city, ())], type_=, ) .set_series_opts(label_opts=(is_show=True)) .set_global_opts( visualmap_opts=(), title_opts=(title="Geo-Guangdong map") ) .render("geo_guangdong.html") )
The running effect is as follows:
Batch add latitude and longitude data on map
Data from Meituan.com Chengdu area hotel information, using which the hotel's latitude and longitude data, batch add on the map visualization.
# -*- coding: UTF-8 -*- """ @File : @Author: Tingyun Ye @CSDN :/ """ import pandas as pd from import Geo from pyecharts import options as opts from import GeoType, CurrentConfig, ThemeType CurrentConfig.ONLINE_HOST = 'D:/python/pyecharts-assets-master/assets/' # Read Excel data Data Source Meituan.com Hotel Information df = pd.read_excel("") # Get location and latitude and longitude information # geo_sight_coord = {[i]['Hotel address']: [[i]['Longitude'], [i]['Latitude']] for i in range(len(df))} data = [(df['Hotel address'][j], f"{int(df['Lowest price'][j])}unit of money (in PRC: Chinese yuan, in USA: dollar, etc)(lowest price)") for j in range(len(df))] # print(data) # print(geo_sight_coord) # Instantiate Geo objects Import Chengdu map g = Geo(init_opts=(theme=ThemeType.PURPLE_PASSION, width="1000px", height="600px")) g.add_schema(maptype="Chengdu.") for k, v in list(geo_sight_coord.items()): # Add address, latitude and longitude data g.add_coordinate(k, v[0], v[1]) # Generate ripple scatterplots ("", data_pair=data, type_=GeoType.EFFECT_SCATTER, symbol_size=6) g.set_series_opts(label_opts=(is_show=False)) g.set_global_opts(title_opts=(title="Chengdu - Hotel Address Distribution")) ("HotelAddressDistribution.html")
The running effect is as follows:
To this article about an article to take you to master Pyecharts geographic data visualization method of the article is introduced to this, more related Pyecharts geographic data visualization content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!