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
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.
We can show arrays as images using the plt. imshow command from matplotlib. Here is the default output: >>> plt.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With