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

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

.. _sphx_glr_gallery_color_set_alpha.py:


=================================
Ways to set a color's alpha value
=================================

Compare setting alpha by the *alpha* keyword argument and by one of the Matplotlib color
formats. Often, the *alpha* keyword is the only tool needed to add transparency to a
color. In some cases, the *(matplotlib_color, alpha)* color format provides an easy way
to fine-tune the appearance of a Figure.

.. GENERATED FROM PYTHON SOURCE LINES 12-45

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    # Fixing random state for reproducibility.
    np.random.seed(19680801)

    fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(8, 4))

    x_values = [n for n in range(20)]
    y_values = np.random.randn(20)

    facecolors = ['green' if y > 0 else 'red' for y in y_values]
    edgecolors = facecolors

    ax1.bar(x_values, y_values, color=facecolors, edgecolor=edgecolors, alpha=0.5)
    ax1.set_title("Explicit 'alpha' keyword value\nshared by all bars and edges")


    # Normalize y values to get distinct face alpha values.
    abs_y = [abs(y) for y in y_values]
    face_alphas = [n / max(abs_y) for n in abs_y]
    edge_alphas = [1 - alpha for alpha in face_alphas]

    colors_with_alphas = list(zip(facecolors, face_alphas))
    edgecolors_with_alphas = list(zip(edgecolors, edge_alphas))

    ax2.bar(x_values, y_values, color=colors_with_alphas,
            edgecolor=edgecolors_with_alphas)
    ax2.set_title('Normalized alphas for\neach bar and each edge')

    plt.show()




.. image-sg:: /gallery/color/images/sphx_glr_set_alpha_001.png
   :alt: Explicit 'alpha' keyword value shared by all bars and edges, Normalized alphas for each bar and each edge
   :srcset: /gallery/color/images/sphx_glr_set_alpha_001.png, /gallery/color/images/sphx_glr_set_alpha_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 46-59

.. admonition:: References

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

   - `matplotlib.axes.Axes.bar`
   - `matplotlib.pyplot.subplots`

.. tags::

   styling: color
   plot-type: bar
   level: beginner


.. _sphx_glr_download_gallery_color_set_alpha.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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