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 .

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 .

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
(
RidgeMap(bbox=(-156.250305,18.890695,-154.714966,20.275080),
font=font_prop)
.plot_map(label="Hawai'i")
)
plt.savefig(' chart 3.png')

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 :

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),
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 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,
water_ntile=10,
vertical_ratio=240)
rm.plot_map(values, label="Hawai'i")
plt.savefig(' chart 6.png')

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 tomatplotlib
Mediumcolormap
Object, the color mapping mode is turned onkind: 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 heightlinewidth: 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'))

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')

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 :

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),
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(' chart 10.png')

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