Mateusz Konopelski
Mateusz Konopelski

Reputation: 1052

Gray scale color (0-255) to MAtplotlib colors representation

I cannot solve this (what seems easy) problem when using matplotlib to build some plots.

I have gray scale colors represented as integers in range(0-255 - higher numbers meaning darker) which can be simplified in this example df:

colors = pd.DataFrame({'color1': [15], 'color2': [27], 'color3': [89], 
'color4': [123], 'color5': [220], 'color6': [100], 
'color7': [123], 'color8': [247], 'color9': [255]})

Now by looping I want to change the plots background with those colors as:

fig, ax = plt.subplots(3, 3, figsize=(10, 10), sharey='row', sharex='col')
fig.subplots_adjust(hspace=0, wspace=0)

column = 0
for i in range(3):
    for j in range(3):
        color = colors.iloc[0, column]
        print(f'{i}, {j}: {color}')

        # here I used (0.15, 0.16, 0.17) as example. 
        #But I want to have variable "color" converted into readable color by set_facecolor
        ax[i, j].set_facecolor((0.15, 0.16, 0.17))

        column += 1

by using matplotlib documentation I can only do it in those colors formats:

Matplotlib recognizes the following formats to specify a color:

  • an RGB or RGBA tuple of float values in [0, 1] (e.g., (0.1, 0.2, 0.5) or (0.1, 0.2, 0.5, 0.3));

  • a hex RGB or RGBA string (e.g., '#0F0F0F' or '#0F0F0F0F');

  • a string representation of a float value in [0, 1] inclusive for gray level (e.g., '0.5'); one of {'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'};

  • a X11/CSS4 color name;

  • a name from the xkcd color survey; prefixed with 'xkcd:' (e.g., 'xkcd:sky blue');

  • one of {'tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan'} which are the Tableau Colors from the ‘T10’ categorical palette (which is the default color cycle);

  • a “CN” color spec, i.e. 'C' followed by a single digit, which is an index into the default property cycle (matplotlib.rcParams['axes.prop_cycle']); the indexing occurs at artist creation time and defaults to black if the cycle does not include color.

Using those SO answers:

I converted rewrote my code to:

def rgb_int2tuple(rgbint):
    return (rgbint // 256 // 256 % 256, rgbint // 256 % 256, rgbint % 256)


colors = pd.DataFrame({'color1': [15], 'color2': [27], 'color3': [89], 'color4': [123],
                       'color5': [220], 'color6': [100], 'color7': [123], 'color8': [247], 'color9': [255]})

fig, ax = plt.subplots(3, 3, figsize=(10, 10), sharey='row', sharex='col')
fig.subplots_adjust(hspace=0, wspace=0)

column = 0
for i in range(3):
    for j in range(3):
        color = colors.iloc[0, column]
        color = 255 - color
        Blue, Green, Red = rgb_int2tuple(color)
        print(f'{i}, {j}: {color}\t{Blue}{Green}{Red}')
        ax[i, j].set_facecolor((Blue/255, Green/255, Red/255))

        column += 1

But the result is: enter image description here

Which takes me to step1, how to let python know that my 0-255 scale is gray.

[EDIT]:

I read again the matplotlib.colors documentation and found

  • a string representation of a float value in [0, 1] inclusive for gray level (e.g., '0.5');

using this:

I rewrote my code to:

colors = pd.DataFrame({'color1': [15], 'color2': [27], 'color3': [89], 'color4': [123],
                       'color5': [220], 'color6': [100], 'color7': [123], 'color8': [247], 'color9': [255]})

fig, ax = plt.subplots(3, 3, figsize=(10, 10), sharey='row', sharex='col')
fig.subplots_adjust(hspace=0, wspace=0)

column = 0
for i in range(3):
    for j in range(3):
        color = colors.iloc[0, column]
        color = 255 - color
        color = color / 255
        ax[i, j].set_facecolor(str(color))

        column += 1

And this gave me: enter image description here

But I doubt, that this is the best solution.

Upvotes: 2

Views: 8816

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339765

You may convert the number of a string which denotes the grey level between 0 and 1.

import pandas as pd
import matplotlib.pyplot as plt

colors = pd.DataFrame({'color1': [15], 'color2': [27], 'color3': [89], 'color4': [123],
                       'color5': [220], 'color6': [100], 'color7': [123], 'color8': [247], 'color9': [255]})

fig, axes = plt.subplots(3, 3, figsize=(10, 10), sharey='row', sharex='col')
fig.subplots_adjust(hspace=0, wspace=0)

for ax, c in zip(axes.flat, colors.T[0].values):
    ax.set_facecolor(str(c/255.))

plt.show()

Or you may convert it to a RGB tuple, where each channel has the same value

for ax, c in zip(axes.flat, colors.T[0].values):
    ax.set_facecolor((c/255.,c/255.,c/255.))

Finally, you may use a colormap and normalization as

norm = plt.Normalize(0,255)
cmap = plt.get_cmap("gray")
for ax, c in zip(axes.flat, colors.T[0].values):
    ax.set_facecolor(cmap(norm(c)))

You get the same result in all three cases.

Upvotes: 1

Related Questions