
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/XY/NCL_xy_3.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_3.py>`
        to download the full example code.

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

.. _sphx_glr_gallery_XY_NCL_xy_3.py:


NCL_xy_3.py
===============
This script illustrates the following concepts:
   - Reversing the Y axis
   - Changing the line dash pattern in an XY plot
   - Creating your own line dash pattern for an XY plot
   - Changing the line color and thickness in an XY plot
   - Creating a vertical profile plot

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

.. GENERATED FROM PYTHON SOURCE LINES 17-18

Import packages:

.. GENERATED FROM PYTHON SOURCE LINES 18-26

.. code-block:: Python


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

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








.. GENERATED FROM PYTHON SOURCE LINES 27-28

Read in data:

.. GENERATED FROM PYTHON SOURCE LINES 28-36

.. 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/atmos.nc"), decode_times=False)
    ds = ds.U
    ds = ds.isel(time=0, drop=True)
    ds = ds.isel(lon=0, drop=True)
    ds = ds.isel(lat=42, drop=True)








.. GENERATED FROM PYTHON SOURCE LINES 37-38

Plot:

.. GENERATED FROM PYTHON SOURCE LINES 38-59

.. code-block:: Python


    # Generate figure (set its size (width, height) in inches) and axes
    plt.figure(figsize=(8, 8))
    ax = plt.gca()

    # Plot data
    plt.plot(ds.data, ds.lev)

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

    # Use geocat.viz.util convenience function to set axes parameters
    gv.set_axes_limits_and_ticks(ax, ylim=(1000, 0), xticks=np.arange(-10, 30, 5))

    # Use geocat.viz.util convenience function to set titles and labels
    gv.set_titles_and_labels(
        ax, maintitle="Profile Plot", xlabel=ds.long_name, ylabel=ds['lev'].long_name
    )

    plt.show()




.. image-sg:: /gallery/XY/images/sphx_glr_NCL_xy_3_001.png
   :alt: Profile Plot
   :srcset: /gallery/XY/images/sphx_glr_NCL_xy_3_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 60-61

Plot:

.. GENERATED FROM PYTHON SOURCE LINES 61-95

.. code-block:: Python


    # Generate figure (set its size (width, height) in inches) and axes
    plt.figure(figsize=(8, 8))
    ax = plt.gca()

    # Plot data with custom line characterisitcs
    # Use keyword `color` to change the line color
    # Use keyword `linewidth` to change the line thickness
    # Use keyword `dashes` to create a custom dash pattern
    # Use keyword `dash_capstyle` to change the shape of the dash end
    plt.plot(
        ds.data,
        ds.lev,
        color='red',
        linewidth=3,
        dashes=[3, 2, 1, 2, 1, 2, 1, 2],
        dash_capstyle='round',
    )

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

    # Use geocat.viz.util convenience function to set axes parameters
    gv.set_axes_limits_and_ticks(ax, ylim=(1000, 0), xticks=np.arange(-10, 30, 5))

    # Use geocat.viz.util convenience function to set titles and labels
    gv.set_titles_and_labels(
        ax,
        maintitle="Make your own dash pattern",
        xlabel=ds.long_name,
        ylabel=ds['lev'].long_name,
    )

    plt.show()



.. image-sg:: /gallery/XY/images/sphx_glr_NCL_xy_3_002.png
   :alt: Make your own dash pattern
   :srcset: /gallery/XY/images/sphx_glr_NCL_xy_3_002.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_gallery_XY_NCL_xy_3.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_3.ipynb <NCL_xy_3.ipynb>`

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

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

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

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


.. only:: html

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

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