Laura
Laura

Reputation: 1282

Plot multiple RGB colors in one plot with matplotlib

I have a dictionary with this RGB arrays:

colors = {"blue": [
            [0  ,128,255],
            [0  ,102,204],
            [0  ,76 ,153],
            [102,102,255],
            [51 ,51 ,255],
            [0  ,0  ,255],
            [0  ,0  ,204],
            [42, 81, 122],
            [60, 108, 191]   
            ]
           }

Is there some function on Matplotlib that allows me to easily plot these colors (each array represents an RGB code) all together in one plot?

Upvotes: 0

Views: 608

Answers (1)

Tom
Tom

Reputation: 8800

Yes, you can pass RGB/RGBA values to (at least some) plotting functions in matplotlib. Here's an example with your input:

import matplotlib.pyplot as plt
import numpy as np

colors = {"blue": [
            [0  ,128,255],
            [0  ,102,204],
            [0  ,76 ,153],
            [102,102,255],
            [51 ,51 ,255],
            [0  ,0  ,255],
            [0  ,0  ,204],
            [42, 81, 122],
            [60, 108, 191]   
            ]
           }
# access the colors, scale them to be 0-1
blues = np.array(colors['blue']) / 255

# create figure and data
fig, ax = plt.subplots()
x = range(len(blues))

# plot, using one color for each point
ax.scatter(x, x, c=blues)

enter image description here

See the docs for the c parameter (in the case of a scatter plot).

Upvotes: 1

Related Questions