从Matplotlib Heatplot中删除空格 [英] Remove whitespace from matplotlib heatplot
问题描述
我在matplotlib中有一个加热图,要删除该加热图的北部和东部的空白,如下图所示.
I have a heatplot in matplotlib for which I want to remove the whitespace to the north and east of the plot, as shown in the image below.
这是我用来生成绘图的代码:
here is the code I'm using to generate the plots:
# 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')
.
The easiest way to do this is to use ax.axis('tight')
.
默认情况下,matplotlib尝试为轴限制选择偶数"数字.如果希望将图缩放到数据的严格限制,请使用ax.axis('tight')
. ax.axis('image')
与之相似,但也会使热图"的单元格变成正方形.
By default, matplotlib tries to choose "even" numbers for the axes limits. If you want the plot to be scaled to the strict limits of your data, use ax.axis('tight')
. ax.axis('image')
is similar, but will also make the cells of your "heatmap" square.
例如:
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()
这篇关于从Matplotlib Heatplot中删除空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!