
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/XY/NCL_xy_10.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_XY_NCL_xy_10.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_XY_NCL_xy_10.py:


NCL_xy_10.py
===============
This script illustrates the following concepts:
   - Filling the area between two curves in an XY plot
   - Drawing Greek characters on an XY plot

See following URLs to see the reproduced NCL plot & script:
    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/xy_10.ncl
    - Original NCL plots: https://www.ncl.ucar.edu/Applications/Images/xy_10_lg.png

.. GENERATED FROM PYTHON SOURCE LINES 14-15

Import packages:

.. GENERATED FROM PYTHON SOURCE LINES 15-24

.. code-block:: Python


    import numpy as np
    import xarray as xr
    import matplotlib.pyplot as plt
    import math

    import geocat.datafiles as gdf
    import geocat.viz as gv








.. GENERATED FROM PYTHON SOURCE LINES 25-26

Read in data:

.. GENERATED FROM PYTHON SOURCE LINES 26-32

.. code-block:: Python


    # Open a netCDF data file using xarray default engine and load the data into xarrays
    ds = xr.open_dataset(gdf.get("netcdf_files/80.nc"))
    # Extract slice of data
    TS = ds.isel(time=0, lon=21, drop=True).TS








.. GENERATED FROM PYTHON SOURCE LINES 33-34

Define bounds for region centered on the data with a width of 2 sigma

.. GENERATED FROM PYTHON SOURCE LINES 34-43

.. code-block:: Python

    nlat = np.shape(TS.lat)[0]
    top = np.empty(nlat)
    bottom = np.empty(nlat)

    for k in range(0, nlat):
        dx = math.sqrt(TS[k])
        top[k] = TS[k] + dx
        bottom[k] = TS[k] - dx








.. GENERATED FROM PYTHON SOURCE LINES 44-45

Plot:

.. GENERATED FROM PYTHON SOURCE LINES 45-80

.. code-block:: Python

    plt.figure(figsize=(8, 8))
    ax = plt.axes()

    # Plot data
    TS.plot.line(ax=ax, color='black', _labels=False)

    # Plot curves that bound the region to be colored
    plt.plot(TS.lat, top, color='SlateBlue')
    plt.plot(TS.lat, bottom, color='SlateBlue')

    # Fill the area between the bounds
    ax.fill_between(TS.lat, top, bottom, color='SlateBlue')

    # Use geocat.viz.util convenience function to add minor and major tick lines
    gv.add_major_minor_ticks(ax, x_minor_per_major=3, y_minor_per_major=4, labelsize=14)

    # Use geocat.viz.util convenience function to set axes parameters
    gv.set_axes_limits_and_ticks(
        ax,
        ylim=(220, 320),
        xlim=(-90, 90),
        xticks=np.arange(-90, 91, 30),
        xticklabels=['90S', '60S', '30S', '0', '30N', '60N', '90N'],
    )

    # Use geocat.viz.util convenience function to set titles and labels
    gv.set_titles_and_labels(
        ax,
        maintitle=r"A Title with $\eta\epsilon\lambda\alpha\sigma$ Characters",
        ylabel=TS.long_name,
    )

    # Draw sigma on axes
    ax.text(0.15, 0.15, r"$\sigma$", fontsize=40, transform=ax.transAxes)
    plt.show()



.. image-sg:: /gallery/XY/images/sphx_glr_NCL_xy_10_001.png
   :alt: A Title with $\eta\epsilon\lambda\alpha\sigma$ Characters
   :srcset: /gallery/XY/images/sphx_glr_NCL_xy_10_001.png
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.155 seconds)


.. _sphx_glr_download_gallery_XY_NCL_xy_10.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: NCL_xy_10.ipynb <NCL_xy_10.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: NCL_xy_10.py <NCL_xy_10.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: NCL_xy_10.zip <NCL_xy_10.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
