diff --git a/doc/examples/ERA5-GRIB-example.ipynb b/doc/examples/ERA5-GRIB-example.ipynb index caa702ebe53..5d09f1a7431 100644 --- a/doc/examples/ERA5-GRIB-example.ipynb +++ b/doc/examples/ERA5-GRIB-example.ipynb @@ -37,7 +37,7 @@ "metadata": {}, "outputs": [], "source": [ - "ds = xr.tutorial.load_dataset('era5-2mt-2019-03-uk.grib', engine='cfgrib')" + "ds = xr.tutorial.load_dataset(\"era5-2mt-2019-03-uk.grib\", engine=\"cfgrib\")" ] }, { @@ -72,11 +72,14 @@ "source": [ "import cartopy.crs as ccrs\n", "import cartopy\n", - "fig = plt.figure(figsize=(10,10))\n", + "\n", + "fig = plt.figure(figsize=(10, 10))\n", "ax = plt.axes(projection=ccrs.Robinson())\n", - "ax.coastlines(resolution='10m')\n", - "plot = ds.t2m[0].plot(cmap=plt.cm.coolwarm, transform=ccrs.PlateCarree(), cbar_kwargs={'shrink':0.6})\n", - "plt.title('ERA5 - 2m temperature British Isles March 2019')" + "ax.coastlines(resolution=\"10m\")\n", + "plot = ds.t2m[0].plot(\n", + " cmap=plt.cm.coolwarm, transform=ccrs.PlateCarree(), cbar_kwargs={\"shrink\": 0.6}\n", + ")\n", + "plt.title(\"ERA5 - 2m temperature British Isles March 2019\")" ] }, { @@ -92,8 +95,8 @@ "metadata": {}, "outputs": [], "source": [ - "ds.t2m.sel(longitude=0,latitude=51.5).plot()\n", - "plt.title('ERA5 - London 2m temperature March 2019')" + "ds.t2m.sel(longitude=0, latitude=51.5).plot()\n", + "plt.title(\"ERA5 - London 2m temperature March 2019\")" ] } ], diff --git a/doc/examples/ROMS_ocean_model.ipynb b/doc/examples/ROMS_ocean_model.ipynb index b699c4d5ba9..82d7a8d58af 100644 --- a/doc/examples/ROMS_ocean_model.ipynb +++ b/doc/examples/ROMS_ocean_model.ipynb @@ -26,6 +26,7 @@ "import cartopy.crs as ccrs\n", "import cartopy.feature as cfeature\n", "import matplotlib.pyplot as plt\n", + "\n", "%matplotlib inline\n", "\n", "import xarray as xr" @@ -73,9 +74,9 @@ "outputs": [], "source": [ "# load in the file\n", - "ds = xr.tutorial.open_dataset('ROMS_example.nc', chunks={'ocean_time': 1})\n", + "ds = xr.tutorial.open_dataset(\"ROMS_example.nc\", chunks={\"ocean_time\": 1})\n", "\n", - "# This is a way to turn on chunking and lazy evaluation. Opening with mfdataset, or \n", + "# This is a way to turn on chunking and lazy evaluation. Opening with mfdataset, or\n", "# setting the chunking in the open_dataset would also achive this.\n", "ds" ] @@ -105,12 +106,12 @@ "source": [ "if ds.Vtransform == 1:\n", " Zo_rho = ds.hc * (ds.s_rho - ds.Cs_r) + ds.Cs_r * ds.h\n", - " z_rho = Zo_rho + ds.zeta * (1 + Zo_rho/ds.h)\n", + " z_rho = Zo_rho + ds.zeta * (1 + Zo_rho / ds.h)\n", "elif ds.Vtransform == 2:\n", " Zo_rho = (ds.hc * ds.s_rho + ds.Cs_r * ds.h) / (ds.hc + ds.h)\n", " z_rho = ds.zeta + (ds.zeta + ds.h) * Zo_rho\n", "\n", - "ds.coords['z_rho'] = z_rho.transpose() # needing transpose seems to be an xarray bug\n", + "ds.coords[\"z_rho\"] = z_rho.transpose() # needing transpose seems to be an xarray bug\n", "ds.salt" ] }, @@ -148,7 +149,7 @@ "outputs": [], "source": [ "section = ds.salt.isel(xi_rho=50, eta_rho=slice(0, 167), ocean_time=0)\n", - "section.plot(x='lon_rho', y='z_rho', figsize=(15, 6), clim=(25, 35))\n", + "section.plot(x=\"lon_rho\", y=\"z_rho\", figsize=(15, 6), clim=(25, 35))\n", "plt.ylim([-100, 1]);" ] }, @@ -167,7 +168,7 @@ "metadata": {}, "outputs": [], "source": [ - "ds.salt.isel(s_rho=-1, ocean_time=0).plot(x='lon_rho', y='lat_rho')" + "ds.salt.isel(s_rho=-1, ocean_time=0).plot(x=\"lon_rho\", y=\"lat_rho\")" ] }, { @@ -186,11 +187,13 @@ "proj = ccrs.LambertConformal(central_longitude=-92, central_latitude=29)\n", "fig = plt.figure(figsize=(15, 5))\n", "ax = plt.axes(projection=proj)\n", - "ds.salt.isel(s_rho=-1, ocean_time=0).plot(x='lon_rho', y='lat_rho', \n", - " transform=ccrs.PlateCarree())\n", + "ds.salt.isel(s_rho=-1, ocean_time=0).plot(\n", + " x=\"lon_rho\", y=\"lat_rho\", transform=ccrs.PlateCarree()\n", + ")\n", "\n", - "coast_10m = cfeature.NaturalEarthFeature('physical', 'land', '10m',\n", - " edgecolor='k', facecolor='0.8')\n", + "coast_10m = cfeature.NaturalEarthFeature(\n", + " \"physical\", \"land\", \"10m\", edgecolor=\"k\", facecolor=\"0.8\"\n", + ")\n", "ax.add_feature(coast_10m)" ] }, diff --git a/doc/examples/apply_ufunc_vectorize_1d.ipynb b/doc/examples/apply_ufunc_vectorize_1d.ipynb index e9a48d70173..d1d6a52919c 100644 --- a/doc/examples/apply_ufunc_vectorize_1d.ipynb +++ b/doc/examples/apply_ufunc_vectorize_1d.ipynb @@ -674,7 +674,9 @@ " exclude_dims=set((dim,)), # dimensions allowed to change size. Must be a set!\n", " # vectorize=True, # not needed since numba takes care of vectorizing\n", " dask=\"parallelized\",\n", - " output_dtypes=[data.dtype], # one per output; could also be float or np.dtype(\"float64\")\n", + " output_dtypes=[\n", + " data.dtype\n", + " ], # one per output; could also be float or np.dtype(\"float64\")\n", " ).rename({\"__newdim__\": dim})\n", " interped[dim] = newdim # need to add this manually\n", "\n", diff --git a/doc/examples/monthly-means.ipynb b/doc/examples/monthly-means.ipynb index 3490fc9a4fe..fd31e21a872 100644 --- a/doc/examples/monthly-means.ipynb +++ b/doc/examples/monthly-means.ipynb @@ -29,7 +29,7 @@ "import numpy as np\n", "import pandas as pd\n", "import xarray as xr\n", - "import matplotlib.pyplot as plt " + "import matplotlib.pyplot as plt" ] }, { @@ -50,7 +50,7 @@ }, "outputs": [], "source": [ - "ds = xr.tutorial.open_dataset('rasm').load()\n", + "ds = xr.tutorial.open_dataset(\"rasm\").load()\n", "ds" ] }, @@ -88,13 +88,15 @@ "outputs": [], "source": [ "# Calculate the weights by grouping by 'time.season'.\n", - "weights = month_length.groupby('time.season') / month_length.groupby('time.season').sum()\n", + "weights = (\n", + " month_length.groupby(\"time.season\") / month_length.groupby(\"time.season\").sum()\n", + ")\n", "\n", "# Test that the sum of the weights for each season is 1.0\n", - "np.testing.assert_allclose(weights.groupby('time.season').sum().values, np.ones(4))\n", + "np.testing.assert_allclose(weights.groupby(\"time.season\").sum().values, np.ones(4))\n", "\n", "# Calculate the weighted average\n", - "ds_weighted = (ds * weights).groupby('time.season').sum(dim='time')" + "ds_weighted = (ds * weights).groupby(\"time.season\").sum(dim=\"time\")" ] }, { @@ -123,7 +125,7 @@ "outputs": [], "source": [ "# only used for comparisons\n", - "ds_unweighted = ds.groupby('time.season').mean('time')\n", + "ds_unweighted = ds.groupby(\"time.season\").mean(\"time\")\n", "ds_diff = ds_weighted - ds_unweighted" ] }, @@ -139,39 +141,54 @@ "outputs": [], "source": [ "# Quick plot to show the results\n", - "notnull = pd.notnull(ds_unweighted['Tair'][0])\n", - "\n", - "fig, axes = plt.subplots(nrows=4, ncols=3, figsize=(14,12))\n", - "for i, season in enumerate(('DJF', 'MAM', 'JJA', 'SON')):\n", - " ds_weighted['Tair'].sel(season=season).where(notnull).plot.pcolormesh(\n", - " ax=axes[i, 0], vmin=-30, vmax=30, cmap='Spectral_r', \n", - " add_colorbar=True, extend='both')\n", - " \n", - " ds_unweighted['Tair'].sel(season=season).where(notnull).plot.pcolormesh(\n", - " ax=axes[i, 1], vmin=-30, vmax=30, cmap='Spectral_r', \n", - " add_colorbar=True, extend='both')\n", - "\n", - " ds_diff['Tair'].sel(season=season).where(notnull).plot.pcolormesh(\n", - " ax=axes[i, 2], vmin=-0.1, vmax=.1, cmap='RdBu_r',\n", - " add_colorbar=True, extend='both')\n", + "notnull = pd.notnull(ds_unweighted[\"Tair\"][0])\n", + "\n", + "fig, axes = plt.subplots(nrows=4, ncols=3, figsize=(14, 12))\n", + "for i, season in enumerate((\"DJF\", \"MAM\", \"JJA\", \"SON\")):\n", + " ds_weighted[\"Tair\"].sel(season=season).where(notnull).plot.pcolormesh(\n", + " ax=axes[i, 0],\n", + " vmin=-30,\n", + " vmax=30,\n", + " cmap=\"Spectral_r\",\n", + " add_colorbar=True,\n", + " extend=\"both\",\n", + " )\n", + "\n", + " ds_unweighted[\"Tair\"].sel(season=season).where(notnull).plot.pcolormesh(\n", + " ax=axes[i, 1],\n", + " vmin=-30,\n", + " vmax=30,\n", + " cmap=\"Spectral_r\",\n", + " add_colorbar=True,\n", + " extend=\"both\",\n", + " )\n", + "\n", + " ds_diff[\"Tair\"].sel(season=season).where(notnull).plot.pcolormesh(\n", + " ax=axes[i, 2],\n", + " vmin=-0.1,\n", + " vmax=0.1,\n", + " cmap=\"RdBu_r\",\n", + " add_colorbar=True,\n", + " extend=\"both\",\n", + " )\n", "\n", " axes[i, 0].set_ylabel(season)\n", - " axes[i, 1].set_ylabel('')\n", - " axes[i, 2].set_ylabel('')\n", + " axes[i, 1].set_ylabel(\"\")\n", + " axes[i, 2].set_ylabel(\"\")\n", "\n", "for ax in axes.flat:\n", " ax.axes.get_xaxis().set_ticklabels([])\n", " ax.axes.get_yaxis().set_ticklabels([])\n", - " ax.axes.axis('tight')\n", - " ax.set_xlabel('')\n", - " \n", - "axes[0, 0].set_title('Weighted by DPM')\n", - "axes[0, 1].set_title('Equal Weighting')\n", - "axes[0, 2].set_title('Difference')\n", - " \n", + " ax.axes.axis(\"tight\")\n", + " ax.set_xlabel(\"\")\n", + "\n", + "axes[0, 0].set_title(\"Weighted by DPM\")\n", + "axes[0, 1].set_title(\"Equal Weighting\")\n", + "axes[0, 2].set_title(\"Difference\")\n", + "\n", "plt.tight_layout()\n", "\n", - "fig.suptitle('Seasonal Surface Air Temperature', fontsize=16, y=1.02)" + "fig.suptitle(\"Seasonal Surface Air Temperature\", fontsize=16, y=1.02)" ] }, { @@ -186,18 +203,20 @@ "outputs": [], "source": [ "# Wrap it into a simple function\n", - "def season_mean(ds, calendar='standard'):\n", + "def season_mean(ds, calendar=\"standard\"):\n", " # Make a DataArray with the number of days in each month, size = len(time)\n", " month_length = ds.time.dt.days_in_month\n", "\n", " # Calculate the weights by grouping by 'time.season'\n", - " weights = month_length.groupby('time.season') / month_length.groupby('time.season').sum()\n", + " weights = (\n", + " month_length.groupby(\"time.season\") / month_length.groupby(\"time.season\").sum()\n", + " )\n", "\n", " # Test that the sum of the weights for each season is 1.0\n", - " np.testing.assert_allclose(weights.groupby('time.season').sum().values, np.ones(4))\n", + " np.testing.assert_allclose(weights.groupby(\"time.season\").sum().values, np.ones(4))\n", "\n", " # Calculate the weighted average\n", - " return (ds * weights).groupby('time.season').sum(dim='time')" + " return (ds * weights).groupby(\"time.season\").sum(dim=\"time\")" ] } ], diff --git a/doc/examples/multidimensional-coords.ipynb b/doc/examples/multidimensional-coords.ipynb index 3327192e324..f095d1137de 100644 --- a/doc/examples/multidimensional-coords.ipynb +++ b/doc/examples/multidimensional-coords.ipynb @@ -48,7 +48,7 @@ }, "outputs": [], "source": [ - "ds = xr.tutorial.open_dataset('rasm').load()\n", + "ds = xr.tutorial.open_dataset(\"rasm\").load()\n", "ds" ] }, @@ -94,7 +94,7 @@ }, "outputs": [], "source": [ - "fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(14,4))\n", + "fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(14, 4))\n", "ds.xc.plot(ax=ax1)\n", "ds.yc.plot(ax=ax2)" ] @@ -140,12 +140,14 @@ }, "outputs": [], "source": [ - "plt.figure(figsize=(14,6))\n", + "plt.figure(figsize=(14, 6))\n", "ax = plt.axes(projection=ccrs.PlateCarree())\n", "ax.set_global()\n", - "ds.Tair[0].plot.pcolormesh(ax=ax, transform=ccrs.PlateCarree(), x='xc', y='yc', add_colorbar=False)\n", + "ds.Tair[0].plot.pcolormesh(\n", + " ax=ax, transform=ccrs.PlateCarree(), x=\"xc\", y=\"yc\", add_colorbar=False\n", + ")\n", "ax.coastlines()\n", - "ax.set_ylim([0,90]);" + "ax.set_ylim([0, 90]);" ] }, { @@ -169,11 +171,13 @@ "outputs": [], "source": [ "# define two-degree wide latitude bins\n", - "lat_bins = np.arange(0,91,2)\n", + "lat_bins = np.arange(0, 91, 2)\n", "# define a label for each bin corresponding to the central latitude\n", - "lat_center = np.arange(1,90,2)\n", + "lat_center = np.arange(1, 90, 2)\n", "# group according to those bins and take the mean\n", - "Tair_lat_mean = ds.Tair.groupby_bins('xc', lat_bins, labels=lat_center).mean(dim=xr.ALL_DIMS)\n", + "Tair_lat_mean = ds.Tair.groupby_bins(\"xc\", lat_bins, labels=lat_center).mean(\n", + " dim=xr.ALL_DIMS\n", + ")\n", "# plot the result\n", "Tair_lat_mean.plot()" ] diff --git a/doc/examples/visualization_gallery.ipynb b/doc/examples/visualization_gallery.ipynb index 831f162d998..e6fa564db0d 100644 --- a/doc/examples/visualization_gallery.ipynb +++ b/doc/examples/visualization_gallery.ipynb @@ -18,6 +18,7 @@ "import cartopy.crs as ccrs\n", "import matplotlib.pyplot as plt\n", "import xarray as xr\n", + "\n", "%matplotlib inline" ] }, @@ -34,7 +35,7 @@ "metadata": {}, "outputs": [], "source": [ - "ds = xr.tutorial.load_dataset('air_temperature')" + "ds = xr.tutorial.load_dataset(\"air_temperature\")" ] }, { @@ -62,10 +63,13 @@ "# This is the map projection we want to plot *onto*\n", "map_proj = ccrs.LambertConformal(central_longitude=-95, central_latitude=45)\n", "\n", - "p = air.plot(transform=ccrs.PlateCarree(), # the data's projection\n", - " col='time', col_wrap=1, # multiplot settings\n", - " aspect=ds.dims['lon'] / ds.dims['lat'], # for a sensible figsize\n", - " subplot_kws={'projection': map_proj}) # the plot's projection\n", + "p = air.plot(\n", + " transform=ccrs.PlateCarree(), # the data's projection\n", + " col=\"time\",\n", + " col_wrap=1, # multiplot settings\n", + " aspect=ds.dims[\"lon\"] / ds.dims[\"lat\"], # for a sensible figsize\n", + " subplot_kws={\"projection\": map_proj},\n", + ") # the plot's projection\n", "\n", "# We have to set the map's options on all axes\n", "for ax in p.axes.flat:\n", @@ -93,25 +97,25 @@ "f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(8, 6))\n", "\n", "# The first plot (in kelvins) chooses \"viridis\" and uses the data's min/max\n", - "air.plot(ax=ax1, cbar_kwargs={'label': 'K'})\n", - "ax1.set_title('Kelvins: default')\n", - "ax2.set_xlabel('')\n", + "air.plot(ax=ax1, cbar_kwargs={\"label\": \"K\"})\n", + "ax1.set_title(\"Kelvins: default\")\n", + "ax2.set_xlabel(\"\")\n", "\n", "# The second plot (in celsius) now chooses \"BuRd\" and centers min/max around 0\n", "airc = air - 273.15\n", - "airc.plot(ax=ax2, cbar_kwargs={'label': '°C'})\n", - "ax2.set_title('Celsius: default')\n", - "ax2.set_xlabel('')\n", - "ax2.set_ylabel('')\n", + "airc.plot(ax=ax2, cbar_kwargs={\"label\": \"°C\"})\n", + "ax2.set_title(\"Celsius: default\")\n", + "ax2.set_xlabel(\"\")\n", + "ax2.set_ylabel(\"\")\n", "\n", "# The center doesn't have to be 0\n", - "air.plot(ax=ax3, center=273.15, cbar_kwargs={'label': 'K'})\n", - "ax3.set_title('Kelvins: center=273.15')\n", + "air.plot(ax=ax3, center=273.15, cbar_kwargs={\"label\": \"K\"})\n", + "ax3.set_title(\"Kelvins: center=273.15\")\n", "\n", "# Or it can be ignored\n", - "airc.plot(ax=ax4, center=False, cbar_kwargs={'label': '°C'})\n", - "ax4.set_title('Celsius: center=False')\n", - "ax4.set_ylabel('')\n", + "airc.plot(ax=ax4, center=False, cbar_kwargs={\"label\": \"°C\"})\n", + "ax4.set_title(\"Celsius: center=False\")\n", + "ax4.set_ylabel(\"\")\n", "\n", "# Make it nice\n", "plt.tight_layout()" @@ -143,9 +147,10 @@ "\n", "# Plot data\n", "air2d.plot(ax=ax1, levels=levels)\n", - "air2d.plot(ax=ax2, levels=levels, cbar_kwargs={'ticks': levels})\n", - "air2d.plot(ax=ax3, levels=levels, cbar_kwargs={'ticks': levels,\n", - " 'spacing': 'proportional'})\n", + "air2d.plot(ax=ax2, levels=levels, cbar_kwargs={\"ticks\": levels})\n", + "air2d.plot(\n", + " ax=ax3, levels=levels, cbar_kwargs={\"ticks\": levels, \"spacing\": \"proportional\"}\n", + ")\n", "\n", "# Show plots\n", "plt.tight_layout()" @@ -178,12 +183,12 @@ "isel_lats = [10, 15, 20]\n", "\n", "# Temperature vs longitude plot - illustrates the \"hue\" kwarg\n", - "air.isel(time=0, lat=isel_lats).plot.line(ax=ax1, hue='lat')\n", - "ax1.set_ylabel('°C')\n", + "air.isel(time=0, lat=isel_lats).plot.line(ax=ax1, hue=\"lat\")\n", + "ax1.set_ylabel(\"°C\")\n", "\n", "# Temperature vs time plot - illustrates the \"x\" and \"add_legend\" kwargs\n", - "air.isel(lon=30, lat=isel_lats).plot.line(ax=ax2, x='time', add_legend=False)\n", - "ax2.set_ylabel('')\n", + "air.isel(lon=30, lat=isel_lats).plot.line(ax=ax2, x=\"time\", add_legend=False)\n", + "ax2.set_ylabel(\"\")\n", "\n", "# Show\n", "plt.tight_layout()" @@ -216,12 +221,12 @@ "\n", "# The data is in UTM projection. We have to set it manually until\n", "# https://github.com/SciTools/cartopy/issues/813 is implemented\n", - "crs = ccrs.UTM('18')\n", + "crs = ccrs.UTM(\"18\")\n", "\n", "# Plot on a map\n", "ax = plt.subplot(projection=crs)\n", - "da.plot.imshow(ax=ax, rgb='band', transform=crs)\n", - "ax.coastlines('10m', color='r')" + "da.plot.imshow(ax=ax, rgb=\"band\", transform=crs)\n", + "ax.coastlines(\"10m\", color=\"r\")" ] }, { @@ -250,20 +255,27 @@ "\n", "da = xr.tutorial.open_rasterio(\"RGB.byte\")\n", "\n", - "x, y = np.meshgrid(da['x'], da['y'])\n", + "x, y = np.meshgrid(da[\"x\"], da[\"y\"])\n", "transformer = Transformer.from_crs(da.crs, \"EPSG:4326\", always_xy=True)\n", "lon, lat = transformer.transform(x, y)\n", - "da.coords['lon'] = (('y', 'x'), lon)\n", - "da.coords['lat'] = (('y', 'x'), lat)\n", + "da.coords[\"lon\"] = ((\"y\", \"x\"), lon)\n", + "da.coords[\"lat\"] = ((\"y\", \"x\"), lat)\n", "\n", "# Compute a greyscale out of the rgb image\n", - "greyscale = da.mean(dim='band')\n", + "greyscale = da.mean(dim=\"band\")\n", "\n", "# Plot on a map\n", "ax = plt.subplot(projection=ccrs.PlateCarree())\n", - "greyscale.plot(ax=ax, x='lon', y='lat', transform=ccrs.PlateCarree(),\n", - " cmap='Greys_r', shading=\"auto\",add_colorbar=False)\n", - "ax.coastlines('10m', color='r')" + "greyscale.plot(\n", + " ax=ax,\n", + " x=\"lon\",\n", + " y=\"lat\",\n", + " transform=ccrs.PlateCarree(),\n", + " cmap=\"Greys_r\",\n", + " shading=\"auto\",\n", + " add_colorbar=False,\n", + ")\n", + "ax.coastlines(\"10m\", color=\"r\")" ] } ],