当前位置:   article > 正文

基于Python的地图绘制教程(附源码)_python批量制作地图

python批量制作地图

本文将介绍通过Python绘制地形图的方法,所需第三方Python相关模块包括 rasterio、geopandas、cartopy 等,可通过 pip 等方式安装。

1 示例代码

1.1 导入相关模块



<table width="643">
<tbody style="outline: 0px;visibility: visible;">
<tr style="outline: 0px;visibility: visible;">
<td data-line-number="1" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);visibility: visible;"><br></td>
<td data-line-number="1" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;visibility: visible;">
<span style="outline: 0px;color: rgb(86, 156, 214);visibility: visible;">
import
</span> rasterio
</td>
</tr><tr style="outline: 0px;visibility: visible;">
<td data-line-number="2" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);visibility: visible;"><br>
</td><td data-line-number="2" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;visibility: visible;">
<span style="outline: 0px;color: rgb(86, 156, 214);visibility: visible;">import
</span> geopandas <span style="outline: 0px;color: rgb(86, 156, 214);visibility: visible;">as</span> gpd</td></tr>
<tr style="outline: 0px;visibility: visible;">
<td data-line-number="3" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);visibility: visible;"><br>
</td><td data-line-number="3" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;visibility: visible;">
<span style="outline: 0px;color: rgb(86, 156, 214);visibility: visible;">import
</span> numpy <span style="outline: 0px;color: rgb(86, 156, 214);visibility: visible;">as</span> np</td></tr><tr style="outline: 0px;visibility: visible;"><td data-line-number="4" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);visibility: visible;"><br>
</td><td data-line-number="4" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;visibility: visible;">
<span style="outline: 0px;color: rgb(86, 156, 214);visibility: visible;">import</span> cartopy.crs <span style="outline: 0px;color: rgb(86, 156, 214);visibility: visible;">as
</span> ccrs</td></tr><tr style="outline: 0px;visibility: visible;">
<td data-line-number="5" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);visibility: visible;"><br>
</td><td data-line-number="5" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;visibility: visible;"><span style="outline: 0px;color: rgb(86, 156, 214);visibility: visible;">import</span> matplotlib.pyplot <span style="outline: 0px;color: rgb(86, 156, 214);visibility: visible;">as</span> plt</td></tr><tr style="outline: 0px;visibility: visible;"><td data-line-number="6" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);visibility: visible;">
<br></td><td data-line-number="6" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;visibility: visible;">
<span style="outline: 0px;color: rgb(86, 156, 214);visibility: visible;">from</span> matplotlib.colors <span style="outline: 0px;color: rgb(86, 156, 214);visibility: visible;">
import</span> LinearSegmentedColormap
</td>
</tr>
</tbody>
</table>


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

1.2 设置地图字体及字号


<table width="643"><tbody style="outline: 0px;visibility: visible;">
<tr style="outline: 0px;visibility: visible;">
<td data-line-number="1" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);visibility: visible;"><br></td>
<td data-line-number="1" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;visibility: visible;">plt.rcParams[<span style="outline: 0px;color: rgb(214, 157, 133);visibility: visible;">"font.family"</span>] = <span style="outline: 0px;color: rgb(214, 157, 133);visibility: visible;">"Times New Roman"</span></td></tr>
<tr style="outline: 0px;visibility: visible;"><td data-line-number="2" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);visibility: visible;"><br></td>
<td data-line-number="2" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;visibility: visible;">plt.rcParams[<span style="outline: 0px;color: rgb(214, 157, 133);visibility: visible;">"font.size"</span>] = <span style="outline: 0px;color: rgb(184, 215, 163);visibility: visible;">14</span></td></tr></tbody></table>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

1.3 绘制地图



<table width="768" style="width: 768px;"><tbody style="outline: 0px;visibility: visible;"><tr style="outline: 0px;visibility: visible;"><td data-line-number="1" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);visibility: visible;"><br></td><td data-line-number="1" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;visibility: visible;"><span style="outline: 0px;color: rgb(87, 166, 74);font-style: italic;visibility: visible;"> 设置投影:墨卡托投影</span></td></tr><tr style="outline: 0px;visibility: visible;"><td data-line-number="2" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);visibility: visible;"><br></td><td data-line-number="2" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;visibility: visible;"><span style="outline: 0px;color: rgb(87, 166, 74);font-style: italic;visibility: visible;">cartopy 投影说明:https://www.cnblogs.com/youxiaogang/p/14247184.html</span></td></tr><tr style="outline: 0px;visibility: visible;"><td data-line-number="3" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);visibility: visible;"><br></td><td data-line-number="3" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;visibility: visible;">projection = ccrs.Mercator()</td></tr><tr style="outline: 0px;visibility: visible;"><td data-line-number="4" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);visibility: visible;"><br></td><td data-line-number="4" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;visibility: visible;"><br></td></tr><tr style="outline: 0px;visibility: visible;"><td data-line-number="5" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);visibility: visible;"><br></td><td data-line-number="5" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;visibility: visible;"><span style="outline: 0px;color: rgb(87, 166, 74);font-style: italic;visibility: visible;"> 绘制地图</span></td></tr><tr style="outline: 0px;visibility: visible;"><td data-line-number="6" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);visibility: visible;"><br></td><td data-line-number="6" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;visibility: visible;"><span style="outline: 0px;visibility: visible;">fig, ax = plt.subplots(figsize=(</span><span style="outline: 0px;color: rgb(184, 215, 163);visibility: visible;">20</span><span style="outline: 0px;visibility: visible;">, </span><span style="outline: 0px;color: rgb(184, 215, 163);visibility: visible;">10</span><span style="outline: 0px;visibility: visible;">), subplot_kw={</span><span style="outline: 0px;color: rgb(214, 157, 133);visibility: visible;">'projection'</span><span style="outline: 0px;visibility: visible;">: projection})</span></td></tr><tr style="outline: 0px;"><td data-line-number="7" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="7" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;"><br></td></tr><tr style="outline: 0px;"><td data-line-number="8" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="8" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;"><span style="outline: 0px;color: rgb(87, 166, 74);font-style: italic;">
# 设置地图范围(数值参数为对应投影下的范围坐标)
</span></td></tr><tr style="outline: 0px;"><td data-line-number="9" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="9" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;"><span style="outline: 0px;">ax.set_extent([</span><span style="outline: 0px;color: rgb(184, 215, 163);">13530000</span><span style="outline: 0px;">, </span><span style="outline: 0px;color: rgb(184, 215, 163);">14630000</span><span style="outline: 0px;">, </span><span style="outline: 0px;color: rgb(184, 215, 163);">4960000</span><span style="outline: 0px;">, </span><span style="outline: 0px;color: rgb(184, 215, 163);">5850000</span><span style="outline: 0px;">], crs=projection)</span></td></tr><tr style="outline: 0px;"><td data-line-number="10" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="10" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;"><br></td></tr><tr style="outline: 0px;"><td data-line-number="11" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="11" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;"><span style="outline: 0px;color: rgb(87, 166, 74);font-style: italic;"><span style="outline: 0px;">
# 读取
</span>矢量文件</span></td></tr><tr style="outline: 0px;"><td data-line-number="12" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="12" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">shp = gpd.read_file(<span style="outline: 0px;color: rgb(214, 157, 133);">"Data/Jilin_Mercator.shp"</span>)</td></tr><tr style="outline: 0px;"><td data-line-number="13" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="13" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">shp.plot(ax=ax, transform=projection, edgecolor=<span style="outline: 0px;color: rgb(214, 157, 133);">"black"</span>, linewidth=<span style="outline: 0px;color: rgb(184, 215, 163);">1</span>, facecolor=<span style="outline: 0px;color: rgb(214, 157, 133);">"none"</span>)</td></tr><tr style="outline: 0px;"><td data-line-number="14" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="14" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;"><br></td></tr><tr style="outline: 0px;"><td data-line-number="15" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="15" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;"><span style="outline: 0px;color: rgb(87, 166, 74);font-style: italic;">
# 创建自定义颜色映射
</span></td></tr><tr style="outline: 0px;"><td data-line-number="16" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="16" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">colors = [<span style="outline: 0px;color: rgb(214, 157, 133);">"#369121"</span>, <span style="outline: 0px;color: rgb(214, 157, 133);">"#95C769"</span>, <span style="outline: 0px;color: rgb(214, 157, 133);">"#FFFFBF"</span>, <span style="outline: 0px;color: rgb(214, 157, 133);">"#E6865A"</span>, <span style="outline: 0px;color: rgb(214, 157, 133);">"#D14E30"</span>, <span style="outline: 0px;color: rgb(214, 157, 133);">"#BA1414"</span>]</td></tr><tr style="outline: 0px;"><td data-line-number="17" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="17" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">n_bins = <span style="outline: 0px;color: rgb(184, 215, 163);">100</span>  <span style="outline: 0px;color: rgb(87, 166, 74);font-style: italic;">
# 定义色带的颜色数量
</span></td></tr><tr style="outline: 0px;"><td data-line-number="18" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="18" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">cmap_name = <span style="outline: 0px;color: rgb(214, 157, 133);">"green_brown"</span></td></tr><tr style="outline: 0px;"><td data-line-number="19" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="19" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">cm = LinearSegmentedColormap.from_list(cmap_name, colors, N=n_bins)</td></tr><tr style="outline: 0px;"><td data-line-number="20" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="20" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;"><br></td></tr><tr style="outline: 0px;"><td data-line-number="21" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="21" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;"><span style="outline: 0px;color: rgb(87, 166, 74);font-style: italic;">
# 读取地形栅格数据
</span></td></tr><tr style="outline: 0px;"><td data-line-number="22" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="22" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">dataset = rasterio.<span style="outline: 0px;color: rgb(78, 201, 176);">open</span>(<span style="outline: 0px;color: rgb(214, 157, 133);">"Data/DEM_Jilin_Mercator.tif"</span>)</td></tr><tr style="outline: 0px;"><td data-line-number="23" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="23" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">data = dataset.read(<span style="outline: 0px;color: rgb(184, 215, 163);">1</span>)  <span style="outline: 0px;color: rgb(87, 166, 74);font-style: italic;">
# 读取第一个波段的数据
</span></td></tr><tr style="outline: 0px;"><td data-line-number="24" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="24" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">nodata_value = dataset.nodata   <span style="outline: 0px;color: rgb(87, 166, 74);font-style: italic;">
# 获取NoData值
</span></td></tr><tr style="outline: 0px;"><td data-line-number="25" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="25" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;"><span style="outline: 0px;color: rgb(87, 166, 74);font-style: italic;">
# 创建掩膜,去除NoData区域
</span></td></tr><tr style="outline: 0px;"><td data-line-number="26" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="26" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">data = np.ma.masked_where(data == nodata_value, data)</td></tr><tr style="outline: 0px;"><td data-line-number="27" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="27" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;"><br></td></tr><tr style="outline: 0px;"><td data-line-number="28" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="28" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;"><span style="outline: 0px;color: rgb(87, 166, 74);font-style: italic;">
# 将地形栅格添加到地图中
</span></td></tr><tr style="outline: 0px;"><td data-line-number="29" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="29" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">extent = [dataset.bounds.left, dataset.bounds.right,</td></tr><tr style="outline: 0px;"><td data-line-number="30" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="30" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">dataset.bounds.bottom, dataset.bounds.top]</td></tr><tr style="outline: 0px;"><td data-line-number="31" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="31" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">im = ax.imshow(data, origin=<span style="outline: 0px;color: rgb(214, 157, 133);">"upper"</span>, extent=extent,</td></tr><tr style="outline: 0px;"><td data-line-number="32" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="32" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">transform=projection, cmap=cm)</td></tr><tr style="outline: 0px;"><td data-line-number="33" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="33" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;"><br></td></tr><tr style="outline: 0px;"><td data-line-number="34" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="34" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;"><span style="outline: 0px;color: rgb(87, 166, 74);font-style: italic;">
# 绘制网格线并添加标签
</span></td></tr><tr style="outline: 0px;"><td data-line-number="35" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="35" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">gl = ax.gridlines(draw_labels=<span style="outline: 0px;color: rgb(86, 156, 214);">True</span>, linestyle=<span style="outline: 0px;color: rgb(214, 157, 133);">"--"</span>, color=<span style="outline: 0px;color: rgb(214, 157, 133);">"#4F4F4F"</span>)</td></tr><tr style="outline: 0px;"><td data-line-number="36" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="36" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">gl.xlocator = plt.FixedLocator(<span style="outline: 0px;color: rgb(78, 201, 176);">range</span>(<span style="outline: 0px;color: rgb(184, 215, 163);">120</span>, <span style="outline: 0px;color: rgb(184, 215, 163);">135</span>, <span style="outline: 0px;color: rgb(184, 215, 163);">3</span>))</td></tr><tr style="outline: 0px;"><td data-line-number="37" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="37" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">gl.ylocator = plt.FixedLocator(<span style="outline: 0px;color: rgb(78, 201, 176);">range</span>(<span style="outline: 0px;color: rgb(184, 215, 163);">40</span>, <span style="outline: 0px;color: rgb(184, 215, 163);">50</span>, <span style="outline: 0px;color: rgb(184, 215, 163);">2</span>))</td></tr><tr style="outline: 0px;"><td data-line-number="38" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="38" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;"><br></td></tr><tr style="outline: 0px;"><td data-line-number="39" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="39" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;"><span style="outline: 0px;color: rgb(87, 166, 74);font-style: italic;">
# 添加色带,设置色带的缩放比例为 0.4,主图和色带之间的间距为 0.1
</span></td></tr><tr style="outline: 0px;"><td data-line-number="40" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="40" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">cbar = plt.colorbar(im, ax=ax, orientation=<span style="outline: 0px;color: rgb(214, 157, 133);">"horizontal"</span>, shrink=<span style="outline: 0px;color: rgb(184, 215, 163);">0.4</span>, pad=<span style="outline: 0px;color: rgb(184, 215, 163);">0.1</span>)</td></tr><tr style="outline: 0px;"><td data-line-number="41" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="41" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">cbar.set_label(<span style="outline: 0px;color: rgb(214, 157, 133);">"Elevation (m)"</span>, labelpad=<span style="outline: 0px;color: rgb(184, 215, 163);">10</span>) <span style="outline: 0px;color: rgb(87, 166, 74);font-style: italic;">
# 设置色带标签与色带的距离为 10 点
</span></td></tr><tr style="outline: 0px;"><td data-line-number="42" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="42" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">cbar.ax.xaxis.set_label_position(<span style="outline: 0px;color: rgb(214, 157, 133);">"top"</span>)  <span style="outline: 0px;color: rgb(87, 166, 74);font-style: italic;">
# 设置色带标签位置
</span></td></tr><tr style="outline: 0px;"><td data-line-number="43" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="43" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;"><br></td></tr><tr style="outline: 0px;"><td data-line-number="44" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="44" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">plt.savefig(<span style="outline: 0px;color: rgb(214, 157, 133);">"Pic.jpg"</span>, dpi=<span style="outline: 0px;color: rgb(184, 215, 163);">600</span>)</td></tr><tr style="outline: 0px;"><td data-line-number="45" style="padding: 0px 14px;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;user-select: none;text-align: center;vertical-align: top;color: rgb(191, 191, 191);"><br></td><td data-line-number="45" style="padding: initial;outline: 0px;word-break: break-all;hyphens: auto;border-width: initial;border-style: none;border-color: initial;border-collapse: initial;min-width: initial;">plt.show()</td></tr></tbody></table>


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

2 结果图展示

关于Python学习指南

学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后给大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!

包括:Python激活码+安装包、Python web开发,Python爬虫,Python数据分析,人工智能、自动化办公等学习教程。带你从零基础系统性的学好Python!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/612641
推荐阅读
相关标签