I have a list of cells:
my_path = [(4, 4), (4, 3), (3, 3), (2, 3), (2, 4), (1, 4), (0, 4), (0, 3), (0, 2), (1, 2), (1, 1), (1, 0), (0, 0)]
that represent a path through a 5x5 grid. I'm looking for a straightforward way of drawing the path and grid something like this (as long as they contrast, the actual colours aren't important):
I've looked at things like Python - Plotting colored grid based on values, Drawing grid pattern in matplotlib, custom matplotlib plot : chess board like table with colored cells, but they seem too complicated for what I want. Thank you.
what do you mean too complicated?
the code in your first link. posted here just for a reminder
import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np
data = np.random.rand(10, 10) * 20
# create discrete colormap
cmap = colors.ListedColormap(['red', 'blue'])
bounds = [0,10,20]
norm = colors.BoundaryNorm(bounds, cmap.N)
fig, ax = plt.subplots()
ax.imshow(data, cmap=cmap, norm=norm)
# draw gridlines
ax.grid(which='major', axis='both', linestyle='-', color='k', linewidth=2)
ax.set_xticks(np.arange(-.5, 10, 1));
ax.set_yticks(np.arange(-.5, 10, 1));
plt.show()
can be modified easily with
# imports, duh
import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np
# putting your path in a grid that you can actually use
# because here's a bunch of numbers, now what?
my_path = [(4, 4), (4, 3), (3, 3), (2, 3), (2, 4), (1, 4), (0, 4), (0, 3), (0, 2), (1, 2), (1, 1), (1, 0), (0, 0)]
data = np.zeros((5, 5))
for x,y in my_path:
data[x][y] = 1
# create discrete colormap
cmap = colors.ListedColormap(['red', 'blue'])
bounds = [0,1,2] # boundary at value >=1
norm = colors.BoundaryNorm(bounds, cmap.N)
# create the canvas you're drawing on
fig, ax = plt.subplots()
ax.imshow(data, cmap=cmap, norm=norm)
# draw gridlines
ax.grid(which='major', axis='both', linestyle='-', color='k', linewidth=2)
ax.set_xticks(np.arange(-.5, 5, 1)); # make them align with edges
ax.set_yticks(np.arange(-.5, 5, 1));
plt.show()