Python uses Matplotlib toolkit to map the world

Time:2021-1-15

The text and pictures of this article are from the Internet, only for learning and communication, and do not have any commercial use. If you have any questions, please contact us in time.

The following article is from Python practical dictionary, written by ckend

Introduction to Python data analysis

https://www.bilibili.com/video/BV18f4y1i7q9/

 

Python uses Matplotlib toolkit to map the world

 

Python is so popular because it can be used not only in the field of science and technology, but also as a research tool for many other disciplines. Mapping is one of its functions.

Today we usematplotlibMPL, one of the Toolkits_ Toolkit is a simple visualization tool for drawing world maps. If you want to draw more complex maps, you can consider using Google Maps API, but this is not within the scope of our discussion today.

1. Installation

In order to successfully carry out this project, you need to install the following dependencies first, and enter the following command in CMD or terminal:

pip install numpy
pip install matplotlib

 

To use MPL_ Toolkit: simply installing Matplotlib is not enough. We need to install basemap separately. If you have installed anaconda, this step is very easy. Just enter the following command to install

conda install basemap

 

If not, a little bit of trouble:

1. Install GEOS:

pip install geos

 

2. Download basemap according to your Python version

http://www.lfd.uci.edu/~gohlke/pythonlibs/#basemap

 

Note that the number after CP is the python version. (press Ctrl + F on the page and enter basemap to locate quickly)
3. Enter the directory of the file under CMD and run

pip install basemap‑1.2.1‑cp37‑cp37m‑win_amd64.whl

 

2. Simple map

Let’s begin to draw an earth with the center pointing to China:

Python uses Matplotlib toolkit to map the world

 

The focus here is on basemap, which specifies the center you want to place.

Python uses Matplotlib toolkit to map the world

 

The effect is not bad. Not only that, it is not only an image, but also a full-featured matplot canvas. That means you can draw lines on it! Let’s zoom in on the map, enter China, and mark the location of Shenzhen:

Python uses Matplotlib toolkit to map the world

 

Python uses Matplotlib toolkit to map the world

 

Don’t use the blue background. It’s not very clear. Let’s change it to the relief type

Python uses Matplotlib toolkit to map the world

 

We can clearly see the geographical features of mountains and hills. You can also make lines for certain cities or draw some areas between longitude and latitude according to your needs. Don’t forget, this is the maplotlib edit canvas.

3. World map

Next, we will expand the above world map into a plane figure with longitude and latitude lines.

Python uses Matplotlib toolkit to map the world

 

Python uses Matplotlib toolkit to map the world

 

Well, it tastes like that. You can even print it out for children to learn geography.

But if he wants to study geography, does the whole world seem a little big? Let’s let him learn the location of world famous scenic spots first?

Python uses Matplotlib toolkit to map the world

 

Zoom in:

Python uses Matplotlib toolkit to map the world

 

In this way, you just need to add the latitude and longitude of a place to the locations to display it on the map. You can also customize the line between two places, or focus on enlarging a certain area. All in all, what you want to do is basically based on Matplotlib.