Skip to content

Added Documentation on customizing color Sequences in Matplotlib #29554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,80 @@
:maxdepth: 2

project/index.rst

.. _color-sequences:

Color Sequences in Matplotlib
=============================

Matplotlib provides a powerful system for defining custom color sequences,
or color cycles, which are essential for creating consistent, visually appealing plots.
By customizing color sequences, users can control the color schemes used in their plots to improve accessibility and
make their visualizations clearer.

What Are Color Sequences?
--------------------------

In Matplotlib, color sequences are essentially ordered lists of colors
that can be used across multiple plots. They allow for consistency in the colors used
for different lines, markers, and other visual elements in your plots. Color sequences can be customized and applied through the `cycler` module or in style sheets.

Check warning on line 194 in doc/index.rst

View workflow job for this annotation

GitHub Actions / sphinx

[sphinx] doc/index.rst#L194

py:obj reference target not found: cycler [ref.obj]
Raw output
doc/index.rst:194: WARNING: py:obj reference target not found: cycler [ref.obj]

Defining a Custom Color Cycle
------------------------------

To define a custom color cycle, we use the `cycler` module

Check warning on line 199 in doc/index.rst

View workflow job for this annotation

GitHub Actions / sphinx

[sphinx] doc/index.rst#L199

py:obj reference target not found: cycler [ref.obj]
Raw output
doc/index.rst:199: WARNING: py:obj reference target not found: cycler [ref.obj]
from Matplotlib. Here's how to define and apply your own color sequence:

.. code-block:: python

import matplotlib.pyplot as plt
from cycler import cycler

# Define a custom color cycle
custom_cycle = cycler(color=["#E63946", "#F4A261", "#2A9D8F", "#264653"])

# Apply the custom color cycle
plt.rc("axes", prop_cycle=custom_cycle)

# Create a simple plot to visualize the color sequence
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6], label="Line 1")
ax.plot([1, 2, 3], [6, 7, 8], label="Line 2")
ax.legend()
plt.show()

Using Color Sequences in Style Sheets
--------------------------------------

You can also apply custom color sequences by defining them in a
Matplotlib style sheet (`.mplstyle` file). This is especially useful if you want to

Check warning on line 224 in doc/index.rst

View workflow job for this annotation

GitHub Actions / sphinx

[sphinx] doc/index.rst#L224

py:obj reference target not found: mplstyle [ref.obj]
Raw output
doc/index.rst:224: WARNING: py:obj reference target not found: mplstyle [ref.obj]
keep your color settings consistent across different projects.

Here's how you can define a custom color cycle in a style sheet:

.. code-block:: plaintext

Check warning on line 229 in doc/index.rst

View workflow job for this annotation

GitHub Actions / sphinx

[sphinx] doc/index.rst#L229

Pygments lexer name 'plaintext' is not known
Raw output
doc/index.rst:229: WARNING: Pygments lexer name 'plaintext' is not known

axes.prop_cycle: cycler(color=["#E63946", "#F4A261", "#2A9D8F", "#264653"])

Benefits of Using Custom Color Sequences
-----------------------------------------

Custom color sequences are beneficial for several reasons:

- **Consistency**: They ensure that your plots have a uniform color
scheme across multiple figures.
- **Accessibility**: By using colorblind-friendly colors, you can make your
visualizations more accessible to a wider audience.
- **Aesthetics**: Custom color sequences allow you to create more visually appealing
plots that match your preferred style or brand.

Conclusion
----------

Color sequences are a powerful tool in Matplotlib for customizing the appearance of your plots.
By defining your own color cycles and using them consistently, you can create more accessible and
aesthetically pleasing visualizations.
Experiment with your own color sequences and see how they improve your plots.

For more information on colormaps and color customization,
check out the [colormap tutorial](https://matplotlib.org/stable/tutorials/colors/colormap.html).
Empty file.
1 change: 1 addition & 0 deletions matplotlib
Submodule matplotlib added at e081c7
Loading