Plot a black-and-white binary map in matplotlib

92,644

Solution 1

You can change the color map you are using via the cmap keyword. The color map 'Greys' provides the effect you want. You can find a list of available maps on the scipy website.

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(101)
g = np.floor(np.random.random((100, 100)) + .5)

plt.subplot(211)
plt.imshow(g)
plt.subplot(212)
plt.imshow(g, cmap='Greys',  interpolation='nearest')
plt.savefig('blkwht.png')

plt.show()

which results in:

enter image description here

Solution 2

There is an alternative method to Yann's answer that gives you finer control. Matplotlib's imshow can take a MxNx3 matrix where each entry is the RGB color value - just set them to white [1,1,1] or black [0,0,0] accordingly. If you want three colors it's easy to expand this method.

import matplotlib.pyplot as plt
import numpy as np

# Z is your data set
N = 100
Z = np.random.random((N,N))

# G is a NxNx3 matrix
G = np.zeros((N,N,3))

# Where we set the RGB for each pixel
G[Z>0.5] = [1,1,1]
G[Z<0.5] = [0,0,0]

plt.imshow(G,interpolation='nearest')
plt.show()

enter image description here

Share:
92,644
Tomas Aschan
Author by

Tomas Aschan

I am an engineering physicist from Stockholm, Sweden, with a passionate interest in programming and software architecture. Since creating my first program at age 12 (a VB6 app that showed a smiley when a button was clicked) I've spent many hours in front of my computer, watching screen casts and reading blogs about programming as well as trying all the new concepts out in my own programs. With a Master's degree in Engineering Physics from the Royal Institute of Technology in Stockholm, Sweden, I have deepened my modelling and reasoning skills, as well as had the opportunity to try out many different technologies and tools. I am currently working as a software engineer at Spotify, mostly massaging data to enable our internal research into developer productivity.

Updated on May 09, 2020

Comments

  • Tomas Aschan
    Tomas Aschan about 4 years

    I'm using python to simulate some automation models, and with the help of matplotlib I'm producing plots like the one shown below.

    enter image description here

    I'm currently plotting with the following command:

    ax.imshow(self.g, cmap=map, interpolation='nearest')
    

    where self.g is the binary map (0 -> blue, 1 -> red in my current plots).

    However, to include this in my report I would like the plot to be with black dots on white background instead of red on blue. How do I accomplish that?

  • Avaris
    Avaris over 12 years
    You can just give the name of the colormap to cmap. plt.imshow(g, cmap="Greys") would do the same thing.
  • touchStone
    touchStone over 8 years
    you may also use plt.gray() at the beginning to get similar results.
  • Nick Crews
    Nick Crews about 7 years
    In my version of matplotlib (1.5.2rc2) I had to use cmap='gray' . Guess they changed the names a bit. If you do it wrong though, it prints out all the options, which is nice.