You’ll notice that printing a dataset still shows a preview of array values,
│ │ │ even if they are actually Dask arrays. We can do this quickly with Dask because
│ │ │ we only need to compute the first few values (typically from the first block).
│ │ │ To reveal the true nature of an array, print a DataArray:
Once you’ve manipulated a Dask array, you can still write a dataset too big to
│ │ ├── ./usr/share/doc/python-xarray-doc/html/data-structures.html
│ │ │ @@ -890,18 +890,18 @@
│ │ │ a method call with an external function (e.g., ds.pipe(func)) instead of
│ │ │ simply calling it (e.g., func(ds)). This allows you to write pipelines for
│ │ │ transforming your data (using “method chaining”) instead of writing hard to
│ │ │ follow nested function calls:
│ │ │
# these lines are equivalent, but with pipe we can make the logic flow
│ │ │ # entirely from left to right
│ │ │ In [60]: plt.plot((2*ds.temperature.sel(x=0)).mean("y"))
│ │ │ -Out[60]: [<matplotlib.lines.Line2D at 0x7f707159cc40>]
│ │ │ +Out[60]: [<matplotlib.lines.Line2D at 0x7f786fb7fdc0>]
│ │ │
│ │ │ In [61]: (ds.temperature.sel(x=0).pipe(lambdax:2*x).mean("y").pipe(plt.plot))
│ │ │ -Out[61]: [<matplotlib.lines.Line2D at 0x7f70715ab7c0>]
│ │ │ +Out[61]: [<matplotlib.lines.Line2D at 0x7f786fb0e0a0>]
│ │ │
With xarray, there is no performance penalty for creating new datasets, even if
│ │ │ variables are lazily loaded from a file on disk. Creating new objects instead
│ │ ├── ./usr/share/doc/python-xarray-doc/html/examples/ERA5-GRIB-example.html
│ │ │ @@ -521,15 +521,15 @@
│ │ │ /build/python-xarray-zcUjoD/python-xarray-0.16.2/xarray/tutorial.py in open_dataset(name, cache, cache_dir, github_url, branch, **kws)
│ │ │ 76# May want to add an option to remove it.
│ │ │ 77ifnot _os.path.isdir(longdir):
│ │ │ ---> 78_os.mkdir(longdir)
│ │ │ 79
│ │ │ 80 url ="/".join((github_url,"raw", branch, fullname))
│ │ │
│ │ │ -FileNotFoundError: [Errno 2] No such file or directory: '/sbuild-nonexistent/.xarray_tutorial_data'
│ │ │ +FileNotFoundError: [Errno 2] No such file or directory: '/nonexistent/.xarray_tutorial_data'
│ │ │
│ │ │
│ │ │
Let’s create a simple plot of 2-m air temperature in degrees Celsius:
Write equations to calculate the vertical coordinate. These will be only evaluated when data is requested. Information about the ROMS vertical coordinate can be found (here)[https://www.myroms.org/wiki/Vertical_S-coordinate]
│ │ │
In short, for Vtransform==2 as used in this example,
│ │ ├── ./usr/share/doc/python-xarray-doc/html/examples/ROMS_ocean_model.ipynb.gz
│ │ │ ├── ROMS_ocean_model.ipynb
│ │ │ │ │ --- /tmp/diffoscope_dn2f40t9_/tmppiw76r5vGzipContainer/75384067-a040-483f-92b6-3f4b8c52b5e7
│ │ │ │ ├── +++ /tmp/diffoscope_dn2f40t9_/tmpx5e2xdd1GzipContainer/6b91b89d-13f5-4592-a59a-97eb1fce8152
│ │ │ │ │┄ Differences: {'replace': "OrderedDict([('cells', [OrderedDict([('cell_type', 'markdown'), ('metadata', "
│ │ │ │ │┄ "OrderedDict()), ('source', ['# ROMS Ocean Model Example'])]), "
│ │ │ │ │┄ "OrderedDict([('cell_type', 'markdown'), ('metadata', OrderedDict()), ('source', ['The "
│ │ │ │ │┄ 'Regional Ocean Modeling System ([ROMS](http://myroms.org)) is an open source '
│ │ │ │ │┄ 'hydrodynamic model that is used for simulating currents and water properties in '
│ │ │ │ │┄ 'coastal and estuarine regions. ROMS is one […]
│ │ │ │ │ @@ -69,22 +69,22 @@
│ │ │ │ │ {
│ │ │ │ │ "cell_type": "code",
│ │ │ │ │ "execution_count": 2,
│ │ │ │ │ "metadata": {},
│ │ │ │ │ "outputs": [
│ │ │ │ │ {
│ │ │ │ │ "ename": "FileNotFoundError",
│ │ │ │ │ - "evalue": "[Errno 2] No such file or directory: '/sbuild-nonexistent/.xarray_tutorial_data'",
│ │ │ │ │ + "evalue": "[Errno 2] No such file or directory: '/nonexistent/.xarray_tutorial_data'",
│ │ │ │ │ "output_type": "error",
│ │ │ │ │ "traceback": [
│ │ │ │ │ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
│ │ │ │ │ "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)",
│ │ │ │ │ "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# load in the file\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mxr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtutorial\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen_dataset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'ROMS_example.nc'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mchunks\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;34m'ocean_time'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m# This is a way to turn on chunking and lazy evaluation. Opening with mfdataset, or\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;31m# setting the chunking in the open_dataset would also achive this.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
│ │ │ │ │ "\u001b[0;32m/build/python-xarray-zcUjoD/python-xarray-0.16.2/xarray/tutorial.py\u001b[0m in \u001b[0;36mopen_dataset\u001b[0;34m(name, cache, cache_dir, github_url, branch, **kws)\u001b[0m\n\u001b[1;32m 76\u001b[0m \u001b[0;31m# May want to add an option to remove it.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 77\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0m_os\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0misdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlongdir\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 78\u001b[0;31m \u001b[0m_os\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmkdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlongdir\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 79\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 80\u001b[0m \u001b[0murl\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"/\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgithub_url\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"raw\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbranch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfullname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
│ │ │ │ │ - "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '/sbuild-nonexistent/.xarray_tutorial_data'"
│ │ │ │ │ + "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '/nonexistent/.xarray_tutorial_data'"
│ │ │ │ │ ]
│ │ │ │ │ }
│ │ │ │ │ ],
│ │ │ │ │ "source": [
│ │ │ │ │ "# load in the file\n",
│ │ │ │ │ "ds = xr.tutorial.open_dataset('ROMS_example.nc', chunks={'ocean_time': 1})\n",
│ │ │ │ │ "\n",
│ │ ├── ./usr/share/doc/python-xarray-doc/html/examples/apply_ufunc_vectorize_1d.html
│ │ │ @@ -553,15 +553,15 @@
│ │ │ /build/python-xarray-zcUjoD/python-xarray-0.16.2/xarray/tutorial.py in open_dataset(name, cache, cache_dir, github_url, branch, **kws)
│ │ │ 76# May want to add an option to remove it.
│ │ │ 77ifnot _os.path.isdir(longdir):
│ │ │ ---> 78_os.mkdir(longdir)
│ │ │ 79
│ │ │ 80 url ="/".join((github_url,"raw", branch, fullname))
│ │ │
│ │ │ -FileNotFoundError: [Errno 2] No such file or directory: '/sbuild-nonexistent/.xarray_tutorial_data'
│ │ │ +FileNotFoundError: [Errno 2] No such file or directory: '/nonexistent/.xarray_tutorial_data'
│ │ │
│ │ │
│ │ │
The function we will apply is np.interp which expects 1D numpy arrays. This functionality is already implemented in xarray so we use that capability to make sure we are not making mistakes.
We first have to come up with the weights, - calculate the month lengths for each monthly data record - calculate weights using groupby('time.season')
│ │ │
Finally, we just need to multiply our weights by the Dataset and sum allong the time dimension. Creating a DataArray for the month length is as easy as using the days_in_month accessor on the time coordinate. The calendar type, in this case 'noleap', is automatically considered in this operation.
│ │ ├── ./usr/share/doc/python-xarray-doc/html/examples/monthly-means.ipynb.gz
│ │ │ ├── monthly-means.ipynb
│ │ │ │ │ --- /tmp/diffoscope_dn2f40t9_/tmponyfih7jGzipContainer/5a4a0eca-8885-4a09-b723-8d45b95540cd
│ │ │ │ ├── +++ /tmp/diffoscope_dn2f40t9_/tmpdmpqkgdfGzipContainer/da72fa57-2a91-45b5-93c6-dd5fb5998530
│ │ │ │ │┄ Differences: {'replace': "OrderedDict([('cells', [OrderedDict([('cell_type', 'markdown'), ('metadata', "
│ │ │ │ │┄ "OrderedDict()), ('source', ['Calculating Seasonal Averages from Timeseries of Monthly "
│ │ │ │ │┄ "Means \\n', '=====\\n', '\\n', 'Author: [Joe "
│ │ │ │ │┄ "Hamman](https://github.com/jhamman/)\\n', '\\n', 'The data used for this example can "
│ │ │ │ │┄ 'be found in the [xarray-data](https://github.com/pydata/xarray-data) repository. You '
│ │ │ │ │┄ "may need to change the path to `rasm.nc` below […]
│ │ │ │ │ @@ -47,22 +47,22 @@
│ │ │ │ │ "end_time": "2018-11-28T20:51:36.072316Z",
│ │ │ │ │ "start_time": "2018-11-28T20:51:36.016594Z"
│ │ │ │ │ }
│ │ │ │ │ },
│ │ │ │ │ "outputs": [
│ │ │ │ │ {
│ │ │ │ │ "ename": "FileNotFoundError",
│ │ │ │ │ - "evalue": "[Errno 2] No such file or directory: '/sbuild-nonexistent/.xarray_tutorial_data'",
│ │ │ │ │ + "evalue": "[Errno 2] No such file or directory: '/nonexistent/.xarray_tutorial_data'",
│ │ │ │ │ "output_type": "error",
│ │ │ │ │ "traceback": [
│ │ │ │ │ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
│ │ │ │ │ "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)",
│ │ │ │ │ "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mxr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtutorial\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen_dataset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'rasm'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mds\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
│ │ │ │ │ "\u001b[0;32m/build/python-xarray-zcUjoD/python-xarray-0.16.2/xarray/tutorial.py\u001b[0m in \u001b[0;36mopen_dataset\u001b[0;34m(name, cache, cache_dir, github_url, branch, **kws)\u001b[0m\n\u001b[1;32m 76\u001b[0m \u001b[0;31m# May want to add an option to remove it.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 77\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0m_os\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0misdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlongdir\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 78\u001b[0;31m \u001b[0m_os\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmkdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlongdir\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 79\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 80\u001b[0m \u001b[0murl\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"/\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgithub_url\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"raw\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbranch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfullname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
│ │ │ │ │ - "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '/sbuild-nonexistent/.xarray_tutorial_data'"
│ │ │ │ │ + "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '/nonexistent/.xarray_tutorial_data'"
│ │ │ │ │ ]
│ │ │ │ │ }
│ │ │ │ │ ],
│ │ │ │ │ "source": [
│ │ │ │ │ "ds = xr.tutorial.open_dataset('rasm').load()\n",
│ │ │ │ │ "ds"
│ │ │ │ │ ]
│ │ ├── ./usr/share/doc/python-xarray-doc/html/examples/multidimensional-coords.html
│ │ │ @@ -525,15 +525,15 @@
│ │ │ /build/python-xarray-zcUjoD/python-xarray-0.16.2/xarray/tutorial.py in open_dataset(name, cache, cache_dir, github_url, branch, **kws)
│ │ │ 76# May want to add an option to remove it.
│ │ │ 77ifnot _os.path.isdir(longdir):
│ │ │ ---> 78_os.mkdir(longdir)
│ │ │ 79
│ │ │ 80 url ="/".join((github_url,"raw", branch, fullname))
│ │ │
│ │ │ -FileNotFoundError: [Errno 2] No such file or directory: '/sbuild-nonexistent/.xarray_tutorial_data'
│ │ │ +FileNotFoundError: [Errno 2] No such file or directory: '/nonexistent/.xarray_tutorial_data'
│ │ │
│ │ │
│ │ │
In this example, the logical coordinates are x and y, while the physical coordinates are xc and yc, which represent the latitudes and longitude of the data.
"
│ │ │ │ │ ],
│ │ │ │ │ "text/plain": [
│ │ │ │ │ "\n",
│ │ │ │ │ "Dimensions: (location: 3, time: 731)\n",
│ │ │ │ │ "Coordinates:\n",
│ │ │ │ │ " * time (time) datetime64[ns] 2000-01-01 2000-01-02 ... 2001-12-31\n",
│ │ │ │ │ " * location (location) object 'IA' 'IN' 'IL'\n",
│ │ ├── ./usr/share/doc/python-xarray-doc/html/indexing.html
│ │ │ @@ -873,15 +873,15 @@
│ │ │ /build/python-xarray-zcUjoD/python-xarray-0.16.2/xarray/tutorial.py in open_dataset(name, cache, cache_dir, github_url, branch, **kws)
│ │ │ 76# May want to add an option to remove it.
│ │ │ 77ifnot_os.path.isdir(longdir):
│ │ │ ---> 78_os.mkdir(longdir)
│ │ │ 79
│ │ │ 80url="/".join((github_url,"raw",branch,fullname))
│ │ │
│ │ │ -FileNotFoundError: [Errno 2] No such file or directory: '/sbuild-nonexistent/.xarray_tutorial_data'
│ │ │ +FileNotFoundError: [Errno 2] No such file or directory: '/nonexistent/.xarray_tutorial_data'
│ │ │
│ │ │ # add an empty 2D dataarray
│ │ │ In [54]: ds["empty"]=xr.full_like(ds.air.mean("time"),fill_value=0)
│ │ │ ---------------------------------------------------------------------------
│ │ │ AttributeErrorTraceback (most recent call last)
│ │ │ <ipython-input-54-6709edaff03d>in<module>
│ │ │ ----> 1ds["empty"]=xr.full_like(ds.air.mean("time"),fill_value=0)
│ │ ├── ./usr/share/doc/python-xarray-doc/html/internals.html
│ │ │ @@ -429,18 +429,18 @@
│ │ │ /build/python-xarray-zcUjoD/python-xarray-0.16.2/xarray/tutorial.py in open_dataset(name, cache, cache_dir, github_url, branch, **kws)
│ │ │ 76 # May want to add an option to remove it.
│ │ │ 77 if not _os.path.isdir(longdir):
│ │ │ ---> 78 _os.mkdir(longdir)
│ │ │ 79
│ │ │ 80 url = "/".join((github_url, "raw", branch, fullname))
│ │ │
│ │ │ -FileNotFoundError: [Errno 2] No such file or directory: '/sbuild-nonexistent/.xarray_tutorial_data'
│ │ │ +FileNotFoundError: [Errno 2] No such file or directory: '/nonexistent/.xarray_tutorial_data'
│ │ │
│ │ │ In [5]: ds.to_zarr("rasm.zarr",mode="w")
│ │ │ -Out[5]: <xarray.backends.zarr.ZarrStore at 0x7f705ccf9520>
│ │ │ +Out[5]: <xarray.backends.zarr.ZarrStore at 0x7f78581b08e0>
│ │ │
│ │ │ In [6]: importzarr
│ │ │
│ │ │ In [7]: zgroup=zarr.open("rasm.zarr")
│ │ │
│ │ │ In [8]: print(zgroup.tree())
│ │ │ /
│ │ ├── ./usr/share/doc/python-xarray-doc/html/interpolation.html
│ │ │ @@ -406,24 +406,24 @@
│ │ │ ....: np.sin(np.linspace(0,2*np.pi,10)),
│ │ │ ....: dims="x",
│ │ │ ....: coords={"x":np.linspace(0,1,10)},
│ │ │ ....: )
│ │ │ ....:
│ │ │
│ │ │ In [17]: da.plot.line("o",label="original")
│ │ │ -Out[17]: [<matplotlib.lines.Line2D at 0x7f705d1eeb20>]
│ │ │ +Out[17]: [<matplotlib.lines.Line2D at 0x7f78586da400>]
│ │ │
│ │ │ In [18]: da.interp(x=np.linspace(0,1,100)).plot.line(label="linear (default)")
│ │ │ -Out[18]: [<matplotlib.lines.Line2D at 0x7f705d1d8220>]
│ │ │ +Out[18]: [<matplotlib.lines.Line2D at 0x7f7858761ca0>]
│ │ │
│ │ │ In [19]: da.interp(x=np.linspace(0,1,100),method="cubic").plot.line(label="cubic")
│ │ │ -Out[19]: [<matplotlib.lines.Line2D at 0x7f705d1d8eb0>]
│ │ │ +Out[19]: [<matplotlib.lines.Line2D at 0x7f78587617c0>]
│ │ │
│ │ │ In [20]: plt.legend()
│ │ │ -Out[20]: <matplotlib.legend.Legend at 0x7f705cfc7340>
│ │ │ +Out[20]: <matplotlib.legend.Legend at 0x7f785861c3a0>
│ │ │
│ │ │
│ │ │
│ │ │
Additional keyword arguments can be passed to scipy’s functions.
│ │ │
# fill 0 for the outside of the original coordinates.
│ │ │ In [21]: da.interp(x=np.linspace(-0.5,1.5,10),kwargs={"fill_value":0.0})
│ │ │ Out[21]:
│ │ │ @@ -609,15 +609,15 @@
│ │ │ /build/python-xarray-zcUjoD/python-xarray-0.16.2/xarray/tutorial.py in open_dataset(name, cache, cache_dir, github_url, branch, **kws)
│ │ │ 76# May want to add an option to remove it.
│ │ │ 77ifnot_os.path.isdir(longdir):
│ │ │ ---> 78_os.mkdir(longdir)
│ │ │ 79
│ │ │ 80url="/".join((github_url,"raw",branch,fullname))
│ │ │
│ │ │ -FileNotFoundError: [Errno 2] No such file or directory: '/sbuild-nonexistent/.xarray_tutorial_data'
│ │ │ +FileNotFoundError: [Errno 2] No such file or directory: '/nonexistent/.xarray_tutorial_data'
│ │ │
│ │ │ In [45]: fig,axes=plt.subplots(ncols=2,figsize=(10,4))
│ │ │
│ │ │ In [46]: ds.air.plot(ax=axes[0])
│ │ │ ---------------------------------------------------------------------------
│ │ │ AttributeErrorTraceback (most recent call last)
│ │ │ <ipython-input-46-cb8f083667be>in<module>
│ │ │ @@ -764,15 +764,15 @@
│ │ │ /build/python-xarray-zcUjoD/python-xarray-0.16.2/xarray/core/utils.py in drop_dims_from_indexers(indexers, dims, missing_dims)
│ │ │ 766invalid=indexers.keys()-set(dims)
│ │ │ 767ifinvalid:
│ │ │ --> 768raiseValueError(
│ │ │ 769f"dimensions {invalid} do not exist. Expected one or more of {dims}"
│ │ │ 770)
│ │ │
│ │ │ -ValueError: dimensions {'lat', 'lon'} do not exist. Expected one or more of Frozen(SortedKeysDict({'longitude': 50, 'latitude': 50}))
│ │ │ +ValueError: dimensions {'lon', 'lat'} do not exist. Expected one or more of Frozen(SortedKeysDict({'longitude': 50, 'latitude': 50}))
│ │ │
│ │ │ In [63]: dsi.air.plot(ax=axes[1])
│ │ │ ---------------------------------------------------------------------------
│ │ │ NameErrorTraceback (most recent call last)
│ │ │ <ipython-input-63-433e5c8b0d13>in<module>
│ │ │ ----> 1dsi.air.plot(ax=axes[1])
│ │ ├── ./usr/share/doc/python-xarray-doc/html/io.html
│ │ │ @@ -1057,15 +1057,15 @@
│ │ │ ....: "y":pd.date_range("2000-01-01",periods=5),
│ │ │ ....: "z":("x",list("abcd")),
│ │ │ ....: },
│ │ │ ....: )
│ │ │ ....:
│ │ │
│ │ │ In [39]: ds.to_zarr("path/to/directory.zarr")
│ │ │ -Out[39]: <xarray.backends.zarr.ZarrStore at 0x7f705cca5520>
│ │ │ +Out[39]: <xarray.backends.zarr.ZarrStore at 0x7f786c187940>
│ │ │
│ │ │
│ │ │
(The suffix .zarr is optional–just a reminder that a zarr store lives
│ │ │ there.) If the directory does not exist, it will be created. If a zarr
│ │ │ store is already present at that path, an error will be raised, preventing it
│ │ │ from being overwritten. To override this behavior and overwrite an existing
│ │ │ store, add mode='w' when invoking to_zarr().
│ │ │ @@ -1111,15 +1111,15 @@
│ │ │ These options can be passed to the to_zarr method as variable encoding.
│ │ │ For example:
│ │ │
In [42]: importzarr
│ │ │
│ │ │ In [43]: compressor=zarr.Blosc(cname="zstd",clevel=3,shuffle=2)
│ │ │
│ │ │ In [44]: ds.to_zarr("foo.zarr",encoding={"foo":{"compressor":compressor}})
│ │ │ -Out[44]: <xarray.backends.zarr.ZarrStore at 0x7f705cca56a0>
│ │ │ +Out[44]: <xarray.backends.zarr.ZarrStore at 0x7f786c397580>
│ │ │
│ │ │
│ │ │
│ │ │
Note
│ │ │
Not all native zarr compression and filtering options have been tested with
│ │ │ xarray.
Finally, you can use region to write to limited regions of existing arrays
│ │ │ in an existing Zarr store. This is a good option for writing data in parallel
│ │ │ from independent processes.
│ │ │
To scale this up to writing large datasets, the first step is creating an
│ │ │ initial Zarr store without writing all of its array data. This can be done by
│ │ │ @@ -1216,33 +1216,33 @@
│ │ │
│ │ │ In [51]: ds=xr.Dataset({"foo":("x",dummies)})
│ │ │
│ │ │ In [52]: path="path/to/directory.zarr"
│ │ │
│ │ │ # Now we write the metadata without computing any array values
│ │ │ In [53]: ds.to_zarr(path,compute=False,consolidated=True)
│ │ │ -Out[53]: Delayed('_finalize_store-1cf228a7-668a-4a9d-814a-cc1458d05c32')
│ │ │ +Out[53]: Delayed('_finalize_store-cc2bed2f-261c-4b61-88b7-125cc58608fb')
│ │ │
│ │ │
│ │ │
Now, a Zarr store with the correct variable shapes and attributes exists that
│ │ │ can be filled out by subsequent calls to to_zarr. The region provides a
│ │ │ mapping from dimension names to Python slice objects indicating where the
│ │ │ data should be written (in index space, not coordinate space), e.g.,
│ │ │
# For convenience, we'll slice a single dataset, but in the real use-case
│ │ │ # we would create them separately, possibly even from separate processes.
│ │ │ In [54]: ds=xr.Dataset({"foo":("x",np.arange(30))})
│ │ │
│ │ │ In [55]: ds.isel(x=slice(0,10)).to_zarr(path,region={"x":slice(0,10)})
│ │ │ -Out[55]: <xarray.backends.zarr.ZarrStore at 0x7f705cca5dc0>
│ │ │ +Out[55]: <xarray.backends.zarr.ZarrStore at 0x7f786c187fa0>
│ │ │
│ │ │ In [56]: ds.isel(x=slice(10,20)).to_zarr(path,region={"x":slice(10,20)})
│ │ │ -Out[56]: <xarray.backends.zarr.ZarrStore at 0x7f705c99e8e0>
│ │ │ +Out[56]: <xarray.backends.zarr.ZarrStore at 0x7f788431f460>
│ │ │
│ │ │ In [57]: ds.isel(x=slice(20,30)).to_zarr(path,region={"x":slice(20,30)})
│ │ │ -Out[57]: <xarray.backends.zarr.ZarrStore at 0x7f707160ef40>
│ │ │ +Out[57]: <xarray.backends.zarr.ZarrStore at 0x7f786fba5520>
│ │ │
│ │ │
│ │ │
Concurrent writes with region are safe as long as they modify distinct
│ │ │ chunks in the underlying Zarr arrays (or use an appropriate lock).
│ │ │
As a safety check to make it harder to inadvertently override existing values,
│ │ │ if you set region then all variables included in a Dataset must have
│ │ │ dimensions included in region. Other variables (typically coordinates)
│ │ ├── ./usr/share/doc/python-xarray-doc/html/plotting.html
│ │ │ @@ -324,15 +324,15 @@
│ │ │ /build/python-xarray-zcUjoD/python-xarray-0.16.2/xarray/tutorial.py in open_dataset(name, cache, cache_dir, github_url, branch, **kws)
│ │ │ 76# May want to add an option to remove it.
│ │ │ 77ifnot_os.path.isdir(longdir):
│ │ │ ---> 78_os.mkdir(longdir)
│ │ │ 79
│ │ │ 80url="/".join((github_url,"raw",branch,fullname))
│ │ │
│ │ │ -FileNotFoundError: [Errno 2] No such file or directory: '/sbuild-nonexistent/.xarray_tutorial_data'
│ │ │ +FileNotFoundError: [Errno 2] No such file or directory: '/nonexistent/.xarray_tutorial_data'
│ │ │
│ │ │ In [6]: airtemps
│ │ │ ---------------------------------------------------------------------------
│ │ │ NameErrorTraceback (most recent call last)
│ │ │ <ipython-input-6-eb57b540ddce>in<module>
│ │ │ ----> 1airtemps
│ │ │
│ │ │ @@ -850,15 +850,15 @@
│ │ │ --> 171ref_var=variables[ref_name]
│ │ │ 172
│ │ │ 173ifvar_nameisNone:
│ │ │
│ │ │ KeyError: 'lat'
│ │ │
│ │ │ In [51]: b.plot()
│ │ │ -Out[51]: [<matplotlib.lines.Line2D at 0x7f705c7f07f0>]
│ │ │ +Out[51]: [<matplotlib.lines.Line2D at 0x7f78384f5280>]
│ │ │
│ │ │
│ │ │
│ │ │
│ │ │
Since this is a thin wrapper around matplotlib, all the functionality of
│ │ │ @@ -1312,56 +1312,56 @@
│ │ │ Data variables:
│ │ │ A (x, y, z, w) float64 -0.104 0.02719 -0.0425 ... -0.1175 -0.0183
│ │ │ B (x, y, z, w) float64 0.0 0.0 0.0 0.0 ... 1.369 1.408 1.387 1.417
│ │ │
│ │ │
│ │ │
Suppose we want to scatter A against B
│ │ │
In [95]: ds.plot.scatter(x="A",y="B")
│ │ │ -Out[95]: <matplotlib.collections.PathCollection at 0x7f705d20d730>
│ │ │ +Out[95]: <matplotlib.collections.PathCollection at 0x7f786c2a3ac0>
│ │ │
│ │ │
│ │ │
│ │ │
The hue kwarg lets you vary the color by variable value
│ │ │
In [96]: ds.plot.scatter(x="A",y="B",hue="w")
│ │ │ Out[96]:
│ │ │ -[<matplotlib.collections.PathCollection at 0x7f705cf0aa30>,
│ │ │ - <matplotlib.collections.PathCollection at 0x7f705cf792e0>,
│ │ │ - <matplotlib.collections.PathCollection at 0x7f705cf50eb0>,
│ │ │ - <matplotlib.collections.PathCollection at 0x7f705d6f07c0>]
│ │ │ +[<matplotlib.collections.PathCollection at 0x7f786fc28640>,
│ │ │ + <matplotlib.collections.PathCollection at 0x7f786ff11d90>,
│ │ │ + <matplotlib.collections.PathCollection at 0x7f786fc3bd30>,
│ │ │ + <matplotlib.collections.PathCollection at 0x7f78586c4c70>]
│ │ │
│ │ │
│ │ │
│ │ │
When hue is specified, a colorbar is added for numeric hue DataArrays by
│ │ │ default and a legend is added for non-numeric hue DataArrays (as above).
│ │ │ You can force a legend instead of a colorbar by setting hue_style='discrete'.
│ │ │ Additionally, the boolean kwarg add_guide can be used to prevent the display of a legend or colorbar (as appropriate).
│ │ │
In [97]: ds=ds.assign(w=[1,2,3,5])
│ │ │
│ │ │ In [98]: ds.plot.scatter(x="A",y="B",hue="w",hue_style="discrete")
│ │ │ Out[98]:
│ │ │ -[<matplotlib.collections.PathCollection at 0x7f70719bad60>,
│ │ │ - <matplotlib.collections.PathCollection at 0x7f70716e6fa0>,
│ │ │ - <matplotlib.collections.PathCollection at 0x7f705cfeda60>,
│ │ │ - <matplotlib.collections.PathCollection at 0x7f705c6d4c40>]
│ │ │ +[<matplotlib.collections.PathCollection at 0x7f7858600760>,
│ │ │ + <matplotlib.collections.PathCollection at 0x7f785844aa00>,
│ │ │ + <matplotlib.collections.PathCollection at 0x7f78584317f0>,
│ │ │ + <matplotlib.collections.PathCollection at 0x7f78583b0340>]
│ │ │
│ │ │
│ │ │
│ │ │
The markersize kwarg lets you vary the point’s size by variable value. You can additionally pass size_norm to control how the variable’s values are mapped to point sizes.
│ │ │
In [99]: ds.plot.scatter(x="A",y="B",hue="z",hue_style="discrete",markersize="z")
│ │ │ Out[99]:
│ │ │ -[<matplotlib.collections.PathCollection at 0x7f705c692bb0>,
│ │ │ - <matplotlib.collections.PathCollection at 0x7f705c6a1d00>,
│ │ │ - <matplotlib.collections.PathCollection at 0x7f705c692e20>,
│ │ │ - <matplotlib.collections.PathCollection at 0x7f705c6a1ee0>]
│ │ │ +[<matplotlib.collections.PathCollection at 0x7f78384ceee0>,
│ │ │ + <matplotlib.collections.PathCollection at 0x7f78384cea90>,
│ │ │ + <matplotlib.collections.PathCollection at 0x7f786fb7f400>,
│ │ │ + <matplotlib.collections.PathCollection at 0x7f786fb700d0>]
│ │ │
│ │ │
│ │ │
│ │ │
Faceting is also possible
│ │ │
In [100]: ds.plot.scatter(x="A",y="B",col="x",row="z",hue="w",hue_style="discrete")
│ │ │ -Out[100]: <xarray.plot.facetgrid.FacetGrid at 0x7f705c6f99d0>
│ │ │ +Out[100]: <xarray.plot.facetgrid.FacetGrid at 0x7f786c3727f0>
│ │ │
│ │ │
│ │ │
│ │ │
For more advanced scatter plots, we recommend converting the relevant data variables to a pandas DataFrame and using the extensive plotting capabilities of seaborn.
│ │ │ @@ -1378,15 +1378,15 @@
│ │ │ /build/python-xarray-zcUjoD/python-xarray-0.16.2/xarray/tutorial.py in open_dataset(name, cache, cache_dir, github_url, branch, **kws)
│ │ │ 76# May want to add an option to remove it.
│ │ │ 77ifnot_os.path.isdir(longdir):
│ │ │ ---> 78_os.mkdir(longdir)
│ │ │ 79
│ │ │ 80url="/".join((github_url,"raw",branch,fullname))
│ │ │
│ │ │ -FileNotFoundError: [Errno 2] No such file or directory: '/sbuild-nonexistent/.xarray_tutorial_data'
│ │ │ +FileNotFoundError: [Errno 2] No such file or directory: '/nonexistent/.xarray_tutorial_data'
│ │ │
│ │ │ In [103]: p=air.isel(time=0).plot(
│ │ │ .....: subplot_kws=dict(projection=ccrs.Orthographic(-80,35),facecolor="gray"),
│ │ │ .....: transform=ccrs.PlateCarree(),
│ │ │ .....: )
│ │ │ .....:
│ │ │ ---------------------------------------------------------------------------
│ │ │ @@ -1470,24 +1470,24 @@
│ │ │
In [109]: importxarray.plotasxplt
│ │ │
│ │ │ In [110]: da=xr.DataArray(range(5))
│ │ │
│ │ │ In [111]: fig,axes=plt.subplots(ncols=2,nrows=2)
│ │ │
│ │ │ In [112]: da.plot(ax=axes[0,0])
│ │ │ -Out[112]: [<matplotlib.lines.Line2D at 0x7f705c37f160>]
│ │ │ +Out[112]: [<matplotlib.lines.Line2D at 0x7f7838209c40>]
│ │ │
│ │ │ In [113]: da.plot.line(ax=axes[0,1])
│ │ │ -Out[113]: [<matplotlib.lines.Line2D at 0x7f705c3669a0>]
│ │ │ +Out[113]: [<matplotlib.lines.Line2D at 0x7f7838209fd0>]
│ │ │
│ │ │ In [114]: xplt.plot(da,ax=axes[1,0])
│ │ │ -Out[114]: [<matplotlib.lines.Line2D at 0x7f705c3666d0>]
│ │ │ +Out[114]: [<matplotlib.lines.Line2D at 0x7f785840e9d0>]
│ │ │
│ │ │ In [115]: xplt.line(da,ax=axes[1,1])
│ │ │ -Out[115]: [<matplotlib.lines.Line2D at 0x7f705c37fbe0>]
│ │ │ +Out[115]: [<matplotlib.lines.Line2D at 0x7f7838200ee0>]
│ │ │
│ │ │ In [116]: plt.tight_layout()
│ │ │
│ │ │ In [117]: plt.draw()
│ │ │
│ │ │
│ │ │
│ │ │ @@ -1540,15 +1540,15 @@
│ │ │
│ │ │
The plot will produce an image corresponding to the values of the array.
│ │ │ Hence the top left pixel will be a different color than the others.
│ │ │ Before reading on, you may want to look at the coordinates and
│ │ │ think carefully about what the limits, labels, and orientation for
│ │ │ each of the axes should be.
│ │ │
In [122]: a.plot()
│ │ │ -Out[122]: <matplotlib.collections.QuadMesh at 0x7f705d1b7670>
│ │ │ +Out[122]: <matplotlib.collections.QuadMesh at 0x7f7838275d90>
│ │ │
│ │ │
│ │ │
│ │ │
It may seem strange that
│ │ │ the values on the y axis are decreasing with -0.5 on the top. This is because
│ │ │ the pixels are centered over their coordinates, and the
│ │ │ axis labels and ranges correspond to the values of the
│ │ │ @@ -1570,81 +1570,81 @@
│ │ │ .....: np.arange(20).reshape(4,5),
│ │ │ .....: dims=["y","x"],
│ │ │ .....: coords={"lat":(("y","x"),lat),"lon":(("y","x"),lon)},
│ │ │ .....: )
│ │ │ .....:
│ │ │
│ │ │ In [127]: da.plot.pcolormesh("lon","lat")
│ │ │ -Out[127]: <matplotlib.collections.QuadMesh at 0x7f705c56e820>
│ │ │ +Out[127]: <matplotlib.collections.QuadMesh at 0x7f781877b850>
│ │ │
│ │ │
│ │ │
│ │ │
Note that in this case, xarray still follows the pixel centered convention.
│ │ │ This might be undesirable in some cases, for example when your data is defined
│ │ │ on a polar projection (GH781). This is why the default is to not follow
│ │ │ this convention when plotting on a map:
│ │ │
In [128]: importcartopy.crsasccrs
│ │ │
│ │ │ In [129]: ax=plt.subplot(projection=ccrs.PlateCarree())
│ │ │
│ │ │ In [130]: da.plot.pcolormesh("lon","lat",ax=ax)
│ │ │ -Out[130]: <matplotlib.collections.QuadMesh at 0x7f705c272250>
│ │ │ +Out[130]: <matplotlib.collections.QuadMesh at 0x7f78187bb610>
│ │ │
│ │ │ In [131]: ax.scatter(lon,lat,transform=ccrs.PlateCarree())
│ │ │ -Out[131]: <matplotlib.collections.PathCollection at 0x7f705c277be0>
│ │ │ +Out[131]: <matplotlib.collections.PathCollection at 0x7f78186f28b0>
│ │ │
│ │ │ In [132]: ax.coastlines()
│ │ │ -Out[132]: <cartopy.mpl.feature_artist.FeatureArtist at 0x7f705c6f9f70>
│ │ │ +Out[132]: <cartopy.mpl.feature_artist.FeatureArtist at 0x7f783842abe0>
│ │ │
│ │ │ In [133]: ax.gridlines(draw_labels=True)
│ │ │ -Out[133]: <cartopy.mpl.gridliner.Gridliner at 0x7f705c225310>
│ │ │ +Out[133]: <cartopy.mpl.gridliner.Gridliner at 0x7f78186f2a00>
│ │ │
│ │ │
│ │ │
│ │ │
You can however decide to infer the cell boundaries and use the
│ │ │ infer_intervals keyword:
│ │ │
In [134]: ax=plt.subplot(projection=ccrs.PlateCarree())
│ │ │
│ │ │ In [135]: da.plot.pcolormesh("lon","lat",ax=ax,infer_intervals=True)
│ │ │ -Out[135]: <matplotlib.collections.QuadMesh at 0x7f705c625250>
│ │ │ +Out[135]: <matplotlib.collections.QuadMesh at 0x7f78184e4ca0>
│ │ │
│ │ │ In [136]: ax.scatter(lon,lat,transform=ccrs.PlateCarree())
│ │ │ -Out[136]: <matplotlib.collections.PathCollection at 0x7f705c5e8730>
│ │ │ +Out[136]: <matplotlib.collections.PathCollection at 0x7f78184969a0>
│ │ │
│ │ │ In [137]: ax.coastlines()
│ │ │ -Out[137]: <cartopy.mpl.feature_artist.FeatureArtist at 0x7f705c229910>
│ │ │ +Out[137]: <cartopy.mpl.feature_artist.FeatureArtist at 0x7f7818698cd0>
│ │ │
│ │ │ In [138]: ax.gridlines(draw_labels=True)
│ │ │ -Out[138]: <cartopy.mpl.gridliner.Gridliner at 0x7f705c229580>
│ │ │ +Out[138]: <cartopy.mpl.gridliner.Gridliner at 0x7f78186986a0>
│ │ │
│ │ │
│ │ │
│ │ │
│ │ │
Note
│ │ │
The data model of xarray does not support datasets with cell boundaries
│ │ │ yet. If you want to use these coordinates, you’ll have to make the plots
│ │ │ outside the xarray framework.
│ │ │
│ │ │
One can also make line plots with multidimensional coordinates. In this case, hue must be a dimension name, not a coordinate name.
│ │ │
In [139]: f,ax=plt.subplots(2,1)
│ │ │
│ │ │ In [140]: da.plot.line(x="lon",hue="y",ax=ax[0])
│ │ │ Out[140]:
│ │ │ -[<matplotlib.lines.Line2D at 0x7f704fef87f0>,
│ │ │ - <matplotlib.lines.Line2D at 0x7f704fef8850>,
│ │ │ - <matplotlib.lines.Line2D at 0x7f704fef8910>,
│ │ │ - <matplotlib.lines.Line2D at 0x7f704fef8a90>]
│ │ │ +[<matplotlib.lines.Line2D at 0x7f7838440400>,
│ │ │ + <matplotlib.lines.Line2D at 0x7f78384405b0>,
│ │ │ + <matplotlib.lines.Line2D at 0x7f78187d1eb0>,
│ │ │ + <matplotlib.lines.Line2D at 0x7f78187d15e0>]
│ │ │
│ │ │ In [141]: da.plot.line(x="lon",hue="x",ax=ax[1])
│ │ │ Out[141]:
│ │ │ -[<matplotlib.lines.Line2D at 0x7f704fe99100>,
│ │ │ - <matplotlib.lines.Line2D at 0x7f704fe99220>,
│ │ │ - <matplotlib.lines.Line2D at 0x7f704fe99280>,
│ │ │ - <matplotlib.lines.Line2D at 0x7f704fe993d0>,
│ │ │ - <matplotlib.lines.Line2D at 0x7f704fe99490>]
│ │ │ +[<matplotlib.lines.Line2D at 0x7f7818415a60>,
│ │ │ + <matplotlib.lines.Line2D at 0x7f7818415a00>,
│ │ │ + <matplotlib.lines.Line2D at 0x7f7818415ac0>,
│ │ │ + <matplotlib.lines.Line2D at 0x7f78184314c0>,
│ │ │ + <matplotlib.lines.Line2D at 0x7f78184312e0>]
│ │ │
Visualizing your datasets is quick and convenient:
│ │ │
In [37]: data.plot()
│ │ │ -Out[37]: <matplotlib.collections.QuadMesh at 0x7f704fdeffd0>
│ │ │ +Out[37]: <matplotlib.collections.QuadMesh at 0x7f781830ff70>
│ │ │
│ │ │
│ │ │
│ │ │
Note the automatic labeling with names and units. Our effort in adding metadata attributes has paid off! Many aspects of these figures are customizable: see Plotting.