Represent a 2D list as a map of colored squares in Pylab

Question:

Let’s say I have a 2D list that looks like this:

[
['Red','Green','Blue'],
['Red','Red','Green'],
['Red','Red','Red' ]
                   ]

Is there a way I could represent it in Pylab as an map with squares actually being of the color given in a 2D list?

Thank You in Advance.

Asked By: Akavall

||

Answers:

If you can convert your 2D list as a list of numbers, then you can use the imshow function in python

import numpy as np
import matplotlib.pyplot as plt

a = np.array(((1, 2, 3), (4, 5, 6)))
plt.imshow(a)
plt.show()

you will need the numpy and matplotlib packages for this.

Answered By: Spot
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.