Python在气象中的应用

-3- 绘制中国地图进阶版本

class Draw_China():
    '''
    Function:This script is used to map China.
    Author:xiaohuoya
    Date:2022-05-25
    '''

    def __init__(self):
        self.china_shp = './shp/china.shp'
        self.fs = 6
        self.cl = 'k'

    def Draw(self):
        import matplotlib.pyplot as plt
        import cartopy.crs as ccrs
        import cartopy.feature as cfeature
        import matplotlib.pyplot as plt
        import matplotlib.ticker as mticker
        from cartopy.io.shapereader import Reader

        def Map_China(ax):
            ax.set_extent([60, 160, 0, 60])
            ax.add_feature(cfeature.COASTLINE.with_scale('50m'), linewidth=0.3)
            ax.add_feature(cfeature.OCEAN.with_scale('50m'), linewidth=0.3)
            ax.add_feature(cfeature.LAND.with_scale('50m'), linewidth=0.3)
            ax.add_feature(cfeature.LAKES.with_scale('50m'), linewidth=0.3)
            ax.add_geometries(Reader(self.china_shp).geometries(),
                              ccrs.PlateCarree(), facecolor='none', edgecolor='k', linewidth=0.3)
            lb = ax.gridlines(draw_labels=True, x_inline=False, y_inline=False,
                              linewidth=0.1, color='gray', alpha=0.8, linestyle='--')
            lb.top_labels = False
            lb.right_labels = None
            lb.xlocator = mticker.FixedLocator(
                range(60, 160, 20))
            lb.ylocator = mticker.FixedLocator(
                range(0, 90, 15))
            lb.ylabel_style = {'size': self.fs, 'color': self.cl}
            lb.xlabel_style = {'size': self.fs, 'color': self.cl}
            lb.rotate_labels = False
        fig = plt.figure(figsize=(10, 10), dpi=300)
        proj = ccrs.PlateCarree()
        ax = fig.add_subplot(1, 1, 1, projection=proj)
        Map_China(ax)
        ax.set_title('Fig1:  Made in xiaohuoya',
                     fontsize=self.fs, loc='left')
        f_x = [105, 122, 122, 105, 105]
        f_y = [26, 26, 32, 32, 26]
        ax.plot(f_x, f_y, color='r', linestyle='--', linewidth=0.5)
        f1_x = [100, 127, 127, 100, 100]
        f2_y = [20, 20, 38, 38, 20]
        ax.plot(f1_x, f2_y, color='r', linestyle='-', linewidth=1)

        ax2 = plt.axes([0.25, 0.6, 0.3, 0.3], projection=proj)  # 左下宽高
        ax2.set_extent([105, 122, 26, 32])
        ax2.add_feature(cfeature.COASTLINE.with_scale('50m'), linewidth=0.3)
        ax2.add_feature(cfeature.OCEAN.with_scale('50m'), linewidth=0.3)
        ax2.add_feature(cfeature.LAND.with_scale('50m'), linewidth=0.3)
        ax2.add_feature(cfeature.LAKES.with_scale('50m'), linewidth=0.3)
        ax2.add_geometries(Reader(self.china_shp).geometries(),
                           ccrs.PlateCarree(), facecolor='none', edgecolor='k', linewidth=0.3)
        lb = ax2.gridlines(draw_labels=True, x_inline=False, y_inline=False,
                           linewidth=0.1, color='gray', alpha=0.8, linestyle='--')
        lb.top_labels = False
        lb.right_labels = None
        lb.xlocator = mticker.FixedLocator(
            [105, 109, 113, 117, 121])
        lb.ylocator = mticker.FixedLocator(
            [26, 28, 30, 32, 34])
        lb.ylabel_style = {'size': 4, 'color': 'k'}
        lb.xlabel_style = {'size': 4, 'color': 'k'}
        lb.rotate_labels = False
        ax.arrow(113.5, 32, -11.5, 12, head_width=3.5,
                 head_length=5.5, ec='r', fc='r', color='red', width=0.8)

        plt.show()


if __name__ == '__main__':
    D = Draw_China()
    D.Draw()

1.增添子图的叠加
2.增添线条、箭头的绘制
3.子图的添加实际上就是在第一张图的顶层在绘制一张图


1653552026(1).png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容