matplotlib.pyplot画灰度图

以画MNIST手写体为例:

import matplotlib.pyplot as plt

from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets

mnist = read_data_sets('MNIST_data', one_hot=False)
x, y = mnist.test.next_batch(1)
x = x.reshape([28, 28])

fig = plt.figure()
# Method1 
ax1 = fig.add_subplot(221)
ax1.imshow(x, cmap=plt.cm.gray)

# Method2: 反转色
ax2 = fig.add_subplot(222)
ax2.imshow(x, cmap=plt.cm.gray_r) # r表示reverse

# Method3(等价于Method1)
ax3 = fig.add_subplot(223)
ax3.imshow(x, cmap='gray')

# Method4(等价于Method2)
ax4 = fig.add_subplot(224)
ax4.imshow(x, cmap='gray_r')

plt.show() 
plt.savefig("gray.png")



你可能感兴趣的:(Python,matplotlib)