第8章 Python绘图基础 8.1 Matplotlib与cartopy基础知识 8.1.1 绘图结构 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import numpy as npimport matplotlib.pyplot as pltdef f (t ): return np.exp(-t) * np.cos(2 *np.pi*t) t1 = np.arange(0.0 , 5.0 , 0.1 ) t2 = np.arange(0.0 , 5.0 , 0.02 ) plt.figure(1 ) plt.subplot(211 ) plt.plot(t1, f(t1), 'bo' , t2, f(t2), 'k' ) plt.subplot(212 ) plt.plot(t2, np.cos(2 *np.pi*t2), 'r--' ) plt.show()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import numpy as npimport matplotlib.pyplot as pltfig=plt.figure(figsize=(12 ,6 )) for i in range (6 ): ax=fig.add_subplot(2 ,3 ,i+1 ) ax.text(0.5 ,0.5 ,f'{i+1 } ' , fontsize=20 ,ha='center' ,va='center' ) ax.text(0.5 ,0.2 ,f'ax=fig.add_subplot(2,3,{i+1 } )' , fontsize=10 ,ha='center' ,va='center' ) ax = fig.add_axes([0.3 , 0.35 , 0.3 , 0.2 ]) ax.text(0.5 ,0.5 ,'ax=fig.add_axes([0.3, 0.35, 0.2, 0.2])' , fontsize=10 , ha='center' , va='center' ) fig.show()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import xarray as xrimport matplotlib.pyplot as pltimport cartopy.crs as ccrsf = xr.open_dataset('/home/mw/input/pythonbook9857/1980060106.nc' ) z = f['z' ].loc[:,500 ,:,:][0 ] lat = f['latitude' ] lon = f['longitude' ] plt.figure(figsize=(5 , 5 )) ax1 = plt.subplot(1 ,2 ,1 ) c1 = ax1.contour(lon, lat, z) ax1.set_title('Axes' ) ax2 = plt.subplot(1 ,2 ,2 , projection=ccrs.PlateCarree()) c2 = ax2.contour(lon, lat, z,transform=ccrs.PlateCarree()) ax2.set_title('GeoAxes' )
Warning: ecCodes 2.21.0 or higher is recommended. You are running version 2.14.1
Text(0.5, 1.0, 'GeoAxes')
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 import xarray as xrimport matplotlib.pyplot as pltimport cartopy.crs as ccrsimport cartopy.feature as cfeatureimport cartopy.feature as cfeatureimport cartopy.mpl.ticker as ctickerf = xr.open_dataset('/home/mw/input/pythonbook9857/1980060106.nc' ) z = f['z' ].loc[:,500 ,:,:][0 ] lat = f['latitude' ] lon = f['longitude' ] plt.figure(figsize=(8 , 6 )) ax1 = plt.subplot(1 ,1 ,1 , projection=ccrs.PlateCarree()) ax1.set_extent([60 ,180 ,0 ,90 ]) ax1.gridlines() ax1.set_xticks(np.arange(60 ,210 ,30 ), crs=ccrs.PlateCarree()) ax1.set_yticks(np.arange(0 ,120 ,30 ), crs=ccrs.PlateCarree()) ax1.xaxis.set_major_formatter(cticker.LongitudeFormatter()) ax1.yaxis.set_major_formatter(cticker.LatitudeFormatter()) c1 = ax1.contour(lon, lat, z,transform=ccrs.PlateCarree()) plt.show()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import matplotlib.pyplot as pltimport cartopy.crs as ccrsimport cartopy.feature as cfeaturefig = plt.figure(figsize=[10 , 5 ]) ax1 = fig.add_subplot(1 , 2 , 1 ,projection=ccrs.PlateCarree()) ax1.set_xticks(np.arange(-180 ,240 ,60 ), crs=ccrs.PlateCarree()) ax1.set_yticks(np.arange(-90 ,120 ,30 ), crs=ccrs.PlateCarree()) ax1.xaxis.set_major_formatter(cticker.LongitudeFormatter()) ax1.yaxis.set_major_formatter(cticker.LatitudeFormatter()) ax1.gridlines() ax2 = fig.add_subplot(1 , 2 , 2 , projection=ccrs.PlateCarree(central_longitude = 120 )) ax2.set_xticks(np.arange(-180 ,240 ,60 ), crs=ccrs.PlateCarree()) ax2.set_yticks(np.arange(-90 ,120 ,30 ), crs=ccrs.PlateCarree()) ax2.xaxis.set_major_formatter(cticker.LongitudeFormatter()) ax2.yaxis.set_major_formatter(cticker.LatitudeFormatter()) ax2.gridlines() plt.show()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import matplotlib.pyplot as pltimport cartopy.crs as ccrsimport cartopy.feature as cfeaturefig = plt.figure(figsize=[10 , 5 ]) ax1 = fig.add_subplot(1 , 2 , 1 , projection=ccrs.LambertConformal()) gl1 = ax1.gridlines(draw_labels=True ,x_inline=False , y_inline=False ) gl1.rotate_labels = False ax2 = fig.add_subplot(1 , 2 , 2 , projection=ccrs.LambertConformal(central_longitude=120 ,cutoff=0 )) gl2 = ax2.gridlines(draw_labels=True ,x_inline=False , y_inline=False ) gl2.rotate_labels = False plt.show()
1 2 3 4 5 6 7 8 9 10 11 12 import matplotlib.pyplot as pltimport cartopy.crs as ccrsimport cartopy.feature as cfeaturefig = plt.figure(figsize=[10 , 5 ]) ax1 = fig.add_subplot(1 , 2 , 1 , projection=ccrs.Mercator()) gl1 = ax1.gridlines(draw_labels=True ,x_inline=False , y_inline=False ) gl1.xlabels_top = False gl1.ylabels_right = False plt.show()
/opt/conda/lib/python3.7/site-packages/cartopy/mpl/gridliner.py:307: UserWarning: The .xlabels_top attribute is deprecated. Please use .top_labels to toggle visibility instead.
warnings.warn('The .xlabels_top attribute is deprecated. Please '
/opt/conda/lib/python3.7/site-packages/cartopy/mpl/gridliner.py:343: UserWarning: The .ylabels_right attribute is deprecated. Please use .right_labels to toggle visibility instead.
warnings.warn('The .ylabels_right attribute is deprecated. Please '
1 2 3 4 5 6 7 8 9 10 11 12 import matplotlib.pyplot as pltimport cartopy.crs as ccrsimport cartopy.feature as cfeaturefig = plt.figure(figsize=[10 , 5 ]) ax1 = fig.add_subplot(1 , 2 , 1 , projection=ccrs.NorthPolarStereo()) gl1 = ax1.gridlines(draw_labels=True ,x_inline=False , y_inline=True ) ax2 = fig.add_subplot(1 , 2 , 2 , projection=ccrs.SouthPolarStereo()) gl2 = ax2.gridlines(draw_labels=True ,x_inline=False , y_inline=True ) plt.show()
1 2 3 4 5 6 7 8 9 10 11 12 13 import matplotlib.path as mpathimport matplotlib.pyplot as pltimport numpy as npimport cartopy.crs as ccrsimport cartopy.feature as cfeaturefig = plt.figure(figsize=[5 , 5 ]) ax1 = fig.add_subplot(1 , 1 , 1 , projection=ccrs.NorthPolarStereo()) ax1.set_extent([-180 , 180 , 30 , 90 ], ccrs.PlateCarree()) gl1 = ax1.gridlines(draw_labels=True ,x_inline=False , y_inline=False ) gl1.rotate_labels = False plt.show()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import matplotlib.path as mpathimport matplotlib.pyplot as pltimport numpy as npimport cartopy.crs as ccrsimport cartopy.feature as cfeaturefig = plt.figure(figsize=[5 , 5 ]) ax1 = fig.add_subplot(1 , 1 , 1 , projection=ccrs.NorthPolarStereo()) ax1.set_extent([-180 , 180 , 30 , 90 ], ccrs.PlateCarree()) ax1.gridlines() ax1.add_feature(cfeature.COASTLINE.with_scale('50m' )) theta = np.linspace(0 , 2 *np.pi, 100 ) center, radius = [0.5 , 0.5 ], 0.5 verts = np.vstack([np.sin(theta), np.cos(theta)]).T circle = mpath.Path(verts * radius + center) ax1.set_boundary(circle, transform=ax1.transAxes) plt.show()
/opt/conda/lib/python3.7/site-packages/cartopy/io/__init__.py:260: DownloadWarning: Downloading: https://naciscdn.org/naturalearth/50m/physical/ne_50m_coastline.zip
warnings.warn('Downloading: {}'.format(url), DownloadWarning)
8.2 地理绘图基础 8.2.2 在GeoAxes上绘制 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import matplotlib.pyplot as pltimport cartopy.crs as ccrsimport cartopy.mpl.ticker as ctickerimport geopandas as gpdimport geopandas as gpdgdf = gpd.GeoDataFrame.from_file('/home/mw/input/pythonbook9857/circle.json' , encoding='utf8' ) fig = plt.figure(figsize=(10 ,8 )) ax = fig.add_subplot(1 , 1 , 1 , projection=ccrs.PlateCarree()) ax.set_extent([115.5 , 123 , 29.5 , 36.5 ], ccrs.PlateCarree()) ax.add_geometries(gdf['geometry' ], crs=ccrs.PlateCarree(), facecolor='none' , edgecolor='black' ) ax.set_xticks(np.arange(116 , 123 , 1 ), crs=ccrs.PlateCarree()) ax.set_yticks(np.arange(30 , 36 , 1 ), crs=ccrs.PlateCarree()) ax.xaxis.set_major_formatter(cticker.LongitudeFormatter()) ax.yaxis.set_major_formatter(cticker.LatitudeFormatter()) plt.show()
8.2.3 几何数据筛选示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import matplotlib.pyplot as pltimport cartopy.crs as ccrsimport cartopy.mpl.ticker as ctickerimport geopandas as gpdname_a = gdf.loc[gdf['name' ]=='A' ] fig = plt.figure(figsize=(10 ,8 )) ax = fig.add_subplot(1 , 1 , 1 , projection=ccrs.PlateCarree()) ax.set_extent([115.5 , 123 , 29.5 , 36.5 ], ccrs.PlateCarree()) ax.add_geometries(name_a['geometry' ], crs=ccrs.PlateCarree(), facecolor='none' , edgecolor='black' ) ax.set_xticks(np.arange(116 , 123 , 1 ), crs=ccrs.PlateCarree()) ax.set_yticks(np.arange(30 , 36 , 1 ), crs=ccrs.PlateCarree()) ax.xaxis.set_major_formatter(cticker.LongitudeFormatter()) ax.yaxis.set_major_formatter(cticker.LatitudeFormatter()) plt.show()
8.2.4 多边形合并 1 2 3 4 5 6 7 8 9 10 11 12 import matplotlib.pyplot as pltfrom shapely.ops import unary_unionimport geopandas as gpdimport cartopy.crs as ccrsgdf = gpd.GeoDataFrame.from_file('/home/mw/input/pythonbook9857/circle.json' , encoding='utf8' ) geom = unary_union([geom if geom.is_valid else geom.buffer(0 ) for geom in gdf['geometry' ]]) print (type (gdf['geometry' ]), len (gdf['geometry' ]))print (type (geom))
<class 'geopandas.geoseries.GeoSeries'> 2
<class 'shapely.geometry.polygon.Polygon'>
8.3 颜色表(colormap) 8.3.3 创建自定义色标 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import numpy as npimport matplotlib.pyplot as pltimport matplotlib.colors as mcolorsN = 100 X, Y = np.mgrid[-3 :3 :complex (0 , N), -2 :2 :complex (0 , N)] Z1 = np.exp(-X**2 - Y**2 ) Z2 = np.exp(-(X - 1 )**2 - (Y - 1 )**2 ) Z = (Z1 - Z2) * 2 fig = plt.figure() ax = fig.add_subplot(1 , 1 , 1 ) levels = [0.1 , 0.3 , 0.5 , 0.9 ] cmap = mcolors.ListedColormap(['r' , 'g' , 'b' ]) cmap.set_under('c' ) cmap.set_over('m' ) norm = mcolors.BoundaryNorm(levels, cmap.N) pcm = ax.contourf(Z, levels=levels, cmap=cmap, norm=norm, extend='both' ) fig.colorbar(pcm, ax=ax, extend='both' ) plt.show()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import numpy as npimport matplotlib.pyplot as pltimport matplotlib.colors as mcolorsN = 100 X, Y = np.mgrid[-3 :3 :complex (0 , N), -2 :2 :complex (0 , N)] Z1 = np.exp(-X**2 - Y**2 ) Z2 = np.exp(-(X - 1 )**2 - (Y - 1 )**2 ) Z = (Z1 - Z2) * 2 fig = plt.figure() ax = fig.add_subplot(1 , 1 , 1 ) levels = [0.1 , 0.3 , 0.5 , 0.9 ] cmap, norm = mcolors.from_levels_and_colors(levels, ['c' , 'r' , 'g' , 'b' , 'm' ], 'both' ) pcm = ax.contourf(Z, levels=levels, cmap=cmap, norm=norm, extend='both' ) fig.colorbar(pcm, ax=ax, extend='both' ) plt.show()
8.4 图像显示与保存 8.4.1 图像显示 1 2 3 4 5 6 7 8 9 10 11 12 import numpy as npimport matplotlib.pyplot as pltx = np.linspace(0 , 10 , 500 ) y = np.sin(x) fig = plt.figure(figsize=(10 , 8 )) ax1 = fig.add_subplot(1 ,1 ,1 ) ax1.plot(x, y) plt.show()
8.4.2 图像保存 1 2 3 4 5 6 7 8 9 10 import numpy as npimport matplotlib.pyplot as pltx = np.linspace(0 , 10 , 500 ) y = np.sin(x) fig = plt.figure(figsize=(10 , 8 )) ax1 = fig.add_subplot(1 ,1 ,1 ) ax1.plot(x, y) plt.savefig('./sinx.png' , dpi=400 , bbox_inches='tight' )