
.. 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:


==========================================
Plot multiple lines using a LineCollection
==========================================

Matplotlib can efficiently draw multiple lines at once using a `~.LineCollection`.

.. GENERATED FROM PYTHON SOURCE LINES 8-35

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    from matplotlib.collections import LineCollection

    colors = ["indigo", "blue", "green", "yellow", "orange", "red"]

    # create a list of half-circles with varying radii
    theta = np.linspace(0, np.pi, 36)
    radii = np.linspace(4, 5, num=len(colors))
    arcs = [np.column_stack([r * np.cos(theta), r * np.sin(theta)]) for r in radii]

    fig, ax = plt.subplots(figsize=(6.4, 3.2))
    # set axes limits manually because Collections do not take part in autoscaling
    ax.set_xlim(-6, 6)
    ax.set_ylim(0, 6)
    ax.set_aspect("equal")  # to make the arcs look circular

    # create a LineCollection with the half-circles
    # its properties can be set per line by passing a sequence (here used for *colors*)
    # or they can be set for all lines by passing a scalar (here used for *linewidths*)
    line_collection = LineCollection(arcs, colors=colors, linewidths=4)
    ax.add_collection(line_collection)

    plt.show()




.. image-sg:: /gallery/shapes_and_collections/images/sphx_glr_line_collection_001.png
   :alt: line collection
   :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 36-40

Instead of passing a list of colors (``colors=colors``), we can alternatively use
colormapping. The lines are then color-coded based on an additional array of values
passed to the *array* parameter. In the below example, we color the lines based on
their radius by passing ``array=radii``.

.. GENERATED FROM PYTHON SOURCE LINES 40-60

.. code-block:: Python


    num_arcs = 15
    theta = np.linspace(0, np.pi, 36)
    radii = np.linspace(4, 5.5, num=num_arcs)
    arcs = [np.column_stack([r * np.cos(theta), r * np.sin(theta)]) for r in radii]

    fig, ax = plt.subplots(figsize=(6.4, 3))
    # set axes limits manually because Collections do not take part in autoscaling
    ax.set_xlim(-6, 6)
    ax.set_ylim(0, 6)
    ax.set_aspect("equal")  # to make the arcs look circular

    # create a LineCollection with the half-circles and color mapping
    line_collection = LineCollection(arcs, array=radii, cmap="rainbow")
    ax.add_collection(line_collection)

    fig.colorbar(line_collection, label="Radius")
    ax.set_title("Line Collection with mapped colors")

    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 61-70

.. admonition:: References

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

   - `matplotlib.collections.LineCollection`
   - `matplotlib.collections.Collection.set_array`
   - `matplotlib.axes.Axes.add_collection`
   - `matplotlib.figure.Figure.colorbar` / `matplotlib.pyplot.colorbar`


.. _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>`_
