Skip to content

Instantly share code, notes, and snippets.

@ssshukla26
Last active September 2, 2021 16:20
Show Gist options
  • Save ssshukla26/0b04c93aecce97ce422cb5591ccc866d to your computer and use it in GitHub Desktop.
Save ssshukla26/0b04c93aecce97ce422cb5591ccc866d to your computer and use it in GitHub Desktop.
Draw Matrix With Text
from matplotlib import pyplot as plt
import numpy as np
m = [
[1,1,0,1,0,0,1,1,0,0],
[0,1,1,1,1,1,1,1,0,0],
[0,0,1,0,1,1,0,0,0,0],
[0,0,0,1,1,0,1,1,0,0],
[0,0,0,1,0,0,1,0,1,0],
[0,0,0,0,1,1,1,1,1,1],
[0,0,0,0,1,1,1,0,1,0],
[0,0,0,0,1,1,1,1,1,0],
[0,0,0,0,0,1,1,0,0,0],
[0,0,0,0,0,0,1,0,0,0]
]
h = len(m)
w = len(m[0])
fig, ax = plt.subplots(facecolor="white", figsize=(w//2,h//2))
ax.matshow(m, cmap=plt.cm.winter)
for row in range(h):
for col in range(w):
c = m[row][col]
ax.text(col, row, str(c), va='center', ha='center')
plt.xticks([i for i in range(w)])
plt.yticks([i for i in range(h)])
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment