Visualizing 2D grids and arrays using matplotlib in Python

A 2D grid array plot can be a valuable visualization tool, e.g. in the area of agent-based simulation. In this post I want to give a brief tutorial in how you can visualize a 2D grid array, using matplotlib in Python. The coding example is below; relevant documentation has been added in the form of comments.

# to start with, we will need matplotlib.pyplot
from matplotlib import pyplot
# next, i will set up a 8 x 8 2d matrix, with random bits as elements (0 or 1); 
# for randomization of integers (0 or 1) I use the random module in Python;
# for building each row in the 2d matrix I use list comprehension in Python
import random
data = [[random.randint(a=0,b=1) for x in range(0,8)], # row 1
        [random.randint(a=0,b=1) for x in range(0,8)], # row 2
        [random.randint(a=0,b=1) for x in range(0,8)], # row 3
        [random.randint(a=0,b=1) for x in range(0,8)], # row 4
        [random.randint(a=0,b=1) for x in range(0,8)], # row 5
        [random.randint(a=0,b=1) for x in range(0,8)], # row 6
        [random.randint(a=0,b=1) for x in range(0,8)], # row 7
        [random.randint(a=0,b=1) for x in range(0,8)]] # row 8
# display the 2d data matrix
data
[[1, 1, 0, 0, 1, 1, 1, 0],
 [1, 1, 1, 0, 1, 0, 1, 1],
 [0, 1, 0, 1, 0, 0, 0, 0],
 [1, 1, 0, 0, 1, 1, 0, 1],
 [0, 1, 0, 1, 0, 0, 0, 0],
 [0, 1, 1, 1, 1, 1, 0, 0],
 [0, 0, 1, 0, 0, 0, 1, 1],
 [0, 0, 0, 1, 1, 0, 0, 1]]
# we will visualize the bits of this data matrix with matplot.pyplot; 
# the .imshow function from Python can do the job
pyplot.figure(figsize=(5,5))
pyplot.imshow(data)
pyplot.show()
# .imshow has a range of parameters that I can use for adjusting the 2D grid visualization
# setting "alpha" result in defined transparency
pyplot.imshow(data,
             alpha=0.75)
pyplot.show()
# "cmap" allows for defining a defined coloring map;
# for this we need to import colors from matplotlib
from matplotlib import colors
# using ListedColormap method from the colors package we can define a color map
colormap = colors.ListedColormap(["red","green"])
# handing this problem over to 
pyplot.imshow(data,
             cmap=colormap)
pyplot.show()
# as showing in my documentation on matplotlib in python, we can also e.g. adjust axis ticks or add labels;
# adjusting figure size
pyplot.figure(figsize=(10,10))
# adding labels to x and y axis
pyplot.xlabel("x axis with ticks",
             size = 14)
pyplot.ylabel("y axis with ticks",
             size= 14)
# addding plot title
pyplot.title("this is the title of the plot",
             size=28)
# adjusting the ticks on both x and y axis
pyplot.xticks(size=14,
             color = "red")
pyplot.yticks(size=14, 
              color = "red")
# defining color map
colormap = colors.ListedColormap(["darkblue","lightblue"])
# assigning color map when colling ".imshow" method
pyplot.imshow(data,
             cmap=colormap)
<matplotlib.image.AxesImage at 0x16408e89488>

I will use this approach for visualizing iterations in some agent-based simulation studies to be published on my blog.

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Close

Meta