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

.. only:: html

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

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

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

.. _sphx_glr_gallery_Polygons_NCL_polyg_8_lbar.py:


NCL_polyg_8_lbar.py
===================
This script illustrates the following concepts:
   - Drawing a scatter plot on a map
   - Changing the marker color and size in a map plot
   - Plotting station locations using markers
   - Creating a custom color bar
   - Adding text to a plot
   - Generating dummy data using "random_uniform"
   - Binning data

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

.. GENERATED FROM PYTHON SOURCE LINES 19-20

Import packages:

.. GENERATED FROM PYTHON SOURCE LINES 20-30

.. code-block:: Python


    import numpy as np
    import cartopy.crs as ccrs
    import cartopy.feature as cfeature
    import matplotlib.pyplot as plt
    from matplotlib import colors, cm
    import cmaps

    import geocat.viz as gv








.. GENERATED FROM PYTHON SOURCE LINES 31-32

Generate dummy data

.. GENERATED FROM PYTHON SOURCE LINES 32-42

.. code-block:: Python

    npts = 100
    random = np.random.default_rng(seed=1)

    # Create random coordinates to position the markers
    lat = random.uniform(low=25, high=50, size=npts)
    lon = random.uniform(low=-125, high=-70, size=npts)

    # Create random data which the color will be based off of
    r = random.uniform(low=-1.2, high=35, size=npts)








.. GENERATED FROM PYTHON SOURCE LINES 43-44

Specify bins and sizes and create custom mappable based on NCV_jet colormap

.. GENERATED FROM PYTHON SOURCE LINES 44-60

.. code-block:: Python

    bins = [0, 5, 10, 15, 20, 23, 26]
    cmap = cmaps.NCV_jet

    # Create the boundaries for your data, this may be larger than bins to
    # accommodate colors for data outside of the smallest and largest bins
    boundaries = [-1.2, 0, 5, 10, 15, 20, 23, 26, 35]
    norm = colors.BoundaryNorm(boundaries, cmap.N)
    mappable = cm.ScalarMappable(norm=norm, cmap=cmap)

    # Retrieve the list of colors to use for the markers
    marker_colors = mappable.to_rgba(boundaries)

    # Increasing sizes for the markers in each bin, by using numpy.geomspace the
    # size differences are more noticeable
    sizes = np.geomspace(10, 250, len(boundaries))








.. GENERATED FROM PYTHON SOURCE LINES 61-62

Plot:

.. GENERATED FROM PYTHON SOURCE LINES 62-122

.. code-block:: Python

    plt.figure(figsize=(9, 6))
    projection = ccrs.PlateCarree()
    ax = plt.axes(projection=projection)
    ax.set_extent([-125, -70, 25, 50], crs=projection)

    # Draw land
    ax.add_feature(cfeature.LAND, color='silver', zorder=0)
    ax.add_feature(cfeature.LAKES, color='white', zorder=0)

    # Use geocat.viz.util convenience function to set axes tick values
    gv.set_axes_limits_and_ticks(
        ax, xticks=np.linspace(-120, -70, 6), yticks=np.linspace(25, 50, 6)
    )

    # Use geocat.viz.util convenience function to make latitude and longitude tick
    # labels
    gv.add_lat_lon_ticklabels(ax)

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

    # Remove ticks on the top and right sides of the plot
    ax.tick_params(axis='both', which='both', top=False, right=False)

    # Use geocat.viz.util convenience function to add titles
    gv.set_titles_and_labels(
        ax,
        maintitlefontsize=16,
        maintitle="Dummy station data colored and\nsized according to range of values",
    )

    # Plot markers with values less than first bin value
    masked_lon = np.where(r < bins[0], lon, np.nan)
    masked_lat = np.where(r < bins[0], lat, np.nan)
    plt.scatter(masked_lon, masked_lat, s=sizes[0], color=marker_colors[0], zorder=1)

    # Plot all other markers but those in the last bin
    for x in range(1, len(bins)):
        masked_lon = np.where(bins[x - 1] <= r, lon, np.nan)
        masked_lon = np.where(r < bins[x], masked_lon, np.nan)
        masked_lat = np.where(bins[x - 1] <= r, lat, np.nan)
        masked_lat = np.where(r < bins[x], masked_lat, np.nan)
        plt.scatter(masked_lon, masked_lat, s=sizes[x], color=marker_colors[x], zorder=1)

    # Plot markers with values greater than or equal to last bin value
    masked_lon = np.where(r >= bins[-1], lon, np.nan)
    masked_lat = np.where(r >= bins[-1], lat, np.nan)
    plt.scatter(masked_lon, masked_lat, s=sizes[-1], color=marker_colors[-1], zorder=1)

    # Create colorbar
    plt.colorbar(
        mappable=mappable,
        ax=ax,
        orientation='horizontal',
        drawedges=True,
        format='%.2f',
        ticks=bins,
    )

    plt.show()



.. image-sg:: /gallery/Polygons/images/sphx_glr_NCL_polyg_8_lbar_001.png
   :alt: Dummy station data colored and sized according to range of values
   :srcset: /gallery/Polygons/images/sphx_glr_NCL_polyg_8_lbar_001.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_gallery_Polygons_NCL_polyg_8_lbar.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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