【问题标题】:How can I remove colormaps in matplotlib? [duplicate]如何删除 matplotlib 中的颜色图? [复制]
【发布时间】:2019-02-07 23:16:31
【问题描述】:

我有以下代码:

fig, ax = plt.subplots()
fig.set_size_inches(10, 10, forward=True)
min_val, max_val = 0, 28

inputBild = np.round(np.reshape(inputBild, [28, 28]), 1)
plt.imshow(inputBild)

for i in range(28):
    for j in range(28):
        c = inputBild[j,i]
        ax.text(i, j, str(c), va='center', ha='center')
ax.set_facecolor((1.0, 0.47, 0.42))
plt.savefig('/tmp/inputMitZahlOhneCmap.png', bbox_inches='tight')

inputBild 是 mnist 数据集的随机图像。

我只想绘制数字而不是颜色图。 如果我什至没有指定一个,我该如何删除它?

【问题讨论】:

  • 如果您评论plt.imshow(inputBild) 行会发生什么?您所说的颜色图是指图表上的颜色?
  • 目前完全不清楚您的数据集是什么,所需的输出图像是什么样的。更加详细一些。 inputBild 是什么?
  • 颜色图将数值映射到像素颜色。这意味着,从技术上讲,您不能在没有颜色图的情况下显示任何图像。但我想这个问题要求将值映射到灰度颜色而不是 RGB 颜色。这将显示为例如在this question。 IE。您可以使用“Greys”、“gray”、“gist_gray”、“gist_yarg”或“binary”中的任何一个作为颜色图。示例:plt.imshow(inputBild, cmap="Greys")
  • 我试图将数字作为字符串绘制到我的矩阵上。我并不是要为每个值都着色。在@Bazingaa 的答案中,它被绘制为颜色,但我希望没有颜色,只有原始数字。我没有也不想要颜色条我的 inputBild 只是 MNIST-Dataset 的一个向量。

标签: matplotlib colormap


【解决方案1】:

当你说“只绘制数字”时,我不确定你是否想要散点图,但看看你对imshow 的使用,我想你想隐藏颜色条。这是一个例子:

带彩条

import matplotlib.pyplot as plt 
import numpy as np
fig = plt.figure() 
ax = fig.add_subplot(111) 
data = np.random.rand(100, 100) 
cax = ax.imshow(data) 
cbar = plt.colorbar(cax) # This line includes the color bar

无彩条

import matplotlib.pyplot as plt 
import numpy as np
fig = plt.figure() 
ax = fig.add_subplot(111) 
data = np.random.rand(100, 100) 
cax = ax.imshow(data) 

删除彩条:方法一(不显示初始代码行)

data = np.random.rand(100, 100) 
cax = ax.imshow(data) 
cbar = plt.colorbar(cax) 
cbar.remove()

删除彩条:方法二(不显示初始代码行)

data = np.random.rand(100, 100) 
cax = ax.imshow(data) 
plt.colorbar(cax) 
plt.delaxes(fig.axes[1]) # axes[0] correspond to the main plot.

输出方法一和二

【讨论】:

    猜你喜欢
    • 2011-07-12
    • 2018-03-31
    • 1970-01-01
    • 2015-02-24
    • 2017-11-12
    • 2021-08-09
    • 2023-03-28
    • 2019-03-29
    • 2020-09-22
    相关资源
    最近更新 更多