Matplotlib - 如何删除颜色条但保持热图位置不变

Sho*_* Gu 9 python matplotlib python-3.x

我制作了一个带有 3 个轴的图形。每个轴都是具有相同颜色条的热图。我只想保留第三个轴的颜色条并隐藏第一个和第二个颜色条(但保持热图位置不变)。我怎么能做到?

这是我的代码:

fig=plt.figure()
grid = plt.GridSpec(4, 6)

plt.subplot(grid[0:2,0:5])
ax1=sns.heatmap(df_norm, cmap='Reds', vmin=0.05, vmax=0.7)
ax1.axes.get_yaxis().set_visible(False)
ax1.xaxis.tick_top()
ax1.set_xticklabels(col, rotation=90)

plt.subplot(grid[2,0:5])
ax2=sns.heatmap(df_tgfup, cmap='Reds', vmin=0.05, vmax=0.7)
ax2.axes.get_xaxis().set_visible(False)
ax2.axes.get_yaxis().set_visible(False)
ax2.xaxis.tick_top()
ax2.set_xticklabels(col, rotation=90)

plt.subplot(grid[3,0:5])
ax3=sns.heatmap(df_tgfdown, cmap='Reds', vmin=0.05, vmax=0.7)
ax3.axes.get_xaxis().set_visible(False)
ax3.axes.get_yaxis().set_visible(False)
ax3.xaxis.tick_top()
ax3.set_xticklabels(col, rotation=90)
Run Code Online (Sandbox Code Playgroud)

这是我做的无花果:
我做的无花果

这是我想做的无花果:
我想做的无花果

moz*_*way 0

颜色条被定义为图中的轴。您可以在以下位置访问它们fig.axes

[<AxesSubplot:>,
 <AxesSubplot:label='<colorbar>'>,
 <AxesSubplot:>,
 <AxesSubplot:label='<colorbar>'>,
 <AxesSubplot:>,
 <AxesSubplot:label='<colorbar>'>]
Run Code Online (Sandbox Code Playgroud)

您可以使用以下set_visible(False)方法来隐藏它们:

fig=plt.figure()
grid = plt.GridSpec(4, 6)

df_norm = pd.DataFrame(np.random.randint(0,10,size=(10, 10)), columns=list('ABCDEFGHIJ'))

plt.subplot(grid[0:2,0:5])
ax1=sns.heatmap(df_norm, cmap='Reds', vmin=0.05, vmax=0.7)
ax1.axes.get_yaxis().set_visible(False)
ax1.xaxis.tick_top()
#ax1.set_xticklabels(col, rotation=90)
fig.axes[1].set_visible(False)

plt.subplot(grid[2,0:5])
ax2=sns.heatmap(df_norm, cmap='Reds', vmin=0.05, vmax=0.7)
ax2.axes.get_xaxis().set_visible(False)
ax2.axes.get_yaxis().set_visible(False)
ax2.xaxis.tick_top()
#ax2.set_xticklabels(col, rotation=90)
fig.axes[3].set_visible(False)

plt.subplot(grid[3,0:5])
ax3=sns.heatmap(df_norm, cmap='Reds', vmin=0.05, vmax=0.7)
ax3.axes.get_xaxis().set_visible(False)
ax3.axes.get_yaxis().set_visible(False)
ax3.xaxis.tick_top()
#ax3.set_xticklabels(col, rotation=90)
Run Code Online (Sandbox Code Playgroud)

带有隐藏颜色条的多个热图