Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 918
Thomas Britton Avatar asked Jan 30 '15 19:01

Thomas Britton


People also ask

How do you plot a graph using NumPy array?

For plotting graphs in Python, we will use the Matplotlib library. Matplotlib is used along with NumPy data to plot any type of graph. From matplotlib we use the specific function i.e. pyplot(), which is used to plot two-dimensional data.

How do I show an array in Matplotlib?

We can show arrays as images using the plt. imshow command from matplotlib. Here is the default output: >>> plt.


1 Answers

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

like image 108
Simon Gibbons Avatar answered Oct 26 '22 22:10

Simon Gibbons