2

enter image description here

I have a set of RGB values in an array rgb_array of the form

[255.000, 56,026, 0.000]
[246.100, 60,000, 0.000]
...

>>> print(rbg_array)
1000, 3

that I'd like to plot similarly to the color gradient shown above.

How can I best use matpotlib's imshow to achieve this?

0

2 Answers 2

4

Supposing your array has N rows where each row contains 3 floats between 0 and 255, you can create an image as follows. First convert it to a numpy array of integers, and reshape it to (1, N, 3). This will make it a 1xN image. Then, display the image using imshow. You need to set an extent to get the x and y axes as in your example, or just set them to [0, 1, 0, 1]. Also the aspect ratio needs to be controlled, as otherwise the pixels would be considered "square".

import numpy as np
import matplotlib.pyplot as plt

rgb_array = [[255.000, 56.026 + (255 - 56.026) * i / 400, 255 * i / 400] for i in range(400)]
rgb_array += [[255 - 255 * i / 600, 255 - 255 * i / 600, 255] for i in range(600)]
img = np.array(rgb_array, dtype=int).reshape((1, len(rgb_array), 3))
plt.imshow(img, extent=[0, 16000, 0, 1], aspect='auto')
plt.show()

example image

Sign up to request clarification or add additional context in comments.

Comments

2

Don't use this method - @JohanC provides a much superior solution of creating an image rather than making a bar-graph.

I'm not so good on Matplotlib, but came up with this. There may be more efficient methods, so someone correct me please if this is the wrong approach.

#!/usr/bin/env python3

import numpy as np
import matplotlib.pyplot as plt

NSAMPLES = 100

# Synthesize R, G, B and A channels with dummy data
# The thing to note is that the samples are REAL and in range [0..1]
r = np.linspace(0,1,NSAMPLES).astype(np.float)
g = 1.0 - r
b = np.full(NSAMPLES,0.5,np.float)
a = np.full(NSAMPLES,1,np.float)

# Merge into a single array, 4 deep
RGBA = np.dstack((r,g,b,a))

# Plot
height, width = 40, 1
plt.bar(np.arange(NSAMPLES), height, width, color=rgba.reshape(-1,4))
plt.title("Some Funky Barplot")
plt.show()

enter image description here

The array RGBA looks like this:

array([[[0.        , 1.        , 0.5       , 1.        ],
        [0.01010101, 0.98989899, 0.5       , 1.        ],
        [0.02020202, 0.97979798, 0.5       , 1.        ],
        [0.03030303, 0.96969697, 0.5       , 1.        ],
        [0.04040404, 0.95959596, 0.5       , 1.        ],
        [0.05050505, 0.94949495, 0.5       , 1.        ],
        [0.06060606, 0.93939394, 0.5       , 1.        ],
        [0.07070707, 0.92929293, 0.5       , 1.        ],
        [0.08080808, 0.91919192, 0.5       , 1.        ],
        [0.09090909, 0.90909091, 0.5       , 1.        ],
        [0.1010101 , 0.8989899 , 0.5       , 1.        ],
        [0.11111111, 0.88888889, 0.5       , 1.        ],
        [0.12121212, 0.87878788, 0.5       , 1.        ],
        [0.13131313, 0.86868687, 0.5       , 1.        ],
        [0.14141414, 0.85858586, 0.5       , 1.        ],
        [0.15151515, 0.84848485, 0.5       , 1.        ],
        [0.16161616, 0.83838384, 0.5       , 1.        ],
        [0.17171717, 0.82828283, 0.5       , 1.        ],
        [0.18181818, 0.81818182, 0.5       , 1.        ],
        [0.19191919, 0.80808081, 0.5       , 1.        ],
        [0.2020202 , 0.7979798 , 0.5       , 1.        ],
        [0.21212121, 0.78787879, 0.5       , 1.        ],
        [0.22222222, 0.77777778, 0.5       , 1.        ],
        [0.23232323, 0.76767677, 0.5       , 1.        ],
        [0.24242424, 0.75757576, 0.5       , 1.        ],
        [0.25252525, 0.74747475, 0.5       , 1.        ],
        [0.26262626, 0.73737374, 0.5       , 1.        ],
        [0.27272727, 0.72727273, 0.5       , 1.        ],
        [0.28282828, 0.71717172, 0.5       , 1.        ],
        [0.29292929, 0.70707071, 0.5       , 1.        ],
        [0.3030303 , 0.6969697 , 0.5       , 1.        ],
        [0.31313131, 0.68686869, 0.5       , 1.        ],
        [0.32323232, 0.67676768, 0.5       , 1.        ],
        [0.33333333, 0.66666667, 0.5       , 1.        ],
        [0.34343434, 0.65656566, 0.5       , 1.        ],
        [0.35353535, 0.64646465, 0.5       , 1.        ],
        [0.36363636, 0.63636364, 0.5       , 1.        ],
        [0.37373737, 0.62626263, 0.5       , 1.        ],
        [0.38383838, 0.61616162, 0.5       , 1.        ],
        [0.39393939, 0.60606061, 0.5       , 1.        ],
        [0.4040404 , 0.5959596 , 0.5       , 1.        ],
        [0.41414141, 0.58585859, 0.5       , 1.        ],
        [0.42424242, 0.57575758, 0.5       , 1.        ],
        [0.43434343, 0.56565657, 0.5       , 1.        ],
        [0.44444444, 0.55555556, 0.5       , 1.        ],
        [0.45454545, 0.54545455, 0.5       , 1.        ],
        [0.46464646, 0.53535354, 0.5       , 1.        ],
        [0.47474747, 0.52525253, 0.5       , 1.        ],
        [0.48484848, 0.51515152, 0.5       , 1.        ],
        [0.49494949, 0.50505051, 0.5       , 1.        ],
        [0.50505051, 0.49494949, 0.5       , 1.        ],
        [0.51515152, 0.48484848, 0.5       , 1.        ],
        [0.52525253, 0.47474747, 0.5       , 1.        ],
        [0.53535354, 0.46464646, 0.5       , 1.        ],
        [0.54545455, 0.45454545, 0.5       , 1.        ],
        [0.55555556, 0.44444444, 0.5       , 1.        ],
        [0.56565657, 0.43434343, 0.5       , 1.        ],
        [0.57575758, 0.42424242, 0.5       , 1.        ],
        [0.58585859, 0.41414141, 0.5       , 1.        ],
        [0.5959596 , 0.4040404 , 0.5       , 1.        ],
        [0.60606061, 0.39393939, 0.5       , 1.        ],
        [0.61616162, 0.38383838, 0.5       , 1.        ],
        [0.62626263, 0.37373737, 0.5       , 1.        ],
        [0.63636364, 0.36363636, 0.5       , 1.        ],
        [0.64646465, 0.35353535, 0.5       , 1.        ],
        [0.65656566, 0.34343434, 0.5       , 1.        ],
        [0.66666667, 0.33333333, 0.5       , 1.        ],
        [0.67676768, 0.32323232, 0.5       , 1.        ],
        [0.68686869, 0.31313131, 0.5       , 1.        ],
        [0.6969697 , 0.3030303 , 0.5       , 1.        ],
        [0.70707071, 0.29292929, 0.5       , 1.        ],
        [0.71717172, 0.28282828, 0.5       , 1.        ],
        [0.72727273, 0.27272727, 0.5       , 1.        ],
        [0.73737374, 0.26262626, 0.5       , 1.        ],
        [0.74747475, 0.25252525, 0.5       , 1.        ],
        [0.75757576, 0.24242424, 0.5       , 1.        ],
        [0.76767677, 0.23232323, 0.5       , 1.        ],
        [0.77777778, 0.22222222, 0.5       , 1.        ],
        [0.78787879, 0.21212121, 0.5       , 1.        ],
        [0.7979798 , 0.2020202 , 0.5       , 1.        ],
        [0.80808081, 0.19191919, 0.5       , 1.        ],
        [0.81818182, 0.18181818, 0.5       , 1.        ],
        [0.82828283, 0.17171717, 0.5       , 1.        ],
        [0.83838384, 0.16161616, 0.5       , 1.        ],
        [0.84848485, 0.15151515, 0.5       , 1.        ],
        [0.85858586, 0.14141414, 0.5       , 1.        ],
        [0.86868687, 0.13131313, 0.5       , 1.        ],
        [0.87878788, 0.12121212, 0.5       , 1.        ],
        [0.88888889, 0.11111111, 0.5       , 1.        ],
        [0.8989899 , 0.1010101 , 0.5       , 1.        ],
        [0.90909091, 0.09090909, 0.5       , 1.        ],
        [0.91919192, 0.08080808, 0.5       , 1.        ],
        [0.92929293, 0.07070707, 0.5       , 1.        ],
        [0.93939394, 0.06060606, 0.5       , 1.        ],
        [0.94949495, 0.05050505, 0.5       , 1.        ],
        [0.95959596, 0.04040404, 0.5       , 1.        ],
        [0.96969697, 0.03030303, 0.5       , 1.        ],
        [0.97979798, 0.02020202, 0.5       , 1.        ],
        [0.98989899, 0.01010101, 0.5       , 1.        ],
        [1.        , 0.        , 0.5       , 1.        ]]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.