Reputation: 4102
I have two values in a numpy array. 0,1. I want to ensure 1 is always black and 0 is always grey when I graph the array.
How can I do this in matplotlib?
Thank you
Upvotes: 0
Views: 42
Reputation: 339230
Assuming that you want to plot an image plot (plt.imshow()
), you can select a colormap like "gray_r" which has white as lowest and black as highest color, gray will be in the middle. If you now normalize the image plot to values between -1 and 1, the array's 0 value will correspond to the middle of the colormap (being gray) and 1 will correspond to the upper end of the colormap (being black).
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(0)
a = np.random.randint(0,2, size=(12,25))
plt.imshow(a, cmap="gray_r", vmin=-1, vmax=1)
plt.show()
Upvotes: 3