Today, the boss suddenly thought that it was boring to just look at the Excel sheet, so we need to put the data on the map, so today we are going to study how to draw the scatter chart on the map, the main content of today is like this

  • Use the geographic information in the library to draw the map
  • Exception handling

Then, in the second phase, how to import longitude and latitude information

Today we’ll switch to a new library and instead of looking at the Matplotlib brand, we’ll try the awesome Echarts Python version

First let’s look at the official example


It’s awesome. It’s easy to draw dots on a map and add a filter bar that can be pulled

Let’s look at the code for the official example:

The from pyecharts import Geo data = [(" haimen ", 9), (" erdos ", 12), (" zhaoyuan ", 12), (" zhoushan ", 12), (" qiqihar, 14), (" yancheng ", 15), (" chifeng ", 16), (" Qingdao ", 18), (" rushan mountain ", 18), (" jinchang, 19), (" quanzhou ", 21), (" lacey, 21), (" sunshine ", 21), (" jiaonan, 22), (" nantong ", 23), (" Lhasa ", 24), (" yunfu." 24), (" meizhou ", 25), (" - ", 25), (" Shanghai ", 25), (" panzhihua, 25), (" weihai, 25), (" chengde, 25), (" xiamen ", 26), (" shanwei, 26), (" chaozhou ", 26), (" dandong," 27), (" taicang ", 27), (" qujing ", 27), (" yantai ", 28), (" fuzhou ", 29), (" wafangdian, 30), (" cieme, 30), (" fushun, 31), (" yuxi ", 31), (" zhangjiakou, 31), (" yangquan ", 31), (" state ", 32), (" lake ", 32), (" shantou ", 32), (" kunshan ", 33), (" ningbo ", 33), (" zhanjiang ", 33), (" jieyang, 34), (" rongcheng city ", 34), (" lianyungang, 35), (" huludao," 35), (" changshu ", 36), (" dongguan ", 36), (" source ", 36), (" huaian ", 36), (" taizhou ", 36), (" nanning ", 37), (" yingkou ", 37), (" huizhou ", 37), (" jiangyin ", 37), (" penglai ", 37), (" shaoguan, 38), (" jiayuguan, 38), (" guangzhou ", 38), (" yan ", 38), (" taiyuan ", 39), (" qingyuan, 39), (" zhongshan ", 39), (" kunming ", 39), (" shouguang, 40), (" file ", (40), changzhi, 41), (" shenzhen ", 41), (" zhuhai ", 42), (43) "suqian,", (" xianyang ", 43), (" tongchuan, 44), (" pingdu, 44), (", "foshan, 44), (" haikou", 44), (" jiangmen ", 45), (" chapter grave ", 45), (" zhaoqing ", 46), (" dalian ", 47), (" linfen ", 47), (" wu jiang ", 47), (" making ", 49), (" shenyang ", 50), (" suzhou ", 50), (" maoming, 50), (" jiaxing ", 51), (" changchun ", 51), (" jiaozhou ", 52), (" yinchuan ", 52), (" zhangjiagang, 52), (" sanmenxia, 53), (" jinzhou ", 54), (" nanchang, 54), (" liuzhou ", 54), (" sanya ", 54), (" city ", 56), (" jilin ", 56), (" yangjiang ", 57), (" luzhou ", 57), (" xining, 57), (" yibin, 58), (" Hohhot, 58), (" chengdu ", 58), (" datong ", 58), (" zhenjiang ", 59), (" guilin ", 59), (" zhangjiajie ", 59), (" yixing ", 59), (" north sea ", 60), (" xian ", 61), (" tan ", 62), (" dongying ", 62), (" mudanjiang ", 63), (" zunyi ", 63), (" shaoxing ", 63), (" yangzhou." 64), "changzhou", (64), (" weifang ", 65), (" chongqing ", 66), (" taizhou ", 67), (" nanjing ", 67), (" binzhou ", 70), (" guiyang ", 71), (" wuxi ", 71), (" benxi ", 71), (" karamay." 72), (" weinan ", 72), (" ma on shan ", 72), (" baoji ", 72), (" coke ", 75), (" jurong ", 75), (" Beijing ", 79), (" xuzhou ", 79), (" hengshui ", 80), (" baotou ", 80), (" city ", 80), "in urumqi," (84), (" zaozhuang ", 84), (" hangzhou ", 84), (" zibo ", 85), (" anshan ", 86), (" liyang, 86), (" korla ", 86), (" anyang ", 90), (" kaifeng ", 90), (" jinan ", 92), (" deyang ", 93), (" wenzhou ", 95), (" jiujiang ", 96), (" handan ", 98), (" linan ", 99), (" lanzhou ", 99), (" cangzhou ", 100), (" linyi ", 103), (" nanchong ", 104), (" tianjin ", 105), (" fuyang ", 106), (" taian ", 112), (" zhuji ", 112), (" zhengzhou ", 113), (" Harbin ", 114), (" liaocheng ", 116), (" wuhu ", 117), (" tangshan ", 119), (" mesa ", 119), (" xingtai ", 119), (" state ", 120), (" jining ", 120), (" jingzhou ", 127), (" yichang ", 130), (" yiwu ", 132), (" lishui ", 133), (" luoyang ", 134), (" qinhuangdao." 136), (" zhuzhou ", 143), (" shijiazhuang ", 147), (" laiwu ", 148), (" changde ", 152), (" baoding ", 153), (" xiangtan ", 154), (" jinhua ", 157), (" state ", 169), (" changsha ", 175), (" & ", 177), (" langfang ", 193), (" heze ", 194), (" hefei ", 229), (" wuhan ", 273), (" daqing ", 279)] geo = geo (" major urban air quality in China," "Data from pm2.5", title_color="# FFF ", title_pos="center", width=1200, height=600, background_color='#404a59') value = geo.cast(data) geo.add("", attr, value, visual_range=[0, 200], visual_text_color="#fff", symbol_size=15, is_visualmap=True) geo.render()Copy the code

The code is simple:

Data = [(" haimen ", 9),(" erdu ", 12)]Copy the code

Data is in lists, wrapped in tuples, place names are in text, and maps should come with place names

Title_pos ="center", width=1200, height=600, background_color='#404a59')Copy the code

In Geo, the title, subtitle, title color, position, size, and background color are determined, and the color is #404a59, which is a hexadecimal expression. You can find special online conversion sites to convert RBG values into this hexadecimal.

You can see the official description of parameters in GEO

attr, value = geo.cast(data)
Copy the code

This should be geo’s own data format conversion method, the official document has written

  • Attr -> list Attribute names
  • Value -> list Specifies the value of the property

We can see that cast splits the tuple into two lists, attr for place names and value for values

geo.add("", attr, value, visual_range=[0, 200], visual_text_color="#fff",
        symbol_size=15, is_visualmap=True)
Copy the code

Add includes the details in the picture, I will copy the official one directly!! The name of the legend

  • Attr -> list Attribute names
  • Value -> list Specifies the value of the property
  • Type -> STR Legend type, optional ‘Scatter’, ‘effectScatter’, and’ HeatMap ‘. The default is’ scatter ‘
  • Maptype -> STR maptype. Support China, World, Anhui, Macao, Beijing, Chongqing, Fujian, Fujian, Gansu, Guangdong, Maps of Guangxi, Guangzhou, Hainan, Hebei, Heilongjiang, Henan, Hubei, Hunan, Jiangsu, Jiangxi, Jilin, Liaoning, Inner Mongolia, Ningxia, Qinghai, Shandong, Shanghai, Shaanxi, Shanxi, Sichuan, Taiwan, Tianjin, Hong Kong, Xinjiang, Tibet, Yunnan, Zhejiang, and 363 second-tier cities. Reminder: when drawing municipal maps, the ‘city’ after the name of the city should be omitted. For example, the ‘city’ of Shijiazhuang should not be mentioned, that is, ‘Shijiazhuang’ is ok.
  • Symbol_size -> int Specifies the graphic size. The default is 12
  • Border_color -> STR map border color. The default is’ # 111 ‘
  • Geo_normal_color -> STR The normal color of the map area. The default is’ # 323 c48 ‘
  • Geo_emphasis_color -> STR Highlights the color of the map area. The default is’ # 2 a333d ‘
  • Geo_cities_coords -> dict User – defined latitude and longitude, such as {‘ a ‘: [126.58, 45.32],} dictionary, when used to provide this parameter, will overwrite the original stored region coordinates information.
  • Is_roam -> bool Indicates whether to enable mouse zooming and scrolling. Default True if you only want to enable scaling or panning, you can set it to ‘scale’ or ‘move’. Set to True to enable all

Look back very simple ah, let us personally put their own data into see, first install package

pip install pyecharts 
Copy the code

However, however, however ~~~~

Everything is not so smooth !!!!!

Why can’t I display the map ~~~~???? .

Let’s solve the first problem: why can’t I display the map background!

The new library does not come with maps, so you need to install it yourself.

pip install echarts-countries-pypkg
pip install echarts-china-provinces-pypkg
pip install echarts-china-cities-pypkg
Copy the code

Ok, we’ll continue after installation!

What,


Map still not displayed! ~! ~!

Finally, through my every probe, test, finally found a solution!!

Delete these three packages and reinstall them. Vomit blood

Ok, we finished the first teaching, studied the official example, and then we tried to use our own longitude and latitude excel table and data, then generated