去除matplotlib热图中的空白
我在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])
1 个回答
6
最简单的方法是使用 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()