### Plotting Data with ROI and Station Data (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Loads data from a different NetCDF file using xarray and salem. Sets up a matplotlib figure and cartopy axes. Plots the first time slice of the data using `plot.one_map_flat`. Adds a second axes for plotting station data or a region of interest using `plot.add_sta` with a shapefile (commented out) or coordinates. ```python import xarray import salem file_name='data/drop_data.nc' ds=xarray.open_dataset(file_name, engine='netcdf4') fig = plt.figure() proj = ccrs.PlateCarree() #ccrs.Robinson()ccrs.Mollweide()Mollweide() ax = fig.add_axes([0.1, 0.1, 0.9, 0.9], projection=proj) levels = np.linspace(-30, 30, num=19) plot.one_map_flat(ds['data'][0], ax, levels=levels, cmap="RdBu", mask_ocean=True, add_coastlines=True, add_land=False, colorbar=True, plotfunc="pcolormesh") ax.set_ylim([-60, 90]) ax.set_title("test nc", fontsize=15, pad=8) ax2 = fig.add_axes([1.05, 0.3, 0.15, 0.5]) plot.add_sta(ax2, ds['data'][0].salem.roi(shape=shpfile), [-80,20], 'lat') ``` -------------------------------- ### Plotting Region Map and Hatching Source: https://github.com/giswlh/plot-function/blob/main/README.md Demonstrates plotting a geographical region using `plot.one_map_region` with various options for layers (coastlines, rivers, lakes, gridlines) and adding hatching using `plot.hatch_map`. ```python plot.one_map_region(data, ax, cmap=cmaps.temp_19lev_r, levels=levels, extents=[30, 130, 22, 58], interval=[20, 18], mask_ocean=False, add_coastlines=True, add_land=False, add_river=True, add_lake=True, add_stock=True, add_gridlines=True, colorbar=True, plotfunc="pcolormesh") plot.hatch_map(ax, pmaskregion, 3 * "/", label="Lack of model agreement", invert=True, linewidth=0.25, color="black") ``` -------------------------------- ### Initialize Plot Figure and Axes (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Initializes a matplotlib figure and adds a subplot with a PlateCarree projection, setting up the plotting environment. Imports necessary libraries including matplotlib, cartopy, numpy, and cmaps. ```python import matplotlib.pyplot as plt import cartopy.crs as ccrs import numpy as np import cmaps fig = plt.figure() proj = ccrs.PlateCarree() #ccrs.Robinson()ccrs.Mollweide()Mollweide() ax = fig.add_subplot(111, projection=proj) levels = np.array([-10, -5, -3, -1, 0, 1, 3, 5, 10])#levels = np.linspace(-1, 1, num=19) ``` -------------------------------- ### Importing Core Plotting Libraries Source: https://github.com/giswlh/plot-function/blob/main/README.md Imports necessary libraries for geographical plotting and visualization, including cartopy for map projections, matplotlib for plotting, numpy for numerical operations, and mplotutils. ```python import cartopy.crs as ccrs import cartopy.feature as cfeature import matplotlib as mpl import matplotlib.hatch import matplotlib.pyplot as plt import mplotutils as mpu import numpy as np from matplotlib.path import Path from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter ``` -------------------------------- ### Loading and Processing NetCDF Data Source: https://github.com/giswlh/plot-function/blob/main/README.md Loads temperature data from a NetCDF file using xarray, calculates the mean over the first dimension, and scales the data. ```python import xarray as xr import numpy as np file_name = 'D:/Onedrive/data/tp/tmp_2022.nc' ds=xr.open_dataset(file_name) da = np.mean(ds['tmp'], 0) * 0.1 ``` -------------------------------- ### Load P-value Data with xarray (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Loads data from a GeoTIFF file named 'data_p.tif' using xarray's open_dataset function and extracts the first band of data. ```python file_name='data/data_p.tif' ds=xr.open_dataset(file_name) p = ds['band_data'][0] ``` -------------------------------- ### Import Plotting Utilities (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Imports the 'plot' module from the local 'utils' package, which contains custom plotting functions used in the document. ```python from utils import plot ``` -------------------------------- ### Load ET P-value Data with xarray (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Loads ET p-value data from a GeoTIFF file using xarray's open_dataset function and extracts the first band of data. ```python file_name='data/ET_p.tif' ds=xr.open_dataset(file_name) p = ds['band_data'][0] ``` -------------------------------- ### Load R2 Data with xarray (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Loads data from a GeoTIFF file named 'r2.tif' using xarray's open_dataset function and extracts the first band of data. ```python import xarray as xr file_name='data/r2.tif' ds=xr.open_dataset(file_name) data = ds['band_data'][0] ``` -------------------------------- ### Load ET Trend Data with xarray (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Loads ET trend data from a GeoTIFF file using xarray's open_dataset function and extracts the first band of data. ```python import xarray as xr file_name='data/ET_trend.tif' ds=xr.open_dataset(file_name) data = ds['band_data'][0] ``` -------------------------------- ### Load Trend Data with xarray (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Loads data from a GeoTIFF file named 'data_trend.tif' using xarray's open_dataset function and extracts the first band of data. ```python import xarray as xr file_name='data/data_trend.tif' ds=xr.open_dataset(file_name) data = ds['band_data'][0] ``` -------------------------------- ### Importing Plotting and Data Libraries (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Imports necessary libraries for geographical plotting, data manipulation, and visualization, including cartopy, matplotlib, numpy, and mplotutils. ```python import cartopy.crs as ccrs import cartopy.feature as cfeature import matplotlib as mpl import matplotlib.hatch import matplotlib.pyplot as plt import mplotutils as mpu import numpy as np from matplotlib.path import Path from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter ``` -------------------------------- ### Importing Custom Plotting Utilities (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Imports a custom plotting module named `plot` from the `utils` package, likely containing helper functions for generating maps and visualizations. ```python from utils import plot ``` -------------------------------- ### Loading and Preparing ERA5 Temperature Data (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Loads ERA5 temperature data from a NetCDF file using xarray, renames dimensions and coordinates for consistency, drops original coordinate variables, converts temperature from Kelvin to Celsius, and displays the processed data variable. ```python import xarray as xr file_name='data/ERA5temp_1978_monthly.nc' ds=xr.open_dataset(file_name) lat = ds['latitude'] lon = ds['longitude'] ds = ds.rename_dims({'latitude':'lat','longitude':'lon'}) ds.coords['lat'] = ('lat', lat.to_numpy()) ds.coords['lon'] = ('lon', lon.to_numpy()) # 对维度lon指定新的坐标信息lon ds = ds.reset_coords(names=['latitude','longitude'], drop=True) ds['t2m'] = ds['t2m'] - 273.15 ds['t2m'] ``` -------------------------------- ### Plotting China Map with Hatching Source: https://github.com/giswlh/plot-function/blob/main/README.md Similar to the previous China plot, this snippet plots the masked temperature data but adds hatching (`plot.hatch_map`) to both the main and inset axes based on the boolean mask (`pamask`) created in the previous step. ```python from utils import plot import matplotlib.pyplot as plt import cartopy.crs as ccrs import numpy as np import cmaps fig = plt.figure() proj = ccrs.PlateCarree() #ccrs.Robinson()ccrs.Mollweide()Mollweide() ax = fig.add_subplot(111, projection=proj) levels = np.array([-6, -4, -2, 0, 2, 4, 8, 15, 20, 25])#levels = np.linspace(-1, 1, num=19) plot.one_map_china(damask, ax, cmap=cmaps.temp_19lev, levels=levels, mask_ocean=False, add_coastlines=True, add_land=False, add_river=True, add_lake=True, add_stock=True, add_gridlines=True, colorbar=True, plotfunc="pcolormesh") plot.hatch_map(ax, pamask, 3 * "/", label="Lack of model agreement", invert=False, linewidth=0.25, color="black") ax2 = fig.add_axes([0.71, 0.196, 0.2, 0.3], projection = proj) plot.sub_china_map(damask, ax2, add_coastlines=True, add_land=False, add_stock=True) plot.hatch_map(ax2, pamask, 3 * "/", label="Lack of model agreement", invert=False, linewidth=0.25, color="black") ``` -------------------------------- ### Load and Process ERA5 Temperature Data (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Loads ERA5 temperature data from a NetCDF file, renames dimensions and coordinates to 'lat' and 'lon', converts temperature from Kelvin to Celsius, and displays the resulting DataArray. ```python import xarray as xr file_name='data/ERA5temp_1978_monthly.nc' ds=xr.open_dataset(file_name) lat = ds['latitude'] lon = ds['longitude'] ds = ds.rename_dims({'latitude':'lat','longitude':'lon'}) ds.coords['lat'] = ('lat', lat.to_numpy()) ds.coords['lon'] = ('lon', lon.to_numpy()) # 对维度lon指定新的坐标信息lon ds = ds.reset_coords(names=['latitude','longitude'], drop=True) ds['t2m'] = ds['t2m'] - 273.15 ds['t2m'] ``` -------------------------------- ### Masking Data with China Shapefile Source: https://github.com/giswlh/plot-function/blob/main/README.md Uses salem and geopandas to load a shapefile representing China and mask the loaded temperature data (`da`) to the region defined by the shapefile. ```python import salem import geopandas as gpd shp_dir='data/china.shp' shpfile=gpd.read_file(shp_dir) damask=da.salem.roi(shape=shpfile) ``` -------------------------------- ### Plotting China Map with Sub-Axis Source: https://github.com/giswlh/plot-function/blob/main/README.md Sets up a matplotlib figure and cartopy projection (Lambert Conformal), plots the masked temperature data (`damask`) over China using `plot.one_map_china`, and adds a smaller inset map using `plot.sub_china_map`. ```python from utils import plot import matplotlib.pyplot as plt import cartopy.crs as ccrs import numpy as np import cmaps fig = plt.figure() #proj = ccrs.PlateCarree() #ccrs.Robinson()ccrs.Mollweide()Mollweide() proj = ccrs.LambertConformal(central_longitude=105, central_latitude=40, standard_parallels=(25.0, 47.0)) ax = fig.add_subplot(111, projection=proj) levels = np.array([-6, -4, -2, 0, 2, 4, 8, 15, 20, 25])#levels = np.linspace(-1, 1, num=19) plot.one_map_china(damask, ax, cmap=cmaps.temp_19lev, levels=levels, mask_ocean=False, add_coastlines=True, add_land=False, add_river=True, add_lake=True, add_stock=False, add_gridlines=True, colorbar=True, plotfunc="pcolormesh") ax2 = fig.add_axes([0.708, 0.174, 0.2, 0.3], projection = proj) plot.sub_china_map(damask, ax2, add_coastlines=True, add_land=False) ``` -------------------------------- ### Plotting Mean Temperature with Hatching (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Sets up a matplotlib figure and cartopy axes. Calls a custom function `plot.one_map` to plot the mean temperature across the time dimension using pcolormesh. Overlays hatching on regions defined by the boolean mask using `plot.hatch_map`. ```python fig = plt.figure() proj = ccrs.PlateCarree() #ccrs.Robinson() #proj = ccrs.Robinson() ax = fig.add_subplot(111, projection=proj) levels = np.linspace(-30, 30, num=19) plot.one_map(ds['t2m'], ax, average='mean', dim='time', cmap="RdBu_r", levels=levels, mask_ocean=True, add_coastlines=True, add_land=True, plotfunc="pcolormesh", colorbar=True, getmean=True) plot.hatch_map(ax, p, 3 * ".", label="Lack of model agreement", invert=True, linewidth=0.25, color="0.1") ``` -------------------------------- ### Plotting Single Time Slice of Temperature Data (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Sets up a matplotlib figure and cartopy axes with a PlateCarree projection. Defines contour levels and calls a custom function `plot.one_map_flat` to plot the first time slice of the temperature data using pcolormesh, with optional coastlines. ```python import matplotlib.pyplot as plt import cartopy.crs as ccrs import numpy as np fig = plt.figure() proj = ccrs.PlateCarree() #ccrs.Robinson()ccrs.Mollweide()Mollweide() ax = fig.add_subplot(111, projection=proj) levels = np.linspace(-30, 30, num=19) plot.one_map_flat(ds['t2m'][0], ax, levels=levels, cmap="BrBG_r", mask_ocean=False, add_coastlines=True, add_land=False, plotfunc="pcolormesh") ``` -------------------------------- ### Display P-value Mask DataArray (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Displays the contents and structure of the 'datap' xarray DataArray, typically used in interactive environments like Jupyter notebooks. ```python datap ``` -------------------------------- ### Plot Global ERA5 Temperature Map (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Plots a global map of the first time step of the processed ERA5 temperature data using the 'one_map_region' function with global extents and specified grid intervals. ```python import matplotlib.pyplot as plt import cartopy.crs as ccrs import numpy as np fig = plt.figure() proj = ccrs.PlateCarree() #ccrs.Robinson()ccrs.Mollweide()Mollweide() ax = fig.add_subplot(111, projection=proj) levels = np.linspace(-30, 30, num=19) plot.one_map_region(ds['t2m'][0], ax, extents=[-180, 180, -90, 90], interval=[60, 30], levels=levels, cmap="RdBu", mask_ocean=False, add_coastlines=True, add_land=False, add_gridlines=True, colorbar=True, plotfunc="pcolormesh") ``` -------------------------------- ### Plot Global R2 Map with Flat Projection (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Plots a global map of the 'r2' data using the 'one_map_flat' function from the utils.plot module with a Robinson projection. ```python from utils import plot import matplotlib.pyplot as plt import cartopy.crs as ccrs import numpy as np fig = plt.figure() proj = ccrs.Robinson() #ccrs.Robinson()ccrs.Mollweide()Mollweide() ax = fig.add_subplot(111, projection=proj) levels = np.linspace(0, 1, num=9) plot.one_map_flat(data, ax, levels=levels, cmap="RdBu", mask_ocean=False, add_coastlines=True, add_land=True, colorbar=True, plotfunc="pcolormesh") ``` -------------------------------- ### Creating Boolean Mask from Data Source: https://github.com/giswlh/plot-function/blob/main/README.md Creates a boolean mask (`pa`) from the original temperature data (`da`) where values are greater than -2, and then masks this boolean array to the China region using the previously loaded shapefile. ```python pa = da > -2 pamask=pa.salem.roi(shape=shpfile) ``` -------------------------------- ### Create P-value Mask and Standardize Dimensions (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Creates a boolean mask where p-values are less than 0.05. It then standardizes the dimensions and coordinates of the mask DataArray to 'lat' and 'lon' using numpy arrays for coordinates. ```python datap = p < 0.05 lat = ds['y'] lon = ds['x'] datap = datap.swap_dims({'y':'lat','x':'lon'}) datap.coords['lat'] = ('lat',lat.to_numpy()) datap.coords['lon'] = ('lon',lon.to_numpy()) # 对维度lon指定新的坐标信息lon datap = datap.reset_coords(names=['y','x'], drop=True) ``` -------------------------------- ### Create P-value Mask and Standardize Dimensions (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Creates a boolean mask where p-values are less than 0.05. It then standardizes the dimensions and coordinates of the mask DataArray to 'lat' and 'lon' using numpy arrays for coordinates. ```python datap = p < 0.05 lat = ds['y'] lon = ds['x'] datap = datap.swap_dims({'y':'lat','x':'lon'}) datap.coords['lat'] = ('lat',lat.to_numpy()) datap.coords['lon'] = ('lon',lon.to_numpy()) # 对维度lon指定新的坐标信息lon datap = datap.reset_coords(names=['y','x'], drop=True) ``` -------------------------------- ### Mask DataArray by GRDC Shapefile Region (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Uses the salem library to mask the 'datap' DataArray based on the geometry defined in a shapefile ('GRDC.shp') using the Region of Interest (roi) function. ```python import salem import geopandas as gpd shp_dir='data/GRDC.shp' shpfile=gpd.read_file(shp_dir) pmaskregion=datap.salem.roi(shape=shpfile) ``` -------------------------------- ### Mask DataArray by Shapefile Region (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Uses the salem library to mask the 'datap' DataArray based on the geometry defined in a shapefile ('Yangtze_4326.shp') using the Region of Interest (roi) function. ```python import salem import geopandas as gpd shp_dir='data/Yangtze_4326.shp' shpfile=gpd.read_file(shp_dir) pmaskregion=datap.salem.roi(shape=shpfile) ``` -------------------------------- ### Preparing Data and Plotting at Warming Levels (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Slices the temperature data into different time periods (likely representing warming levels) and stores them in a list. Calls a custom function `plot.at_warming_level_one` to generate a plot based on these warming level data slices, potentially showing changes or frequencies. ```python at_warming_c = [] at_warming_c.append(ds['t2m'][5:8]) at_warming_c.append(ds['t2m'][9:12]) at_warming_c.append(ds['t2m'][0:3]) len(at_warming_c) #fig = plt.figure() #proj = ccrs.Robinson() # #ax = fig.add_subplot(131, projection=proj) plot.at_warming_level_one(at_warming_c=at_warming_c, unit="Change (times as frequent)", title='drought frequency change w.r.t. 1850-1900', \ average="median", mask_ocean=True, colorbar=True, cmap="RdBu", dim='time', add_legend=False, hatch_data=None, levels=levels, plotfunc='pcolormesh', getmean=True) ``` -------------------------------- ### Plot Regional Map with Trend and P-value Mask (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Generates a regional map plot of the 'data' (ET trend) using matplotlib and cartopy with a PlateCarree projection. It overlays a hatch pattern from 'pmaskregion' to indicate areas where the p-value is less than 0.05. ```python from utils import plot import matplotlib.pyplot as plt import cartopy.crs as ccrs import numpy as np fig = plt.figure() proj = ccrs.PlateCarree() #ccrs.Robinson()ccrs.Mollweide()Mollweide() ax = fig.add_subplot(111, projection=proj) levels = np.array([-1, -0.8, -0.4, 0, 0.4, 0.8, 1])#levels = np.linspace(-1, 1, num=19) plot.one_map_region(data, ax, levels=levels, extents=[89, 125, 23, 37], interval=[9, 7], mask_ocean=False, add_coastlines=False, add_land=False, add_gridlines=True, colorbar=True, plotfunc="pcolormesh") plot.hatch_map(ax, pmaskregion, 3 * "/", label="Lack of model agreement", invert=True, linewidth=0.25, color="0.1") #plt.savefig("test.png", dpi=300) ``` -------------------------------- ### Plot Global R2 Map with Default Grid Lines (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Plots a global map of the 'r2' data using the 'one_map_global_line' function from the utils.plot module with a Robinson projection. This function is noted to have default grid line settings. ```python from utils import plot import matplotlib.pyplot as plt import cartopy.crs as ccrs import numpy as np fig = plt.figure() proj = ccrs.Robinson() #ccrs.Robinson()ccrs.Mollweide()Mollweide() ax = fig.add_subplot(111, projection=proj) levels = np.linspace(0, 1, num=9) plot.one_map_global_line(data, ax, levels=levels, cmap="RdBu", mask_ocean=False, add_coastlines=True, add_land=True, colorbar=True, plotfunc="pcolormesh") ``` -------------------------------- ### Calculating Data Mask Based on Mean Temperature (Python) Source: https://github.com/giswlh/plot-function/blob/main/README.md Calculates a boolean mask by checking where the mean temperature across the time dimension is greater than -20 degrees Celsius. This mask can be used for highlighting specific regions on a map. ```python #import rioxarray as xrx #p = rxr.open_rasterio(filename) p = np.mean(ds['t2m'], 0) > -20 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.