仅显示色彩图并隐藏imshow区域数据

时间:2014-07-13 13:58:38

标签: matplotlib colormap

我试图在一侧有多个imshow行,并且在(右)侧只有色图,没有这个大的白色区域。

having blank large white area beside colormap, wanted to be removed

import numpy as np
import matplotlib.pyplot as plt

font = {'family' : 'sans-serif',
        'color'  : 'k',
        'weight' : 'normal',
        'size'   : 12,
        }

x = "0.2, 0.3, 0.4, 0.5, 0.6".split(",")
y = "180, 175, 170, 169, 150".split(",")
z = [[5000, 4800, 4500, 4450, 4300]]

colors = ('b', 'g', 'r', 'c', 'm', 'y', 'k')

#ax1 = plt.subplot2grid((2,2), (0,0), rowspan=2)
#ax2 = plt.subplot2grid((2,2), (0,1))
#ax3 = plt.subplot2grid((2,2), (1,1))

image = np.random.rand(4,4)

axisNum = 0

numRows = 8

#plt.subplots_adjust(left = 0.125, right = 0.9, bottom = 0.1, top = 0.9, wspace = 0.2, hspace = 1)
plt.subplots_adjust(hspace = .5)
for i in range(numRows):
    ax = plt.subplot2grid((numRows,2), (axisNum,0), aspect='equal')

    im = ax.imshow(z, cmap=plt.cm.Blues, interpolation='nearest')
    xticks = ax.get_xticks()

    top_lables_width_spacings = 0.8
    top_lables_hight_spacings = -.55

    for i in range(len(y)):
        ax.text(xticks[i] + top_lables_width_spacings, top_lables_hight_spacings, y[i], fontdict=font)
    #ax1.set_aspect('auto')

    #fig.colorbar(im, orientation='vertical')

    ax.set_xticks(np.arange(len(x)), minor=False)
    ax.set_xticklabels(x, minor=False)

    ax.tick_params(labelbottom='on',labeltop='off', labelleft="off")


    if axisNum == 0: plt.title('Avg. (s)\n', size=13)

    ax.set_yticklabels([])
    if axisNum != numRows-1: ax.set_xticklabels([])
    elif axisNum == numRows-1:  # the last axes
        ax2 = plt.subplot2grid((numRows,2), (0,1), rowspan=numRows)
        plt.colorbar(im)

    axisNum += 1

plt.show()

从这段代码中可以看出,我使用了上一个imshow轴的可映射数据,然后使用colormap来表示它。

1 个答案:

答案 0 :(得分:1)

如果你不喜欢它,请删除它......

在您的示例图中,您有10个不同的轴(您可以通过plt.gcf().get_axes()进行检查)。其中8个是你绘制的图,第9个是大的白色区域,第10个是色条本身。

所以,你想删除倒数第二个轴:

# in case you do not have the `figure` instance at hand
fig = plt.gcf()

fig.delaxis(fig.get_axes()[-2])

现在丑陋的白色区域消失了。然后你就会遇到剩下的轴没有填满空间的问题。这有点棘手,因为你有很多限制。您可以手动调整位置,但这会导致大量工作。

更好的方法是:

  • 绘制子图
  • 调整它们以采用例如0.8的图像宽度
  • 手动创建新图表
  • 告诉colorbar进入刚刚创建的新轴区域

所以使用你的代码(我在其他地方也修改了代码,只是想猜你想要什么):

import numpy as np
import matplotlib.pyplot as plt

x = "0.2, 0.3, 0.4, 0.5, 0.6".split(",")
y = "180, 175, 170, 169, 150".split(",")
z = [[5000, 4800, 4500, 4450, 4300]]

numRows = 8

fig, subaxes = plt.subplots(nrows=numRows, ncols=1)
axeslist = subaxes.flatten()

for ax in axeslist:
    im = ax.imshow(z, cmap=plt.cm.Blues, interpolation='nearest')

    ax.tick_params(labelbottom='off',labeltop='off', labelleft="off", labelright='off',
                   bottom='off', top='off', left='off', right='off')
    if ax == axeslist[0]:
        ax.set_title('Avg. (s)\n', size=13)
        ax.tick_params(top='on', labeltop='on')        
        ax.set_xticks(range(len(y)))
        ax.set_xticklabels(y)
    elif ax == axeslist[-1]:
        ax.tick_params(bottom='on', labelbottom='on')
        ax.set_xticks(range(len(x)))
        ax.set_xticklabels(x)

# make sure the existing subplots only take 80 % of the image
fig.subplots_adjust(right=.8)

# this uses the last image
# and creates new axes for the colorbar
fig.colorbar(im, cax = fig.add_axes([0.85, 0.1, 0.05, 0.8]))
plt.show()

这给出了:

enter image description here

仍有一些不必要的空间,但如果你想保留aspect='equal'图像,则很难避免,因为子图的水平尺寸由垂直尺寸决定。

如果你准备好用正方形像素给出,那么说:

im = ax.imshow(z, cmap=plt.cm.Blues, interpolation='nearest', aspect='auto')

你会得到:

enter image description here