6

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

1 Answer 1

8

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.