-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
43 lines (27 loc) · 21.2 KB
/
atom.xml
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
36
37
38
39
40
41
42
43
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>洋流的博客</title>
<subtitle>只争朝夕,不负韶华</subtitle>
<link href="/atom.xml" rel="self"/>
<link href="https://meteolover.github.io/"/>
<updated>2020-05-09T01:46:41.461Z</updated>
<id>https://meteolover.github.io/</id>
<author>
<name>洋流</name>
</author>
<generator uri="https://hexo.io/">Hexo</generator>
<entry>
<title>代码绘制标准中国版图</title>
<link href="https://meteolover.github.io/2020/05/08/%E4%BB%A3%E7%A0%81%E7%BB%98%E5%88%B6%E6%A0%87%E5%87%86%E4%B8%AD%E5%9B%BD%E7%89%88%E5%9B%BE/"/>
<id>https://meteolover.github.io/2020/05/08/%E4%BB%A3%E7%A0%81%E7%BB%98%E5%88%B6%E6%A0%87%E5%87%86%E4%B8%AD%E5%9B%BD%E7%89%88%E5%9B%BE/</id>
<published>2020-05-08T08:55:53.121Z</published>
<updated>2020-05-09T01:46:41.461Z</updated>
<content type="html"><![CDATA[<p><img src="/2020/05/08/%E4%BB%A3%E7%A0%81%E7%BB%98%E5%88%B6%E6%A0%87%E5%87%86%E4%B8%AD%E5%9B%BD%E7%89%88%E5%9B%BE/output_9_0.png" alt="png"></p><p>基于Python的Basemap模块,绘制标准中国版图!</p><a id="more"></a> <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> mpl_toolkits.basemap <span class="keyword">import</span> Basemap</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"><span class="keyword">from</span> mpl_toolkits.axes_grid1.inset_locator <span class="keyword">import</span> zoomed_inset_axes</span><br><span class="line"><span class="keyword">from</span> matplotlib.patches <span class="keyword">import</span> Polygon</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br></pre></td></tr></table></figure><p>lambert投影:</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="comment"># 读取省份编码</span></span><br><span class="line">df = pd.read_excel(<span class="string">'省份编码.xls'</span>)</span><br><span class="line">color = list(df[<span class="string">'color'</span>])</span><br><span class="line">code = list(df[<span class="string">'ProvinceID'</span>])</span><br><span class="line">name = list(df[<span class="string">'ProvinceNameC'</span>])</span><br><span class="line"></span><br><span class="line">fig = plt.figure(figsize=(<span class="number">10</span>, <span class="number">10</span>))</span><br><span class="line">ax = fig.add_subplot(<span class="number">111</span>)</span><br><span class="line"><span class="comment"># 陆地和海洋设色</span></span><br><span class="line">map1.fillcontinents(color=<span class="string">'#ADADAD'</span>,lake_color=<span class="string">'#ADADAD'</span>)</span><br><span class="line">map1.drawlsmask(land_color=<span class="string">'#ADADAD'</span>,ocean_color=<span class="string">'#B0E2FF'</span>,resolution=<span class="string">'h'</span>)</span><br><span class="line"><span class="comment"># 添加省界线和国界线</span></span><br><span class="line">map1.readshapefile(<span class="string">'D:/map/China_2020/province'</span>,<span class="string">'province'</span>)</span><br><span class="line">map1.readshapefile(<span class="string">'D:/map/China_2020/border'</span>,<span class="string">'border'</span>)</span><br><span class="line"><span class="comment"># 陆地界线和海洋界线设色</span></span><br><span class="line"><span class="keyword">for</span> info, shp <span class="keyword">in</span> zip(map1.border_info, map1.border):</span><br><span class="line"> <span class="keyword">if</span> info[<span class="string">'Name'</span>] == <span class="string">'land'</span>:</span><br><span class="line"> x, y = zip(*shp)</span><br><span class="line"> map1.plot(x, y, marker=<span class="literal">None</span>, color=<span class="string">'#8B0000'</span>)</span><br><span class="line"> <span class="keyword">if</span> info[<span class="string">'Name'</span>] == <span class="string">'ocean'</span>:</span><br><span class="line"> x, y = zip(*shp)</span><br><span class="line"> map1.plot(x, y, marker=<span class="literal">None</span>, color=<span class="string">'#6495ED'</span>)</span><br><span class="line"><span class="comment"># 省市设色</span></span><br><span class="line"><span class="keyword">for</span> info, shp <span class="keyword">in</span> zip(map1.province_info, map1.province):</span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> poly = Polygon(shp, facecolor=color[name.index(info[<span class="string">'省'</span>])],</span><br><span class="line"> edgecolor=color[name.index(info[<span class="string">'省'</span>])],lw=<span class="number">1</span>)</span><br><span class="line"> ax.add_patch(poly)</span><br><span class="line"> <span class="keyword">except</span>:</span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"><span class="comment"># 添加九段线并设色</span></span><br><span class="line">map1.readshapefile(<span class="string">'D:/map/China_2020/nine_line'</span>,<span class="string">'nine_line'</span>)</span><br><span class="line"><span class="keyword">for</span> info, shp <span class="keyword">in</span> zip(map1.nine_line, map1.nine_line):</span><br><span class="line"> x, y = zip(*shp)</span><br><span class="line"> map1.plot(x, y, marker=<span class="literal">None</span>, color=<span class="string">'#8B0000'</span>)</span><br><span class="line"><span class="comment"># 绘制经纬度标签和比例尺</span></span><br><span class="line">map1.drawmeridians(np.arange(<span class="number">0</span>, <span class="number">360</span>, <span class="number">10</span>), labels=[<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">1</span>], color=<span class="string">'grey'</span>)</span><br><span class="line">map1.drawparallels(np.arange(<span class="number">-90</span>, <span class="number">90</span>, <span class="number">10</span>), labels=[<span class="number">1</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>], color=<span class="string">'grey'</span>)</span><br><span class="line">map1.drawmapscale(<span class="number">90</span>,<span class="number">18</span>,<span class="number">100</span>,<span class="number">20</span>,<span class="number">1500</span>,barstyle=<span class="string">'fancy'</span>)</span><br><span class="line"><span class="comment"># 添加南海附图</span></span><br><span class="line">ax = fig.add_axes([<span class="number">0.771</span>, <span class="number">0.208</span>, <span class="number">0.19</span>, <span class="number">0.19</span>],frameon=<span class="literal">False</span>)</span><br><span class="line">axins = zoomed_inset_axes(ax, <span class="number">0.04</span>, loc = <span class="number">3</span>)</span><br><span class="line">axins.set_xlim(<span class="number">106</span>, <span class="number">122</span>)</span><br><span class="line">axins.set_ylim(<span class="number">2</span>, <span class="number">24</span>)</span><br><span class="line">ax.set_xticks([])</span><br><span class="line">ax.set_yticks([])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置附图投影和范围</span></span><br><span class="line">map2 = Basemap(llcrnrlon=<span class="number">106</span>, llcrnrlat=<span class="number">2</span>, urcrnrlon=<span class="number">122</span>, urcrnrlat=<span class="number">24</span>, ax=axins)</span><br><span class="line"><span class="comment"># 陆地和海洋设色</span></span><br><span class="line">map2.fillcontinents(color=<span class="string">'#ADADAD'</span>, lake_color=<span class="string">'#ADADAD'</span>)</span><br><span class="line">map2.drawlsmask(land_color=<span class="string">'#ADADAD'</span>, ocean_color=<span class="string">'#B0E2FF'</span>, resolution=<span class="string">'h'</span>)</span><br><span class="line"><span class="comment"># 添加省界线和国界线</span></span><br><span class="line">map2.readshapefile(<span class="string">'D:/map/China_2020/province'</span>,<span class="string">'province'</span>)</span><br><span class="line">map2.readshapefile(<span class="string">'D:/map/China_2020/border'</span>,<span class="string">'border'</span>)</span><br><span class="line"><span class="comment"># 陆地界线和海洋界线设色</span></span><br><span class="line"><span class="keyword">for</span> info, shp <span class="keyword">in</span> zip(map2.border_info, map2.border):</span><br><span class="line"> <span class="keyword">if</span> info[<span class="string">'Name'</span>] == <span class="string">'land'</span>:</span><br><span class="line"> x, y = zip(*shp)</span><br><span class="line"> map2.plot(x, y, marker=<span class="literal">None</span>, color=<span class="string">'#8B0000'</span>)</span><br><span class="line"> <span class="keyword">if</span> info[<span class="string">'Name'</span>] == <span class="string">'ocean'</span>:</span><br><span class="line"> x, y = zip(*shp)</span><br><span class="line"> map2.plot(x, y, marker=<span class="literal">None</span>, color=<span class="string">'#6495ED'</span>)</span><br><span class="line"><span class="comment"># 省市设色</span></span><br><span class="line"><span class="keyword">for</span> info, shp <span class="keyword">in</span> zip(map2.province_info, map2.province):</span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> poly = Polygon(shp, facecolor=color[name.index(info[<span class="string">'省'</span>])],</span><br><span class="line"> edgecolor=color[name.index(info[<span class="string">'省'</span>])],lw=<span class="number">1</span>)</span><br><span class="line"> axins.add_patch(poly)</span><br><span class="line"> <span class="keyword">except</span>:</span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"><span class="comment"># 添加九段线并设色</span></span><br><span class="line">map2.readshapefile(<span class="string">'D:/map/China_2020/nine_line'</span>,<span class="string">'nine_line'</span>)</span><br><span class="line"><span class="keyword">for</span> info, shp <span class="keyword">in</span> zip(map2.nine_line, map2.nine_line):</span><br><span class="line"> x, y = zip(*shp)</span><br><span class="line"> map2.plot(x, y, marker=<span class="literal">None</span>, color=<span class="string">'#8B0000'</span>)</span><br><span class="line">plt.savefig(<span class="string">'lambert-color.jpg'</span>, dpi=<span class="number">600</span>, bbox_inches=<span class="string">'tight'</span>)</span><br></pre></td></tr></table></figure><p><img src="/2020/05/08/%E4%BB%A3%E7%A0%81%E7%BB%98%E5%88%B6%E6%A0%87%E5%87%86%E4%B8%AD%E5%9B%BD%E7%89%88%E5%9B%BE/output_9_0.png" alt="png"></p><p>mercator投影:</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="comment"># 读取省份编码</span></span><br><span class="line">df = pd.read_excel(<span class="string">'省份编码.xls'</span>)</span><br><span class="line">color = list(df[<span class="string">'color'</span>])</span><br><span class="line">code = list(df[<span class="string">'ProvinceID'</span>])</span><br><span class="line">name = list(df[<span class="string">'ProvinceNameC'</span>])</span><br><span class="line"><span class="comment"># 创建画布</span></span><br><span class="line">fig = plt.figure(figsize=(<span class="number">10</span>, <span class="number">10</span>))</span><br><span class="line">ax = fig.add_subplot(<span class="number">111</span>)</span><br><span class="line"><span class="comment"># 墨卡托投影</span></span><br><span class="line">map1 = Basemap(llcrnrlon=<span class="number">72</span>, llcrnrlat=<span class="number">2</span>, urcrnrlon=<span class="number">136</span>, urcrnrlat=<span class="number">54</span>,projection=<span class="string">'merc'</span>)</span><br><span class="line"><span class="comment"># 陆地和海洋设色</span></span><br><span class="line">map1.fillcontinents(color=<span class="string">'#ADADAD'</span>,lake_color=<span class="string">'#ADADAD'</span>)</span><br><span class="line">map1.drawlsmask(land_color=<span class="string">'#ADADAD'</span>,ocean_color=<span class="string">'#B0E2FF'</span>,resolution=<span class="string">'h'</span>)</span><br><span class="line"><span class="comment"># 添加省界线和国界线</span></span><br><span class="line">map1.readshapefile(<span class="string">'D:/map/China_2020/province'</span>,<span class="string">'province'</span>)</span><br><span class="line">map1.readshapefile(<span class="string">'D:/map/China_2020/border'</span>,<span class="string">'border'</span>)</span><br><span class="line"><span class="comment"># 陆地界线和海洋界线设色</span></span><br><span class="line"><span class="keyword">for</span> info, shp <span class="keyword">in</span> zip(map1.border_info, map1.border):</span><br><span class="line"> <span class="keyword">if</span> info[<span class="string">'Name'</span>] == <span class="string">'land'</span>:</span><br><span class="line"> x, y = zip(*shp)</span><br><span class="line"> map1.plot(x, y, marker=<span class="literal">None</span>, color=<span class="string">'#8B0000'</span>)</span><br><span class="line"> <span class="keyword">if</span> info[<span class="string">'Name'</span>] == <span class="string">'ocean'</span>:</span><br><span class="line"> x, y = zip(*shp)</span><br><span class="line"> map1.plot(x, y, marker=<span class="literal">None</span>, color=<span class="string">'#6495ED'</span>)</span><br><span class="line"><span class="comment"># 省市设色</span></span><br><span class="line"><span class="keyword">for</span> info, shp <span class="keyword">in</span> zip(map1.province_info, map1.province):</span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> poly = Polygon(shp, facecolor=color[name.index(info[<span class="string">'省'</span>])],</span><br><span class="line"> edgecolor=color[name.index(info[<span class="string">'省'</span>])],lw=<span class="number">1</span>)</span><br><span class="line"> ax.add_patch(poly)</span><br><span class="line"> <span class="keyword">except</span>:</span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"><span class="comment"># 添加九段线并设色</span></span><br><span class="line">map1.readshapefile(<span class="string">'D:/map/China_2020/nine_line'</span>,<span class="string">'nine_line'</span>)</span><br><span class="line"><span class="keyword">for</span> info, shp <span class="keyword">in</span> zip(map1.nine_line, map1.nine_line):</span><br><span class="line"> x, y = zip(*shp)</span><br><span class="line"> map1.plot(x, y, marker=<span class="literal">None</span>, color=<span class="string">'#8B0000'</span>)</span><br><span class="line"><span class="comment"># 绘制经纬度标签和比例尺</span></span><br><span class="line">map1.drawmeridians(np.arange(<span class="number">0</span>, <span class="number">360</span>, <span class="number">10</span>), labels=[<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">1</span>], color=<span class="string">'grey'</span>)</span><br><span class="line">map1.drawparallels(np.arange(<span class="number">-90</span>, <span class="number">90</span>, <span class="number">10</span>), labels=[<span class="number">1</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>], color=<span class="string">'grey'</span>)</span><br><span class="line">map1.drawmapscale(<span class="number">90</span>,<span class="number">10</span>,<span class="number">100</span>,<span class="number">20</span>,<span class="number">2000</span>,barstyle=<span class="string">'fancy'</span>)</span><br><span class="line"></span><br><span class="line">plt.savefig(<span class="string">'mercator-color.jpg'</span>, dpi=<span class="number">600</span>, bbox_inches=<span class="string">'tight'</span>)</span><br></pre></td></tr></table></figure><p><img src="/2020/05/08/%E4%BB%A3%E7%A0%81%E7%BB%98%E5%88%B6%E6%A0%87%E5%87%86%E4%B8%AD%E5%9B%BD%E7%89%88%E5%9B%BE/output_10_0.png" alt="png"></p>]]></content>
<summary type="html">
<p><img src="/2020/05/08/%E4%BB%A3%E7%A0%81%E7%BB%98%E5%88%B6%E6%A0%87%E5%87%86%E4%B8%AD%E5%9B%BD%E7%89%88%E5%9B%BE/output_9_0.png" alt="png"></p>
<p>基于Python的Basemap模块,绘制标准中国版图!</p>
</summary>
<category term="编程基础" scheme="https://Meteolover.github.io/categories/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/"/>
<category term="编程基础" scheme="https://Meteolover.github.io/tags/%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/"/>
</entry>
</feed>