Last active
September 2, 2021 16:20
-
-
Save ssshukla26/0b04c93aecce97ce422cb5591ccc866d to your computer and use it in GitHub Desktop.
Draw Matrix With Text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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