
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/lines_bars_and_markers/masked_demo.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_lines_bars_and_markers_masked_demo.py>`
        to download the full example code.

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

.. _sphx_glr_gallery_lines_bars_and_markers_masked_demo.py:


==============================
Plotting masked and NaN values
==============================

Sometimes you need to plot data with missing values.

One possibility is to simply remove undesired data points. The line plotted
through the remaining data will be continuous, and not indicate where the
missing data is located.

If it is useful to have gaps in the line where the data is missing, then the
undesired points can be indicated using a `masked array`_ or by setting their
values to NaN. No marker will be drawn where either x or y are masked and, if
plotting with a line, it will be broken there.

.. _masked array:
   https://numpy.org/doc/stable/reference/maskedarray.generic.html

The following example illustrates the three cases:

1) Removing points.
2) Masking points.
3) Setting to NaN.

.. GENERATED FROM PYTHON SOURCE LINES 26-52

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    x = np.linspace(-np.pi/2, np.pi/2, 31)
    y = np.cos(x)**3

    # 1) remove points where y > 0.7
    x2 = x[y <= 0.7]
    y2 = y[y <= 0.7]

    # 2) mask points where y > 0.7
    y3 = np.ma.masked_where(y > 0.7, y)

    # 3) set to NaN where y > 0.7
    y4 = y.copy()
    y4[y3 > 0.7] = np.nan

    plt.plot(x*0.1, y, 'o-', color='lightgrey', label='No mask')
    plt.plot(x2*0.4, y2, 'o-', label='Points removed')
    plt.plot(x*0.7, y3, 'o-', label='Masked values')
    plt.plot(x*1.0, y4, 'o-', label='NaN values')
    plt.legend()
    plt.title('Masked and NaN data')
    plt.show()




.. image-sg:: /gallery/lines_bars_and_markers/images/sphx_glr_masked_demo_001.png
   :alt: Masked and NaN data
   :srcset: /gallery/lines_bars_and_markers/images/sphx_glr_masked_demo_001.png, /gallery/lines_bars_and_markers/images/sphx_glr_masked_demo_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 53-57

.. tags::

   plot-type: line
   level: intermediate


.. _sphx_glr_download_gallery_lines_bars_and_markers_masked_demo.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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