🌐
Matplotlib
matplotlib.org › stable › gallery › color › color_sequences.html
Named color sequences — Matplotlib 3.11.2 documentation
for n, name in enumerate(names): colors = mpl.color_sequences[name] n_colors = len(colors) x = np.arange(n_colors) y = np.full_like(x, n) ax.scatter(x, y, facecolor=colors, edgecolor='dimgray', s=200, zorder=2) ax.set_yticks(range(len(names)), labels=names) ax.grid(visible=True, axis='y') ax.yaxis.set_inverted(True) ax.xaxis.set_visible(False) ax.spines[:].set_visible(False) ax.tick_params(left=False) built_in_color_sequences = [ 'tab10', 'tab20', 'tab20b', 'tab20c', 'Pastel1', 'Pastel2', 'Paired', 'Accent', 'okabe_ito', 'Dark2', 'Set1', 'Set2', 'Set3', 'petroff6', 'petroff8', 'petroff10'] fig, ax = plt.subplots(figsize=(6.4, 9.6), layout='constrained') plot_color_sequences(built_in_color_sequences, ax) ax.set_title('Built In Color Sequences') plt.show()
🌐
Matplotlib
matplotlib.org › stable › gallery › color › color_cycle_default.html
Colors in the default property cycle — Matplotlib 3.11.2 documentation
Display the colors from the default prop_cycle, which is obtained from the rc parameters. import matplotlib.pyplot as plt import numpy as np from matplotlib.colors import TABLEAU_COLORS, same_color def f(x, a): """A nice sigmoid-like parametrized curve, ending approximately at *a*.""" return 0.85 * a * (1 / (1 + np.exp(-x)) + 0.2) fig, ax = plt.subplots() ax.axis('off') ax.set_title("Colors in the default property cycle") prop_cycle = plt.rcParams['axes.prop_cycle'] colors = prop_cycle.by_key()['color'] x = np.linspace(-4, 4, 200) for i, (color, color_name) in enumerate(zip(colors, TABLEAU_COLORS)): assert same_color(color, color_name) pos = 4.5 - i ax.plot(x, f(x, pos)) ax.text(4.2, pos, f"'C{i}': '{color_name}'", color=color, va="center") ax.bar(9, 1, width=1.5, bottom=pos-0.5) plt.show()
🌐
Matplotlib
matplotlib.org › 2.0.2 › api › colors_api.html
colors — Matplotlib 2.0.2 documentation
Each row in the table for a given color is a sequence of x, y0, y1 tuples. In each sequence, x must increase monotonically from 0 to 1.
🌐
University of Texas at Austin
het.as.utexas.edu › HET › Software › Matplotlib › api › colors_api.html
colors — Matplotlib 1.4.2 documentation
Each row in the table for a given color is a sequence of x, y0, y1 tuples. In each sequence, x must increase monotonically from 0 to 1.
🌐
GitHub
github.com › matplotlib › matplotlib › blob › main › lib › matplotlib › colors.py
matplotlib/lib/matplotlib/colors.py at main · matplotlib/matplotlib
Container for sequences of colors that are known to Matplotlib by name. · The universal registry instance is `matplotlib.color_sequences`. There · should be no need for users to instantiate `.ColorSequenceRegistry` themselves. · Read access uses a dict-like interface mapping names to lists of colors:: ·
🌐
Damask-multiphysics
damask-multiphysics.org › _modules › matplotlib › colors.html
matplotlib.colors - damask-multiphysics.org
The color sequence registry stores a copy of the given *color_list*, so that future changes to the original list do not affect the registered color sequence. Think of this as the registry taking a snapshot of *color_list* at registration. Parameters ---------- name : str The name for the color sequence. color_list : list of :mpltype:`color` An iterable returning valid Matplotlib colors when iterating over.
🌐
Matplotlib
matplotlib.org › 3.1.1 › gallery › color › color_cycle_default.html
Colors in the default property cycle — Matplotlib 3.1.2 documentation
Display the colors from the default prop_cycle, which is obtained from the rc parameters. import numpy as np import matplotlib.pyplot as plt prop_cycle = plt.rcParams['axes.prop_cycle'] colors = prop_cycle.by_key()['color'] lwbase = plt.rcParams['lines.linewidth'] thin = lwbase / 2 thick = lwbase * 3 fig, axs = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True) for icol in range(2): if icol == 0: lwx, lwy = thin, lwbase else: lwx, lwy = lwbase, thick for irow in range(2): for i, color in enumerate(colors): axs[irow, icol].axhline(i, color=color, lw=lwx) axs[irow, icol].axvline(i, color=c
Find elsewhere
🌐
Matplotlib
matplotlib.org › 2.0.1 › api › colors_api.html
colors — Matplotlib 2.0.1 documentation
Each row in the table for a given color is a sequence of x, y0, y1 tuples. In each sequence, x must increase monotonically from 0 to 1.
🌐
Matplotlib
matplotlib.org › 1.4.1 › api › colors_api.html
colors — Matplotlib 1.4.1 documentation
Each row in the table for a given color is a sequence of x, y0, y1 tuples. In each sequence, x must increase monotonically from 0 to 1.
🌐
Matplotlib
matplotlib.org › stable › tutorials › intermediate › color_cycle.html
Styling with cycler — Matplotlib 3.7.2 documentation
November 10, 2021 - custom_cycler = (cycler(color=['c', 'm', 'y', 'k']) + cycler(lw=[1, 2, 3, 4])) fig, (ax0, ax1) = plt.subplots(nrows=2) ax0.plot(yy) ax0.set_title('Set default color cycle to rgby') ax1.set_prop_cycle(custom_cycler) ax1.plot(yy) ax1.set_title('Set axes color cycle to cmyk') # Add a bit more space between the two plots. fig.subplots_adjust(hspace=0.3) plt.show() Remember, a custom cycler can be set in your matplotlibrc file or a style file (style.mplstyle) under axes.prop_cycle:
Top answer
1 of 2
2

imshow can work in two ways:

  • usually, it gets a 2D input, which are interpreted as scalar values which get color mapped
  • on the other hand, a 3D input is interpreted as a 2D grid of explicit rgb values

So, the idea is to create such a 3D list for your values. [rgb_values] serves that goal.

imshow([rgb_values]) creates a plot going from -0.5 to 18.5 on the x-axis. That way x-positions 0, 1, ..., 18 are the centers of the cells. ax.set_xticks() can then label these positions. As it is hard to fit 19 values horizontally, the labels can be rotated a bit. (I am not sure how you want to fit both the numeric and the hex labels with the limited space. Maybe place one at the top? Or create a vertical bar?)

import matplotlib.pyplot as plt
from matplotlib.colors import hex2color

num_values = [-10000000, -5000000, -2500000, -1000000, -500000, -250000, -100000, -50000, -10000, 0, 10000, 50000, 100000, 250000, 500000, 1000000, 2500000, 5000000, 10000000]
hex_values = ['#345f79', '#367aa3', '#368cbf', '#3690c0', '#74a9cf', '#a6bddb', '#d0d1e6', '#ece7f2', '#fff7fb', '#f0f0f0', '#ffffcc', '#ffeda0', '#fed976', '#feb24c', '#fda362', '#fc7154', '#d04c67', '#bd0026', '#a64c67']
rgb_values = [hex2color(hex) for hex in hex_values]

fig, ax = plt.subplots(figsize=(12, 3))
ax.imshow([rgb_values], interpolation='nearest')
ax.set_yticks([])
# ax.set_xticks(range(len(num_values)), num_values, rotation=30)
ax.set_xticks(range(len(hex_values)), hex_values, rotation=30)
plt.tight_layout()
plt.show()

2 of 2
2

Because your num_values spans an exponential range, the colors for the values in the middle aren't being mapped correctly. It's not clear to me what your end goal is for this colormap, but if all you want to do is plot the colors and have the x-ticks show the corresponding number, you can do the following:

fig, ax = plt.subplots(figsize=(10,2))
ax.imshow((range(len(num_values)),),  cmap=ListedColormap(hex_values))
ax.set_xticks(range(len(num_values)), num_values, rotation=90)
fig.tight_layout()
fig.show()

Top answer
1 of 4
217

In matplotlib versions >= 1.5, you can print the rcParam called axes.prop_cycle:

print(plt.rcParams['axes.prop_cycle'].by_key()['color'])

# [u'#1f77b4', u'#ff7f0e', u'#2ca02c', u'#d62728', u'#9467bd', u'#8c564b', u'#e377c2', u'#7f7f7f', u'#bcbd22', u'#17becf']

Or equivalently, in python2:

print plt.rcParams['axes.prop_cycle'].by_key()['color']

In versions < 1.5, this was called color_cycle:

print plt.rcParams['axes.color_cycle']

# [u'b', u'g', u'r', u'c', u'm', u'y', u'k']

Note that the default color cycle changed in version 2.0.0 http://matplotlib.org/users/dflt_style_changes.html#colors-in-default-property-cycle

2 of 4
198

Often, there is no need to get the default color cycle from anywhere, as it is the default one, so just using it is sufficient.

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

t = np.arange(5)

for i in range(4):
    line, = ax.plot(t,i*(t+1), linestyle = '-')
    ax.plot(t,i*(t+1)+.3,color = line.get_color(), linestyle = ':')

plt.show()

In case you want to use the default color cycle for something different, there are of course several options.

"tab10" colormap

First it should be mentionned that the "tab10" colormap comprises the colors from the default color cycle, you can get it via cmap = plt.get_cmap("tab10").

Equivalent to the above would hence be

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

t = np.arange(5)
cmap = plt.get_cmap("tab10")
for i in range(4):
    ax.plot(t,i*(t+1),   color=cmap(i), linestyle = '-')
    ax.plot(t,i*(t+1)+.3,color=cmap(i), linestyle = ':')

plt.show()

Colors from color cycle

You can also use the color cycler directly, cycle = plt.rcParams['axes.prop_cycle'].by_key()['color']. This gives list with the colors from the cycle, which you can use to iterate over.

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

t = np.arange(5)
cycle = plt.rcParams['axes.prop_cycle'].by_key()['color']

for i in range(4):
    ax.plot(t,i*(t+1),   color=cycle[i], linestyle = '-')
    ax.plot(t,i*(t+1)+.3,color=cycle[i], linestyle = ':')

plt.show()

The CN notation

Finally, the CN notation allows to get the Nth color of the color cycle, color="C{}".format(i). This however only works for the first 10 colors (N in [0,1,...9])

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

t = np.arange(5)

for i in range(4):
    ax.plot(t,i*(t+1),   color="C{}".format(i), linestyle = '-')
    ax.plot(t,i*(t+1)+.3,color="C{}".format(i), linestyle = ':')

plt.show()

All codes presented here produce the same plot.

🌐
Statology
statology.org › home › a complete guide to the default colors in matplotlib
A Complete Guide to the Default Colors in Matplotlib
July 17, 2022 - The third hex color code is #2ca02c. This corresponds to the third line in the plot that is green. ... This means if you create a plot in Matplotlib with one line, then the color of the line will be #1f77b4 unless you specify otherwise.
🌐
Robin's Blog
blog.rtwilson.com › easily-specifying-colours-from-the-default-colour-cycle-in-matplotlib
Easily specifying colours from the default colour cycle in matplotlib « Robin's Blog
November 14, 2019 - The first is to get the default colour cycle from the matplotlib settings, and extract the relevant colour: cycle_colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
🌐
Matplotlib
matplotlib.org › stable › users › explain › artists › color_cycle.html
Styling with cycler — Matplotlib 3.11.2 documentation
Remember, a custom cycler can be set in your matplotlibrc file or a style file (style.mplstyle) under axes.prop_cycle, e.g. axes.prop_cycle : cycler(color=['red', 'royalblue', 'gray']) For colors, a single string may be used either for one of the Named color sequences ·
🌐
Matplotlib
matplotlib.org › stable › tutorials › colors › colors.html
Matplotlib
January 28, 2021 - Matplotlib has support for visualizing information with a wide array of colors and colormaps.
🌐
Matplotlib
matplotlib.org › 3.1.1 › api › colors_api.html
matplotlib.colors — Matplotlib 3.1.2 documentation
Choosing Colormaps in Matplotlib for a list of built-in colormaps. Colormap Normalization for more details about data normalization · More colormaps are available at palettable. The module also provides functions for checking whether an object can be interpreted as a color (is_color_like()), for converting such an object to an RGBA tuple (to_rgba()) or to an HTML-like hex string in the #rrggbb format (to_hex()), and a sequence of colors to an (n, 4) RGBA array (to_rgba_array()).