【问题标题】:Remove whitespace from matplotlib heatplot从 matplotlib 热图中删除空格
【发布时间】:2014-06-19 12:11:19
【问题描述】:

我在 matplotlib 中有一个热图,我想删除该图北部和东部的空白,如下图所示。

这是我用来生成绘图的代码:

# plotting
figsize=(50,20)
y,x = 1,2
fig, axarry = plt.subplots(y,x, figsize=figsize)
p = axarry[1].pcolormesh(copy_matrix.values)

# put the major ticks at the middle of each cell
axarry[1].set_xticks(np.arange(copy_matrix.shape[1])+0.5, minor=False)
axarry[1].set_yticks(np.arange(copy_matrix.shape[0])+0.5, minor=False)

axarry[1].set_title(file_name, fontweight='bold')

axarry[1].set_xticklabels(copy_matrix.columns, rotation=90)
axarry[1].set_yticklabels(copy_matrix.index)

fig.colorbar(p, ax=axarry[1])

Phylo.draw(tree, axes=axarry[0])

【问题讨论】:

    标签: python numpy matplotlib


    【解决方案1】:

    最简单的方法是使用ax.axis('tight')

    默认情况下,matplotlib 尝试为轴限制选择“偶数”数。如果您希望将绘图缩放到数据的严格限制,请使用ax.axis('tight')ax.axis('image') 类似,但也会使“热图”的单元格变为正方形。

    例如:

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Note the non-"even" size... (not a multiple of 2, 5, or 10)
    data = np.random.random((73, 78))
    
    fig, axes = plt.subplots(ncols=3)
    for ax, title in zip(axes, ['Default', 'axis("tight")', 'axis("image")']):
        ax.pcolormesh(data)
        ax.set(title=title)
    
    axes[1].axis('tight')
    axes[2].axis('image')
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2023-03-18
      • 2018-09-29
      • 1970-01-01
      • 2021-12-21
      • 2014-03-08
      • 2020-12-25
      • 2021-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多