
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/02-production/2.6-plot_random_location.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

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

        :ref:`Go to the end <sphx_glr_download_auto_examples_02-production_2.6-plot_random_location.py>`
        to download the full example code.

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

.. _sphx_glr_auto_examples_02-production_2.6-plot_random_location.py:


Plot a distribution of random locations
=======================================

.. rubric:: By David Minton

This example demonstrates the generation of random impact locations.

.. GENERATED FROM PYTHON SOURCE LINES 10-84



.. image-sg:: /auto_examples/02-production/images/sphx_glr_2.6-plot_random_location_001.png
   :alt: Number vs Longitude, Number vs Latitude
   :srcset: /auto_examples/02-production/images/sphx_glr_2.6-plot_random_location_001.png
   :class: sphx-glr-single-img





.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    from cratermaker.utils.montecarlo_utils import get_random_location

    # Sample data generation
    size = 10000
    points = get_random_location(size=size)

    lons = points["lon"]
    lats = points["lat"]

    # Number of bins
    bins = 50

    # Longitude plot
    observed_counts_lon, bins_lon_deg = np.histogram(lons, bins=bins, range=(-180, 180.0))
    expected_count_lon = size // bins

    # Latitude plot
    observed_counts_lat, bins_lat_deg = np.histogram(lats, bins=bins, range=(-90, 90))
    # Convert bins to degrees
    bins_lon = np.deg2rad(bins_lon_deg)
    bins_lat = np.deg2rad(bins_lat_deg)

    # For expected counts in latitude, adjust for area covered by each bin
    area_ratio = np.sin(bins_lat[1:]) - np.sin(bins_lat[:-1])
    total_area = np.sin(np.pi / 2) - np.sin(-np.pi / 2)  # Total area for the entire sphere
    expected_count_lat = size * area_ratio / total_area

    # Bar width in degrees
    bar_width_lon = np.diff(bins_lon_deg)
    bar_width_lat = np.diff(bins_lat_deg)

    # Plotting
    fig, axs = plt.subplots(2, 1, figsize=(8, 6))

    # Longitude plot
    axs[0].bar(
        bins_lon_deg[:-1],
        observed_counts_lon,
        width=bar_width_lon,
        align="edge",
        label="Observed",
    )
    axs[0].plot(
        bins_lon_deg[:-1],
        [expected_count_lon] * len(bins_lon_deg[:-1]),
        color="red",
        label="Expected",
    )
    axs[0].set_xlabel("Longitude (deg)")
    axs[0].set_ylabel("Number")
    axs[0].legend()
    axs[0].set_title("Number vs Longitude")

    # Latitude
    axs[1].bar(
        bins_lat_deg[:-1],
        observed_counts_lat,
        width=bar_width_lat,
        align="edge",
        alpha=0.5,
        label="Observed",
    )
    axs[1].plot(bins_lat_deg[:-1], expected_count_lat, label="Expected", color="red")
    axs[1].set_xlabel("Latitude (deg)")
    axs[1].set_ylabel("Number")
    axs[1].legend()
    axs[1].set_title("Number vs Latitude")

    plt.tight_layout()
    plt.show()


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

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


.. _sphx_glr_download_auto_examples_02-production_2.6-plot_random_location.py:

.. only:: html

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

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

      :download:`Download Jupyter notebook: 2.6-plot_random_location.ipynb <2.6-plot_random_location.ipynb>`

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

      :download:`Download Python source code: 2.6-plot_random_location.py <2.6-plot_random_location.py>`

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

      :download:`Download zipped: 2.6-plot_random_location.zip <2.6-plot_random_location.zip>`


.. only:: html

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

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