0

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

1
  • Which kind of plot do you want to use? Please also read How to Ask. Commented Apr 6, 2017 at 12:31

1 Answer 1

3

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()

enter image description here

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.