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

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

.. _sphx_glr_gallery_shapes_and_collections_line_collection.py:


=============================================
Plotting multiple lines with a LineCollection
=============================================

Matplotlib can efficiently draw multiple lines at once using a
`~.LineCollection`, as showcased below.

.. GENERATED FROM PYTHON SOURCE LINES 9-45

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    from matplotlib.collections import LineCollection

    x = np.arange(100)
    # Here are many sets of y to plot vs. x
    ys = x[:50, np.newaxis] + x[np.newaxis, :]

    segs = np.zeros((50, 100, 2))
    segs[:, :, 1] = ys
    segs[:, :, 0] = x

    # Mask some values to test masked array support:
    segs = np.ma.masked_where((segs > 50) & (segs < 60), segs)

    # We need to set the plot limits, they will not autoscale
    fig, ax = plt.subplots()
    ax.set_xlim(x.min(), x.max())
    ax.set_ylim(ys.min(), ys.max())

    # *colors* is sequence of rgba tuples.
    # *linestyle* is a string or dash tuple. Legal string values are
    # solid|dashed|dashdot|dotted.  The dash tuple is (offset, onoffseq) where
    # onoffseq is an even length tuple of on and off ink in points.  If linestyle
    # is omitted, 'solid' is used.
    # See `matplotlib.collections.LineCollection` for more information.
    colors = plt.rcParams['axes.prop_cycle'].by_key()['color']

    line_segments = LineCollection(segs, linewidths=(0.5, 1, 1.5, 2),
                                   colors=colors, linestyle='solid')
    ax.add_collection(line_segments)
    ax.set_title('Line collection with masked arrays')
    plt.show()




.. image-sg:: /gallery/shapes_and_collections/images/sphx_glr_line_collection_001.png
   :alt: Line collection with masked arrays
   :srcset: /gallery/shapes_and_collections/images/sphx_glr_line_collection_001.png, /gallery/shapes_and_collections/images/sphx_glr_line_collection_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 46-49

In the following example, instead of passing a list of colors
(``colors=colors``), we pass an array of values (``array=x``) that get
colormapped.

.. GENERATED FROM PYTHON SOURCE LINES 49-69

.. code-block:: Python


    N = 50
    x = np.arange(N)
    ys = [x + i for i in x]  # Many sets of y to plot vs. x
    segs = [np.column_stack([x, y]) for y in ys]

    fig, ax = plt.subplots()
    ax.set_xlim(np.min(x), np.max(x))
    ax.set_ylim(np.min(ys), np.max(ys))

    line_segments = LineCollection(segs, array=x,
                                   linewidths=(0.5, 1, 1.5, 2),
                                   linestyles='solid')
    ax.add_collection(line_segments)
    axcb = fig.colorbar(line_segments)
    axcb.set_label('Line Number')
    ax.set_title('Line Collection with mapped colors')
    plt.sci(line_segments)  # This allows interactive changing of the colormap.
    plt.show()




.. image-sg:: /gallery/shapes_and_collections/images/sphx_glr_line_collection_002.png
   :alt: Line Collection with mapped colors
   :srcset: /gallery/shapes_and_collections/images/sphx_glr_line_collection_002.png, /gallery/shapes_and_collections/images/sphx_glr_line_collection_002_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 70-81

.. admonition:: References

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

   - `matplotlib.collections`
   - `matplotlib.collections.LineCollection`
   - `matplotlib.cm.ScalarMappable.set_array`
   - `matplotlib.axes.Axes.add_collection`
   - `matplotlib.figure.Figure.colorbar` / `matplotlib.pyplot.colorbar`
   - `matplotlib.pyplot.sci`


.. _sphx_glr_download_gallery_shapes_and_collections_line_collection.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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