In this article, you will learn how to create a perfect Map of China using Python
Yesterday, suddenly early in the morning, the weibo hot search # Ministry of Natural Resources inspection and treatment of the problem of China map # flooded the screen, trance saw recently in pursuit of “dear, love”. Xiaobian is still immersed in the sweet story, and now there is a new hot search!!
As a jia deep (MAO) drama fan, with a happy (BA) happy (GUa) mood browsing hot search, huh?? There seems to be something wrong…
The “problem map” in episode 39 has aroused widespread concern and discussion.
There are some mistakes in the maps used in the drama, which indicate that the national boundaries between Aksaichin and China’s southern Tibet, the background color of China’s Taiwan and Hainan Island are inconsistent with that of the mainland, the south China Sea islands and the South China Sea dotted line are omitted, and the Kashmir region does not conform to relevant national regulations.
Many netizens said that since the drama is aired in public, it should be properly censored, and both producers and censors should be cautious about national sovereignty and adhere to the principle of an integrated country.
Not a storm in a teacup! The national map, like the national anthem and the national flag, is an important symbol of a sovereign state. It is not just a picture that reflects a country’s complete political views. A wrong map of the country is not only harmful to national interests, but also frightening because it can spread false knowledge to the public and weaken the public’s perception of national integrity.
After a lot of work, I decided to present you with a perfect Map of China in Python.
Here is another correct way to map our country!!
To do this experiment you need the following conditions:
- Python 3.6 +
Install Pyecharts first, the default installation is the latest version, the latest version of Pyecharts requires python3.6 or later, after installing Python and adding PIP to the environment variable:
Windows open CMD and enter:
pip install pyecharts
Copy the code
Run the terminal command on macOS.
pip install pyecharts
Copy the code
Pyecharts is Successfully installed. Here we break the tutorial into three parts:
-
- Draw a Map of China using Python with Pyecharts
-
- Mark the missing parts of the map from Dear Beloved on a map of China
-
- Save the map as a picture
1. For the first step, we can try to draw a simple China using PyechartsThe map:
In any new folder, create map.py and write the following code:
from pyecharts.charts import Geo
# ->Geo is a function annotation that indicates that the function returns a Geo object
def geo_effectscatter() -> Geo:
The following is the chain call method declaration object
c = (
Geo()
# Add bottom map
.add_schema(maptype="china"))return c
# generate object
c = geo_effectscatter()
# Render map
c.render()
Copy the code
run
Windows: Open CMD, CD, enter the current folder, and enter the following command
python map.py
Copy the code
In macOS, run the terminal command, CD, and enter the current folder
When this is done, you should see a render. HTML generated in the current directory. Open this file in your browser and you can see our map.
def geo_effectscatter() -> Geo:
Copy the code
Unlike normal function declarations, it is followed by a function annotation (->Geo) indicating that the return value of the function is a Geo object.
The second is:
c = (
Geo()
.add_schema(maptype="china")
# Add bottom map
)
Copy the code
This is a Python chain call that has the same effect as
c = Geo()
c.add_schema(maptype="china")
Copy the code
The generated Map of China is as follows:
2. Step two: ChinaThe mapIt’s labeled “Dear Beloved.The mapThe missing part
Create a new file map_mark.py in the current directory
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType
def geo_effectscatter() -> Geo:
# initialize map parameters page_title: page title, theme: canvas theme (see Echarts website for theme list)
InitOpts = opts.InitOpts(page_title="Map of China", theme="light")
c = (
The parameters will be initialized when the object is declared
Geo(InitOpts)
# Add bottom map
.add_schema(
maptype="china")
# Added area points (Aksai Chin and South Tib)
.add_coordinate(
name='Aksai Chin', longitude=78.928266, latitude=35.115117). Add_coordinate (name='South Tibet',, latitude longitude = 93.128902 = 27.616436)# Highlight what is missing from the map in the play
.add(
"The missing part of the map from Dear Beloved."The [['hainan', 100], ['Taiwan', 100], ['Aksai Chin', 100], ['South Tibet', 100]],
type_=ChartType.EFFECT_SCATTER,
)
Formatter ={b} formatter={b}
# https://pyecharts.org/#/zh-cn/series_options).
.set_series_opts(label_opts=opts.LabelOpts(is_show=True, formatter="{b}", font_size=14))
# set the map name, the upper left corner
.set_global_opts(title_opts=opts.TitleOpts(title="Map of China")))return c
# generate object
c = geo_effectscatter()
# Render map
c.render()
Copy the code
Run the same as the first step, change the file name, remember not to include Chinese in the path. After running, render. HTML will be generated in the current directory, and the map can be seen when opened by the browser.
The effect is as follows:
3. Step three, willThe mapSave as a picture
There are two ways to generate images in Pyecharts. One is selenium, and the other is Phantomjs. Phantomjs is recommended for this tutorial.
First, we need to install snapshot-Phantomjs. Just like installing echarts, open CMD (Windows) or Terminal (macOS) and enter the following command:
pip install snapshot-phantomjs
Copy the code
Then, we need to go to phantomJS ‘official website and download phantomJS: version 2.1.1 download link
Phantomjs installation is explained here using Windows as an example:
After unpacking the downloaded zip package, we get Phantomjs-2.1.1-Windows. The phantomjs.exe in the bin folder is the program we need to add it to the environment variable.
Windows 10 searches for environment variables in the lower left corner.
For windows7, you need to open environment variables as shown below (windows10 can also do this).
Add our phantomjs.exe path to the system variable after path, for example, mine is add:
C: UsersCkendDownloadsphantomjs 2.1.1 – windowsphantomjs – 2.1.1 – windowsbin
If you’re running Windows 7, use it. Separate it from the previous path.
; C: UsersCkendDownloadsphantomjs 2.1.1 – windowsphantomjs – 2.1.1 – windowsbin
Now you can generate images and introduce the two packages we just introduced at the beginning of the code
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot
Copy the code
Finally, we call the function that generates the image
# generate images
make_snapshot(snapshot, c.render(), "map.png")
Copy the code
The complete code is as follows:
from pyecharts.render import make_snapshot from snapshot_phantomjs import snapshot from pyecharts import options as opts from pyecharts.charts import Geo from pyecharts.globals import ChartType def geo_effectscatter() -> Geo:# initialize map parameters page_title: page title, theme: canvas theme (see Echarts website for theme list)
InitOpts = opts.InitOpts(page_title="Map of China", theme="light")
c = (
The parameters will be initialized when the object is declared
Geo(InitOpts)
# Add bottom map
.add_schema(
maptype="china")
# Added area points (Aksai Chin and South Tib)
.add_coordinate(
name='Aksai Chin', longitude=78.928266, latitude=35.115117). Add_coordinate (name='South Tibet',, latitude longitude = 93.128902 = 27.616436)# Highlight what is missing from the map in the play
.add(
"The missing part of the map from Dear Beloved."The [['hainan', 100], ['Taiwan', 100], ['Aksai Chin', 100], ['South Tibet', 100]],
type_=ChartType.EFFECT_SCATTER,
)
Formatter ={b} formatter={b}
# https://pyecharts.org/#/zh-cn/series_options).
.set_series_opts(label_opts=opts.LabelOpts(is_show=True, formatter="{b}", font_size=14))
# set the map name, the upper left corner
.set_global_opts(title_opts=opts.TitleOpts(title="Map of China")))return c
# generate object
c = geo_effectscatter()
# Render map
c.render()
# generate images
make_snapshot(snapshot, c.render(), "map_marked.png")
Copy the code
The generated Map of China is a high definition map, up to 7M oh!
If you want, you can even change the color of the map, the color of the punctuation, the color of each area, and you can even break it down to the provincial and municipal level. See the official documentation for details:
Pyecharts: pyecharts.org
If you want to see more custom methods, read:
Echarts original features: echarts.baidu.com
This is the end of our article! If you enjoyed this article, stay tuned for Python’s handy guide! Remember our official website: pythondict.com.