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

.. only:: html

    .. meta::
        :keywords: codex

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

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

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

.. _sphx_glr_gallery_images_contours_and_fields_tricontour_smooth_user.py:


======================
Tricontour Smooth User
======================

Demonstrates high-resolution tricontouring on user-defined triangular grids
with `matplotlib.tri.UniformTriRefiner`.

.. GENERATED FROM PYTHON SOURCE LINES 9-78

.. code-block:: Python

    import matplotlib.pyplot as plt
    import numpy as np

    import matplotlib.tri as tri


    # ----------------------------------------------------------------------------
    # Analytical test function
    # ----------------------------------------------------------------------------
    def function_z(x, y):
        r1 = np.sqrt((0.5 - x)**2 + (0.5 - y)**2)
        theta1 = np.arctan2(0.5 - x, 0.5 - y)
        r2 = np.sqrt((-x - 0.2)**2 + (-y - 0.2)**2)
        theta2 = np.arctan2(-x - 0.2, -y - 0.2)
        z = -(2 * (np.exp((r1 / 10)**2) - 1) * 30. * np.cos(7. * theta1) +
              (np.exp((r2 / 10)**2) - 1) * 30. * np.cos(11. * theta2) +
              0.7 * (x**2 + y**2))
        return (np.max(z) - z) / (np.max(z) - np.min(z))

    # ----------------------------------------------------------------------------
    # Creating a Triangulation
    # ----------------------------------------------------------------------------
    # First create the x and y coordinates of the points.
    n_angles = 20
    n_radii = 10
    min_radius = 0.15
    radii = np.linspace(min_radius, 0.95, n_radii)

    angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
    angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
    angles[:, 1::2] += np.pi / n_angles

    x = (radii * np.cos(angles)).flatten()
    y = (radii * np.sin(angles)).flatten()
    z = function_z(x, y)

    # Now create the Triangulation.
    # (Creating a Triangulation without specifying the triangles results in the
    # Delaunay triangulation of the points.)
    triang = tri.Triangulation(x, y)

    # Mask off unwanted triangles.
    triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
                             y[triang.triangles].mean(axis=1))
                    < min_radius)

    # ----------------------------------------------------------------------------
    # Refine data
    # ----------------------------------------------------------------------------
    refiner = tri.UniformTriRefiner(triang)
    tri_refi, z_test_refi = refiner.refine_field(z, subdiv=3)

    # ----------------------------------------------------------------------------
    # Plot the triangulation and the high-res iso-contours
    # ----------------------------------------------------------------------------
    fig, ax = plt.subplots()
    ax.set_aspect('equal')
    ax.triplot(triang, lw=0.5, color='white')

    levels = np.arange(0., 1., 0.025)
    ax.tricontourf(tri_refi, z_test_refi, levels=levels, cmap='terrain')
    ax.tricontour(tri_refi, z_test_refi, levels=levels,
                  colors=['0.25', '0.5', '0.5', '0.5', '0.5'],
                  linewidths=[1.0, 0.5, 0.5, 0.5, 0.5])

    ax.set_title("High-resolution tricontouring")

    plt.show()




.. image-sg:: /gallery/images_contours_and_fields/images/sphx_glr_tricontour_smooth_user_001.png
   :alt: High-resolution tricontouring
   :srcset: /gallery/images_contours_and_fields/images/sphx_glr_tricontour_smooth_user_001.png, /gallery/images_contours_and_fields/images/sphx_glr_tricontour_smooth_user_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 79-89

.. admonition:: References

   The use of the following functions, methods, classes and modules is shown
   in this example:

   - `matplotlib.axes.Axes.tricontour` / `matplotlib.pyplot.tricontour`
   - `matplotlib.axes.Axes.tricontourf` / `matplotlib.pyplot.tricontourf`
   - `matplotlib.tri`
   - `matplotlib.tri.Triangulation`
   - `matplotlib.tri.UniformTriRefiner`


.. _sphx_glr_download_gallery_images_contours_and_fields_tricontour_smooth_user.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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