Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
126 views
in Technique[技术] by (71.8m points)

python - Plot a black-and-white binary map in matplotlib

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...