(materials science learning letters 98) pure Python draws a ridge map full of artistic feeling

itread01 2020-11-16 00:31:44
materials science learning letters pure


> This article sample code and attachments have been uploaded to my `Github` Warehouse [https://github.com/CNFeffery/DataScienceStudyNotes](https://github.com/CNFeffery/DataScienceStudyNotes) # 1 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 signals , It has become a symbol of popular culture . Similar diagram 1 Style of , There is also a kind of ** Ridge map **, Based on recording the elevation of the earth's surface ** Elevation data **, We can use the horizontal curve based on the altitude of the actual location , To give a more artistic expression to the terrain of a certain area . And today's article , We will come together on the basis of `Python`, With the choice of color and font , Use short code , You can create art poster level ** Ridge map **. # 2 Based on ridge_map Map the ridge of the mountain 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 : ```Python 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 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 ( RidgeMap(bbox=(-156.250305,18.890695,-154.714966,20.275080), font=font_prop) .plot_map(label="Hawai'i") ) plt.savefig(' Graph 3.png') ``` This is `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()` Call after `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 : Let's introduce it by parts : ## 2.1 Data preparation We use `RidgeMap` Accept `bbox` The argument determines the range of the region , 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](https://www2.jpl.nasa.gov/srtm/) Data sets , The resolution is 1 Arcseconds ( About 30 rice ), It is suitable for north latitude 60° To the south latitude 60° The area between . `ridge_map` The complete process of data preparation is as follows , among `get_elevation_data` Methodical `num_lines` Arguments are used to control the number of horizontal lines corresponding to the returned data , The more detailed , The default is 80;`viewpoint` The argument is used to determine the direction of the compass , The default is `south`: ```Python # initialization rm = RidgeMap(bbox=(-156.250305,18.890695,-154.714966,20.275080), font=font_prop) # 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 : ## 2.2 Data processing Based on the information obtained in the first step , We can use `RidgeMap` Of `preprocess` Method to process , In order to achieve some such as changing the height of the map ratio 、 Low lying area screening function , The main arguments 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 that of the whole **water_ntile%** Quantile data will be treated as water bodies , So that... Is not shown in the image > > **vertical_ratio**: Used to set the multiple of the original height value to be expanded in the drawing , The bigger, the more exaggerated ```Python values = rm.preprocess(values=values, water_ntile=10, vertical_ratio=240) rm.plot_map(values, label="Hawai'i") plt.savefig(' Graph 6.png') ``` ## 2.3 Drawing images After finishing the above data processing , We can call `plot_map()` Method to draw the ridge map , The main arguments are as follows : > **values**: Processed before it was introduced values > > **label**: Used to set the content of the text label superimposed on the image > > **label_x**:0-1 Floating point number between , Used to determine the ratio of the lower left corner of the text label to the drawing area x coordinates > > **label_y**: Similar **label_x**, Adjust y coordinates > > **label_verticalalignment**: Adjust the vertical alignment of text labels , The default is `'bottom'` > > **label_size**: Control the font size of text labels , The default is 40 > > **line_color**: Set the color of the line , The default is `'black'`, When it comes to `matplotlib` Medium `colormap` The color map mode is turned on when the object is selected > > **kind**: Set color mapping strategy ,`'gradient'` It has nothing to do with height , Color gradient in the vertical direction of the picture ,`'elevation'` Then the color mapping is tied to the height > > **linewidth**: Set line thickness , The default is 2 > > **background_color**: Set the image background color The other arguments are very easy to understand , Here's to highlight the difference `kind` The difference between the results under arguments : - **kind='gradient'** ```Python rm.plot_map(values, label="Hawai'i", kind='gradient', line_color=plt.get_cmap('Reds')) ``` You can see in the `gradient` In mode , The line colors on the whole image follow from top to bottom `colormap` Make a gradient . - **kind='elevation'** ```Python rm.plot_map(values, label="Hawai'i", kind='elevation', line_color=plt.get_cmap('Reds')) plt.savefig(' Graph 8.png') ``` We can see that our line color is based on the height information . ## 2.4 Combine matplotlib Because `ridge_map` Based on `matplotlib`, So we can be like `geopandas` Draw like that , Calling `plot_map` In time `ax` An argument is passed into an existing `Axes` thing , To combine different types of images , Like this simple example : 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 : ```Python font_prop = fm.FontProperties(fname="fonts/LongCang-Regular.ttf") rm = RidgeMap(bbox=(106.360758,29.385385,106.74734,29.676339), font=font_prop) values = rm.get_elevation_data(num_lines=250, viewpoint='south') values = rm.preprocess(values=values, water_ntile=5, vertical_ratio=90) rm.plot_map(values, label="", kind='elevation', line_color=plt.get_cmap('plasma'), label_size=100) plt.savefig(' Graph 10.png') ``` --- The above is the whole content of this article , You can create creative ridge maps . Welcome to join me in the comments section
版权声明
本文为[itread01]所创,转载请带上原文链接,感谢

  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