y的热图轴未在Python

时间:2016-08-03 20:15:56

标签: python matplotlib heatmap

我正在使用子图生成4个热图,并希望y轴反转。我正在使用ax[i].invert_yaxis()但它似乎没有用。代码示例如下:

everything = [data_y_axis, data_length, data_roundness, data_y_SDev]
legend = ['title1', 'title2', 'title3', 'title4']

fig, ax = plt.subplots(nrows = 1, ncols = 4, sharex = False, sharey = True, figsize = (13,3))

ax = ax.flatten()

for i, v in enumerate(everything):

    heatmap = ax[i].pcolor(v, cmap=plt.cm.Blues)

    ax[i].invert_yaxis()
    ax[i].xaxis.tick_top()
    ax[i].set_xticks(np.arange(v.shape[1])+0.5, minor=False)
    ax[i].set_xticklabels(column_labels, minor=False)
    ax[i].set_yticks(np.arange(v.shape[0])+0.5, minor=False)
    ax[i].set_yticklabels(row_labels, minor=False, fontproperties = titlefont)
    ax[i].set_xticklabels(column_labels, minor=False, fontproperties = titlefont)

    cb = fig.colorbar(heatmap, orientation='horizontal', pad=0.02, ax = ax[i])
    cb.set_label(label = legend[i], fontproperties = titlefont)
    cb.outline.set_linewidth(2)

everything中的项目只是np.arrays,所有项目都具有相同的形状(4,6)。

目前正在制作这样的地图:enter image description here,如下所示:enter image description here

我做错了什么吗?

1 个答案:

答案 0 :(得分:0)

啊哈。我已经解决了。我从ax[i].invert_yaxis()循环中删除了for并将其粘贴到最后。它现在有效:

for i, v in enumerate(everything):

    heatmap = ax[i].pcolor(v, cmap=plt.cm.Blues)

    ax[i].xaxis.tick_top()
    ax[i].set_xticks(np.arange(v.shape[1])+0.5, minor=False)
    ax[i].set_xticklabels(column_labels, minor=False)
    ax[i].set_yticks(np.arange(v.shape[0])+0.5, minor=False)
    ax[i].set_yticklabels(row_labels, minor=False, fontproperties = titlefont)
    ax[i].set_xticklabels(column_labels, minor=False, fontproperties = titlefont)

    cb = fig.colorbar(heatmap, orientation='horizontal', pad=0.02, ax = ax[i])
    cb.set_label(label = legend[i], fontproperties = titlefont)
    cb.outline.set_linewidth(2)

ax[0].invert_yaxis()