Thomas Britton
Thomas Britton

Reputation: 123

Numpy Array to Graph

I have a simple 2D Numpy array consisting of 0s and 1s. Is there a simple way to make a graph that will shade in corresponding coordinates?

For example if my array was [[1,0],[0,1]] The plot would be a 2x2 square with the top left and bottom right shaded in

Upvotes: 6

Views: 5254

Answers (1)

Simon Gibbons
Simon Gibbons

Reputation: 7194

You can use matplotlib to plot a matrix for you.

Use the matshow command with an appropriate colourmap to produce the plot.

For example

import numpy as np
import matplotlib.pyplot as plt

x = np.array([[1,0],[0,1]])
plt.matshow(x, cmap='Blues')
plt.show()

would produce:

matshow example

Upvotes: 8

Related Questions