在matplotlib中绘制黑白二进制图 [英] Plot a black-and-white binary map in matplotlib

查看:594
本文介绍了在matplotlib中绘制黑白二进制图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python来模拟一些自动化模型,并且在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.

我目前正在使用以下命令进行绘制:

I'm currently plotting with the following command:

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

其中self.g是二进制图(在我的当前图中,0->蓝色,1->红色).

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?

推荐答案

您可以通过cmap关键字更改正在使用的颜色图.颜色图'Greys'提供所需的效果.您可以在scipy网站上找到可用地图的列表.

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

其结果是:

这篇关于在matplotlib中绘制黑白二进制图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆