Travasaurus
Travasaurus

Reputation: 654

How to plot a grid of colors and text

I'm building a bot to play a game, and would like to plot that gameboard after every round. The board will be a 5x5 grid with one of 4 colors in each square and a unique text label for that square. ex:

'dog' 'rub' 'tom', 'rob', 'kon'
'gog' 'bub' 'rom', 'vob', 'zon'
'hog' 'gub' 'kom', 'mob', 'son'
'jog' 'hub' 'bom', 'lob', 'con'
'fog' 'fub' 'mom', 'cob', 'lon'

with each square being one of 4 colors.

How would I plot that?

I've been trying to use confusion matrices, but I know there's a better way (or I'm not using them right)

Upvotes: 0

Views: 597

Answers (1)

JohanC
JohanC

Reputation: 80319

You could create it with seaborn:

from matplotlib import pyplot as plt
import numpy as np
import seaborn as sns
from matplotlib.colors import ListedColormap

labels = [['dog', 'rub', 'tom', 'rob', 'kon'],
          ['gog', 'bub', 'rom', 'vob', 'zon'],
          ['hog', 'gub', 'kom', 'mob', 'son'],
          ['jog', 'hub', 'bom', 'lob', 'con'],
          ['fog', 'fub', 'mom', 'cob', 'lon']]
color_numbers = np.random.randint(0, 4, (5, 5))
cmap = ListedColormap(['red', 'blue', 'grey', 'black']) # (['crimson', 'deepskyblue', 'palegoldenrod', 'midnightblue'])
sns.heatmap(color_numbers, cmap=cmap, annot=labels, fmt='s', cbar=False)
plt.show()

Seaborn automatically will choose between white and black for the text color, depending on the cell color in order to create the largest contrast. The example on the right demonstrates the automatic text color changes.

comparison plot

Upvotes: 1

Related Questions