我在matplotlib中有一个heatplot,我想删除绘图北部和东部的空白,如下图所示。
下面是我用来生成绘图的代码:
# 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])
最佳答案
最简单的方法是使用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()
关于python - 从Matplotlib Heatplot中删除空格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24306316/