【问题标题】:limit columns in matplotlib heatmap限制 matplotlib 热图中的列
【发布时间】:2013-03-12 21:40:16
【问题描述】:

我正在尝试在 matplotlib 中绘制热图并以此为基础:Moving x-axis to the top of a plot in matplotlib

问题是我只有三列但有 70 行,但当我使用示例时,该图始终有 70 行和 70 列。有人知道如何限制适合我的数据的列数吗?

【问题讨论】:

    标签: python matplotlib heatmap


    【解决方案1】:

    您在我的代码中发现了一个错误。我将索引(data.shape)反转了。

    import matplotlib.pyplot as plt
    import numpy as np
    column_labels = list('ABC')
    row_labels = range(7)
    data = np.random.rand(7,3)
    fig, ax = plt.subplots()
    heatmap = ax.pcolor(data, cmap=plt.cm.Blues)
    
    # put the major ticks at the middle of each cell
    ax.set_xticks(np.arange(data.shape[1])+0.5, minor=False)
    ax.set_yticks(np.arange(data.shape[0])+0.5, minor=False)
    
    # want a more natural, table-like display
    ax.invert_yaxis()
    ax.xaxis.tick_top()
    
    ax.set_xticklabels(column_labels, minor=False)
    ax.set_yticklabels(row_labels, minor=False)
    plt.show()
    

    产量

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-15
      • 2020-06-20
      • 1970-01-01
      • 2016-01-21
      • 2020-03-31
      • 1970-01-01
      • 2016-08-25
      • 1970-01-01
      相关资源
      最近更新 更多