
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/03-projectiles_and_scaling/3.3-plot_projectile_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_03-projectiles_and_scaling_3.3-plot_projectile_random_location.py>`
        to download the full example code.

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

.. _sphx_glr_auto_examples_03-projectiles_and_scaling_3.3-plot_projectile_random_location.py:


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

.. rubric:: By David Minton and Dennise Valadez

This example demonstrates the generation of random impact locations.

.. GENERATED FROM PYTHON SOURCE LINES 10-84



.. image-sg:: /auto_examples/03-projectiles_and_scaling/images/sphx_glr_3.3-plot_projectile_random_location_001.png
   :alt: Number vs Longitude, Number vs Latitude
   :srcset: /auto_examples/03-projectiles_and_scaling/images/sphx_glr_3.3-plot_projectile_random_location_001.png
   :class: sphx-glr-single-img





.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    from cratermaker import Projectile

    # Create a projectile
    proj = Projectile(mean_velocity=5000, density=3000)

    # Using projectile for sampling data
    size = 10000
    locations = [proj.new_projectile().location for _ in range(size)]

    # Obtain longitudes and latitudes from projectile - but this is not accurate and doesnt work
    lons = np.array([loc[0] for loc in locations])
    lats = np.array([loc[1] for loc in locations])

    # Bins
    bins = 50

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

    # Latitude histogram
    observed_counts_lat, bins_lat_deg = np.histogram(lats, bins=bins, range=(-90, 90))
    bins_lat = np.deg2rad(bins_lat_deg)
    area_ratio = np.sin(bins_lat[1:]) - np.sin(bins_lat[:-1])
    total_area = np.sin(np.pi / 2) - np.sin(-np.pi / 2)
    expected_count_lat = size * area_ratio / total_area

    # Bar widths
    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 plot
    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 3.379 seconds)


.. _sphx_glr_download_auto_examples_03-projectiles_and_scaling_3.3-plot_projectile_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: 3.3-plot_projectile_random_location.ipynb <3.3-plot_projectile_random_location.ipynb>`

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

      :download:`Download Python source code: 3.3-plot_projectile_random_location.py <3.3-plot_projectile_random_location.py>`

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

      :download:`Download zipped: 3.3-plot_projectile_random_location.zip <3.3-plot_projectile_random_location.zip>`


.. only:: html

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

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