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

.. only:: html

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

        Click :ref:`here <sphx_glr_download__gallery_plot_texture.py>`
        to download the full example code

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

.. _sphx_glr__gallery_plot_texture.py:


Texture triangle
================

Associate name of texture to zone in clay, sand, silt triangle.

.. GENERATED FROM PYTHON SOURCE LINES 7-54



.. image:: /_gallery/images/sphx_glr_plot_texture_001.png
    :alt: plot texture
    :class: sphx-glr-single-img





.. code-block:: default

    from math import cos, radians

    import matplotlib.cm as cm
    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    from soil.texture import texture_name

    # compute texture triangle
    records = []
    for clay in np.linspace(0, 1, 100):
        for sand in np.linspace(0, 1, 100):
            if clay + sand <= 1:
                name = texture_name(clay, sand)
                records.append(dict(
                    name=name,
                    clay=clay * 100,
                    sand=sand * 100
                ))

    df = pd.DataFrame(records)

    # convert tex coords to cartesian triangle
    df['x'] = 100 - df['sand'] - df['clay'] * cos(radians(60))
    df['y'] = df['clay'] * cos(radians(30))

    # plot texture triangle
    cmap = cm.get_cmap('tab20')

    fig, axes = plt.subplots(1, 1, figsize=(8, 8), squeeze=False)

    ax = axes[0, 0]
    for i, (name, sdf) in enumerate(df.groupby(by='name')):
        ax.plot(sdf['x'], sdf['y'], 'o', color=cmap(i), markersize=4)
        ax.text(sdf['x'].mean(), sdf['y'].mean(), name, ha='center', va='center')

    for sand in range(0, 101, 20):
        ax.text(100 - sand, -1, f"{sand:d}", ha='center', va='top')

    for clay in range(0, 101, 20):
        ax.text(clay * cos(radians(60)) - 1, clay * cos(radians(30)), f"{clay:d}", ha='right', va='center')

    ax.set_xticks([])
    ax.set_yticks([])

    fig.tight_layout()
    plt.show()


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

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


.. _sphx_glr_download__gallery_plot_texture.py:


.. only :: html

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



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

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



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

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


.. only:: html

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

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