(data science learning notes 98) pure Python draws a ridge map full of artistic feeling

Freery 2020-11-15 19:42:08
data science learning notes pure

This article sample code and attachment have been uploaded to my Github Warehouse https://github.com/CNFeffery/DataScienceStudyNotes

1 brief introduction

The following picture may have been seen by many readers , This is the British rock band Joy Division stay 1979 His first studio album released in Unknown Pleasures Cover , By the artist Peter Saville Based on the data map of radio pulsar signal , It has become a symbol of popular culture .

chart 1

Similar to 1 Style , There is also a kind of Ridge map , Based on recording the elevation of the earth's surface Elevation data , We can use a horizontal curve based on the altitude of the actual location , To give a more artistic expression to the terrain of a certain area .

chart 2

And today's article , We'll come together based on Python, Match the color and font selection , Use short code , You can create art poster level Ridge map .

2 be based on ridge_map Map of the ridge

We mainly use matplotlib And ridge_map To complete the creation of a ridge map , Use pip install ridge_map Finish right ridge_map After the installation of , Let's start with a very simple example :

from ridge_map import RidgeMap
import matplotlib.font_manager as fm
import matplotlib.pyplot as plt
# Register fonts from local font files
font_prop = fm.FontProperties(fname="fonts/UncialAntiqua-Regular.ttf")
# Based on the lower left corner of the incoming area 、 Latitude and longitude of the upper right corner
# To get the original elevation data and draw a ridge map
# If you have “ Special Internet skills ”, The waiting time for this step will be short
plt.savefig(' chart 3.png')
chart 3

This is it. ridge_map The basic model for mapping ridges , utilize matplotlib.font_manager Register the font to use , And then the drawing area bbox Information and font attributes are passed in RidgeMap() Then call plot_map() Method to draw .

But if you want to make a more customized ridge map like this , You need to know more about it :

chart 4

Let's introduce it by parts :

2.1 Data preparation

We all use it RidgeMap Accept bbox Parameters determine the region range , The format is ( The longitude of the lower left corner , Lower left latitude , The longitude of the upper right corner , Latitude in the upper right corner ), It is based on elevation data from NASA Of SRTM Data sets , A resolution of 1 Arcseconds ( about 30 rice ), It is suitable for north latitude 60° To the south latitude 60° Area between .

ridge_map The complete process of data preparation is as follows , among get_elevation_data Methodical num_lines The parameter is used to control the number of horizontal lines corresponding to the returned data , The more detailed , The default is 80;viewpoint The parameter is used to determine the direction the compass is pointing to , The default is south

# initialization
rm = RidgeMap(bbox=(-156.250305,18.890695,-154.714966,20.275080),
# Get elevation data online
values = rm.get_elevation_data(num_lines=200, viewpoint='north')

And what you get is values In fact, it is the two-dimensional change of altitude of each horizontal line within the range numpy Array :

chart 5

2.2 The data processing

Based on the data obtained in the first step , We can use RidgeMap Of preprocess Method to process , In order to achieve some things such as changing the height mapping ratio 、 Low lying area screening function , The main parameters are as follows :

values: Pass in the 2D array data obtained in the previous step

water_ntile: Floating point numbers , The scope should be in 0 To 100 Between , As a threshold for data deletion , That is, the height is lower than the overall water_ntile% Quantile data would be treated as water , So that... Is not shown in the image

vertical_ratio: Used to set the multiple of the original height value to be enlarged in the drawing , The bigger, the more exaggerated

values = rm.preprocess(values=values,
rm.plot_map(values, label="Hawai'i")
plt.savefig(' chart 6.png')
chart 6

2.3 The plot

After finishing the above data processing , We can call plot_map() Method to draw the ridge map , The main parameters are as follows :

values: Processed before it was introduced values

label: Used to set the content of text label superimposed on the image

label_x:0-1 The floating point number between , Used to determine the scale of the lower left corner of the text label relative to the drawing area x coordinate

label_y: similar label_x, adjustment y coordinate

label_verticalalignment: Adjust the vertical alignment of text labels , The default is 'bottom'

label_size: Control text label font size , The default is 40

line_color: Set the color of the line , The default is 'black', When it comes to matplotlib Medium colormap Object, the color mapping mode is turned on

kind: Color mapping strategy settings ,'gradient' It has nothing to do with height , Color gradient in the vertical direction of the picture ,'elevation' Then the color mapping is bound to the height

linewidth: Set the line thickness , The default is 2

background_color: Set image background color

The other parameters are very easy to understand , Here's the difference kind The result is different under parameter :

  • kind='gradient'
rm.plot_map(values, label="Hawai'i", kind='gradient', line_color=plt.get_cmap('Reds'))
chart 7

You can see in the gradient In mode , The color of the lines on the whole image follows from top to bottom colormap Make a gradient .

  • kind='elevation'
rm.plot_map(values, label="Hawai'i", kind='elevation', line_color=plt.get_cmap('Reds'))
plt.savefig(' chart 8.png')
chart 8

As you can see, our line color is based on height information .

2.4 combination matplotlib

because ridge_map Based on matplotlib, So we can be like geopandas Drawing like that , Calling plot_map Time direction ax Parameters pass in the existing Axes object , To combine different types of images , Like this simple example :

chart 9

stay get To ridge_map After the interesting use of , We can draw anywhere within the legal scope , For example, the following part of Chongqing's downtown area is drawn :

font_prop = fm.FontProperties(fname="fonts/LongCang-Regular.ttf")
rm = RidgeMap(bbox=(106.360758,29.385385,106.74734,29.676339),
values = rm.get_elevation_data(num_lines=250, viewpoint='south')
values = rm.preprocess(values=values,
rm.plot_map(values, label="",
plt.savefig(' chart 10.png')
chart 10

The above is the whole content of this paper , You can create creative ridge maps . Welcome to discuss with me in the comments section ~


  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database