如何通过matplotlib.pyplot.imshow绘制灰度图像? [英] How to plot gray level image by matplotlib.pyplot.imshow?

查看:158
本文介绍了如何通过matplotlib.pyplot.imshow绘制灰度图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据是numpy.array格式的2D图像.通过以下代码:

My data is 2D image in format of numpy.array. By the following code:

fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(visu_base)
plt.show()

我从Python获得彩色图像.但是,如何显示灰度呢?

I get a color image from Python. But, how can I show it in gray level?

推荐答案

答案在imshow命令[1]

The answer is in the api docs of the imshow command[1]

cmap : Colormap, optional, default: None
    If None, default to rc image.cmap value. cmap is ignored when X has RGB(A) information

imshow 接受一个可选参数 cmap,它控制颜色映射.

imshow takes an optional parameter cmap, which controls the color mapping.

此处可以找到 matplotlib[2] 中定义的可用颜色映射的概述

Here can find an overview of the available color mappings defined in matplotlib[2]

您可以执行以下操作:

import matplotlib as mpl
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(visu_base, cmap=mpl.cm.gray)
plt.show()

这篇关于如何通过matplotlib.pyplot.imshow绘制灰度图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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