Geographic information visualization-Introduction to matplotlib basemap in Python

Source: Internet
Author: User

Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!

 

In the process of data visualization, we often need to display the data on the map based on the collected geographical location. For example, we want to map cities, aircraft routes, military bases, and so on. Generally, a Geographic Information System has such a function. Today we will discuss how to implement it in Python and use the free toolkit.

 

Matplotlib is a commonly used data drawing package in Python. It is based on numpy (numpy is a python package used for Array Operations ). Matplotlib has a powerful data rendering function that allows you to easily draw various statistical graphs, such as scatter plots, line charts, and pie charts. The basemap in matplot allows us to draw a map and continue to draw data on the map.

(Matplotlib is often used in combination with numpy and scipy for scientific research. They are strong opponents of Matlab, which is quite understandable, because a set of MATLAB requires thousands of pieces, while the python tool is free .)

Our goal today is to use the above tools to map the population of major cities in Asia. As shown in, the population is represented by the size of an orange circle:

The data is as follows (I sorted it out from Wikipedia and you can use it as needed). I save the data in the file major_city:

 
Shanghai 23019148 31.23n 121.47e chinamumbai 12478447 18.96n 72.82e indiakarachi 13050000 running 67.01e running 16314838 28.67n 77.21e indiamanila 11855975 14.62n 120.97e running 23616000 running 126.99e Korea (South) jakarta 28019545 6.18 s 106.83e indonesiatokyo 35682460 35.67n 139.77e Japan Peking 19612368 39.91n 116.39e China

The first column is the city name, the second column is the population, the third column is the latitude and longitude, And the last column is the country.

 

Below is my pythonCode:

View code

 #  Written by vamei, http://www.cnblogs.com/vamei/  #  Feel free to use or modify this script.  From Mpl_toolkits.basemap Import  Basemap  Import  Matplotlib. pyplot as PLT  Import Numpy as NP  #  ========================================================== ====# Read data Names = [] POPs = [] Lats = [] Lons = [] Countries = []  For Line In File ( "  ../Data/major_city  "  ): Info = Line. Split () names. append (info [0]) POPs. append (float (info [ 1 ]) Lat = Float (info [2] [:-1 ])  If Info [2] [-1] = '  S  ' : Lat =- Lat lats. append (LAT) lon = Float (info [3] [:-1 ])  If Info [3] [-1] = '  W  ' : Lon =-lon + 360.0 Lons. append (Lon) Country = Info [4 ] Countries. append (country)  #  ========================================================== ====  #  Set up map projection  #  Use low resolution coastlines. Map = basemap (projection = '  Ortho  ' , Lat_0 = 35, lon_0 = 120, resolution = '  L '  )  #  Draw coastlines, country boundaries, fill continents. Map. drawcoastlines (linewidth = 0.25 ) Map. drawcountries (linewidth = 0.25 )  #  Draw the edge of the map projection region (the projection limb) Map. drawmapboundary (fill_color = '  #689cd2  '  )  # Draw LAT/lon grid lines every 30 degrees. Map. drawmeridians (NP. arange (0,360, 30 ) Map. drawparallels (NP. arange ( -90, 90, 30 ))  #  Fill continent wit a different color Map. fillcontinents (color = '  # Bf9e30  ' , Lake_color = '  #689cd2  ' , Zorder = 0)  # Compute native map projection coordinates of LAT/lon grid. X, Y = Map (lons, Lats) max_pop = Max (POPs)  #  Plot each city in a loop.  #  Set some parameters Size_factor = 80.0 Y_offset = 15.0 Rotation = 30 For I, J, K, name In  Zip (X, Y, Pops, names): Size = Size_factor * k/Max_pop CS = Map. Scatter (I, j, S = size, marker = '  O  ' , Color = '  # Ff5600  '  ) PLT. Text (I, j + Y_offset, name, rotation = rotation, fontsize = 10 ) PLT. Title (  '  Major cities in Asia & Population  '  ) PLT. Show () 

ProgramThere are two parts: the first part is to read and process data from the file, and the second part is to plot with basemap.

Map = basemap (projection ='Ortho', Lat_0 = 35, lon_0 = 120, resolution ='L')

Sets the map projection method. There are many ways to choose from spherical earth surface projection to a flat map, and the results are also very different.

Our longitude and latitude pass

X, Y =Map (lons, Lats)

The statement is converted to the position on the image and

CS= Map. Scatter (I, j, S = size, marker ='O', Color ='# Ff5600')

You can draw a scatter chart on the map.

 

The required software packages include:

Numpy, matplotlib, mpl_toolkits

You can Google and find related documents.

 

You can use Pip to download and install various packages.

In ubuntu repository, you can find the python-matplotlib package.

ReferencePython tips

 

 

In addition, you can also download EPD python. It integrates Python and all software packages. (EPD python is a commercial version, but it is free for academic users .)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.