Matplotlib | Color settings! (RGB, Hex, Grayscale, Tableau, CSS)

You may be wondering how to set up colors when using Matplotlib to create graphs and diagrams.

This article will explain in detail how to set up Matplotlib’s color-related settings.

After reading it, you will be able to freely set the colors of your graphs using Matplotlib to enhance their visual appeal and information conveyance.

Topics covered include RGB array specification, hexadecimal, single character specification, grayscale, Tableau colors, CSS colors, and more!

Table of Contents

Color Options

Color can be set by RGB, single character, Tableau Colors, or CSS Colors.

Color formats
RGB or RGBA
  • Tuple of float values : Closed interval [0, 1], (red, green, blue, alpha)(0.1, 0.2, 0.5, 0.3)
  • Case-insensitive hex : '#0f0f0f', #0f0f0f80'
  • Hex shorthand of duplicated characters. : #abc' (#aabbcc')
Single character
  • Grayscale :  closed interval [0, 1].'0' =black, '1' =white
  • Single character shorthand notation : 8 basic colors.'b' =blue
Tableau Colors
  • Tableau Colors : ‘T10’ categorical palette.'tab:blue'
  • “CN” Colors : C” + “Number” calls a color cycle. Used by default.'C0'
CSS or xkcd Colors
  • X11/CSS4 : X11/CSS4 color name with no spaces.'aqua'
  • xkcd : color name from xkcd color survey.'xkcd:sky blue'

Default colors and Entire code

No colors are set and the default colors are used

The code from the next chapter changes only step3 “Plot graphs

# step0 Import libraries
import matplotlib.pyplot as plt
import numpy as np

# step1 Create data
x = np.linspace(0, 10, 100)
y1 = 4 + 2 * np.sin(2 * x)
y2 = 4 + 2 * np.cos(2 * x)

# step2 Create graph frames
fig, ax = plt.subplots(figsize=(6, 4))

# step3 Plot graphs
# Specify the color here
ax.plot(x, y1, label='Sample1'))
ax.plot(x, y2, label='Sample2')

# step4 Set a title and axis labels
ax.set_title('Default color')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.legend()

# step5 Call the Figure
plt.show()

RGB or RGBA

There are three ways to specify colors in RGB or RGBA (A is transparency)

  • Tuple of float values : Closed interval [0, 1], (red, green, blue, alpha)(0.1, 0.2, 0.5, 0.3)
  • Case-insensitive hex : '#0f0f0f', #0f0f0f80'
  • Hex shorthand of duplicated characters : #abc' (#aabbcc')

Tuple of float values

In an array (tuple), enter RGB = (red, green, blue) or RGBA = (red, green, blue, alpha).

The value of each element of the array is a float value between 0 and 1.

RGB : color=(0.2, 0.7, 0.7)
RGBA : color=(0.1, 0.2, 0.5, 0.3)

# step3 Plot graphs
# tuple
ax.plot(x, y1, label='color=(0.2, 0.7, 0.7)', color=(0.2, 0.7, 0.7))
ax.plot(x, y2, label='color=(0.1, 0.2, 0.5, 0.3)', color=(0.1, 0.2, 0.5, 0.3))

Case-insensitive hex

Specify the color in hexadecimal.

For RGBA alpha, append 00~99 at the end

RGB : color=#008BCD4
RGBA : color=#4CAF5070

# step3 Plot graphs
# hexadecimal
ax.plot(x, y1, label='#00BCD4', color='#00BCD4')
ax.plot(x, y2, label='#4CAF5070', color='#4CAF5070')

Hex shorthand of duplicated characters

Specifies the color in hexadecimal abbreviated notation.

RGB : #1BB=11BBBB
RGBA : #D2C6=#DD22CC66

# step3 Plot graphs
# Hex shorthand of duplicated characters
ax.plot(x, y1, label='#1BB', color='#1BB')
ax.plot(x, y2, label='#D2C6', color='#D2C6')

Single character

There are two ways to specify the color of a single character.

  • Grayscale :  closed interval [0, 1].'0' =black, '1' =white
  • Single character shorthand notation : 8 basic colors.'b' =blue

Grayscale

For grayscale, enter a float value of 0~1

Enter 0.5 for gray

black:'0'
white:'1'

Please enter ‘0.7’ as a string

# step3 Plot graphs
# Grayscale
ax.plot(x, y1, label='Black=0', color='0')
ax.plot(x, y2, label='Gray=0.6', color='0.6')

8 Basic Colors

This is a method of expressing only one basic color for single character.

The following 8 colors can be specified

  1. blue:'b'
  2. green:'g'
  3. red:'r' 
  4. cyan:'c'
  5. magenta:'m'
  6. yellow:'y' 
  7. black:'k'
  8. white:'w'
# step3 Plot graphs
# Basic colors
ax.plot(x, y1, label='b', color='b')
ax.plot(x, y2, label='r', color='r')

Tableau Color

You can also use Tableau 10 in the Tableau color palette

  • Tableau Colors : ‘T10’ categorical palette.'tab:blue'
  • “CN” Colors : C” + “Number” calls a color cycle. Used by default.'C0'

Tableau Color Palette

Tableau 10 in the Tableau color palette is prefixed with tab

  1. 'tab:blue'
  2. 'tab:orange'
  3. 'tab:green'
  4. 'tab:red'
  5. 'tab:purple'
  6. 'tab:brown'
  7. 'tab:pink'
  8. 'tab:gray'
  9. 'tab:olive'
  10. 'tab:cyan'
# step3 Plot graphs
# Tableau color palette
ax.plot(x, y1, label='tab:blue', color='tab:blue')
ax.plot(x, y2, label='tab:orange', color='tab:orange')

“CN” Colors

There is a method to call a color cycle with “C” + “Number

Specify by typing color='C0', color='C1'

  • '#1f77b4'='tab:blue'
  • '#ff7f0e'='tab:orange'
  • '#2ca02c'='tab:green'
  • '#d62728'='tab:red'
  • '#9467bd'='tab:purple'
  • '#8c564b'='tab:brown'
  • '#e377c2'='tab:pink'
  • '#7f7f7f'='tab:gray'
  • '#bcbd22'='tab:olive'
  • '#17becf'='tab:cyan'

The default color configuration is the same as Tableau 10 in the Tableau color palette

# step3 Plot graphs
# CN colors
ax.plot(x, y1, label='C0', color='C0')
ax.plot(x, y2, label='C1', color='C1')

Update rcParams["axes.prop_cycle"] to change the color cycle

import matplotlib as mpl
mpl.rcParams['axes.prop_cycle'] = cycler(color=['r', 'g', 'b', 'y'])

X11, CSS, xkcd Color

Specifies the color as a string

  • X11/CSS4 : X11/CSS4 color name with no spaces.'aqua'
  • xkcd : color name from xkcd color survey.'xkcd:sky blue'

Of the 148 X11/CSS4 color names, 95 are also included in the xkcd color survey, but with different color values.
Only “black,” “white,” and “cyan” are the same.

X11/CSS4 color name

String representing a color in the X Window System

Enter a string without spaces, 'aquamarine'

You can specify one of the following 140 colors

# step3 Plot graphs
# CSS color name
ax.plot(x, y1, label='olive', color='olive')
ax.plot(x, y2, label='mediumseagreen', color='mediumseagreen')

xkcd color name

954 most common RGB monitor colors as defined by hundreds of thousands of participants

prefix it with 'xkcd:sky blue' and xkcd:.

Here are the colors you can specify

# step3 Plot graphs
# xkcd color name
ax.plot(x, y1, label='xkcd:pink', color='xkcd:pink')
ax.plot(x, y2, label='xkcd:teal', color='xkcd:teal')

References

List of colors that can be specified in Matplotlib

Specify color

CSS4 color name

I hope you will share it with me!

Comments

To comment

Table of Contents