matplotlib在热图和自制注释轴之间共享轴

matplotlib在热图和自制注释轴之间共享轴,matplotlib,axis,heatmap,Matplotlib,Axis,Heatmap,我很难在热图和自制注释轴之间正确共享x轴。我使用的是matplotlib2.2.2(我从matplotlib 1.5升级,非常遗憾) 以下是我的默认代码和结果行为: mn = np.random.rand(2467, 2467) list_coordinates = [0, 51, 220, 289, 605, 720, 776, 995, 1108, 1195, 1348, 1485, 1702, 1888, 2047, 2269,2467] list_names = ['I', 'II',

我很难在热图和自制注释轴之间正确共享x轴。我使用的是matplotlib2.2.2(我从matplotlib 1.5升级,非常遗憾)

以下是我的默认代码和结果行为:

mn = np.random.rand(2467, 2467)
list_coordinates = [0, 51, 220, 289, 605, 720, 776, 995, 1108, 1195, 1348, 1485, 1702, 1888, 2047, 2269,2467]
list_names = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII', 'XIII', 'XIV', 'XV', 'XVI', ]


# Prepare figure and axes
fig = plt.figure(figsize=(12, 14))
gs = gridspec.GridSpec(3, 1, hspace=0.05, wspace=0.1, height_ratios=[1, 12, 1])
main_ax = plt.subplot(gs[1])
legend_ax = plt.subplot(gs[0], sharex=main_ax)    

# Contact map
im = main_ax.imshow(mn**0.2, cmap='afmhot_r', vmin=0, vmax=0.9)

# Legend
colors = [name for name, hex in mcolors.cnames.items()]
for i, pos in enumerate(list_coordinates[:-1]):
    legend_ax.axvspan(list_coordinates[i], list_coordinates[i+1], 0, 0.1, color=colors[i])
    legend_ax.text((list_coordinates[i] + list_coordinates[i+1])/2, 0.2, list_names[i].replace('chr', ''), horizontalalignment='center', size='small')
legend_ax.set_axis_off()

# Show
plt.show()
(我删除了代码的颜色条部分,使其更短)

我想摆脱丑陋的白色侧栏。当我尝试添加行时

x0, x1 = main_ax.get_xlim()
在创建热图和

main_ax.set_xlim(x0, x1)
最后,上栏变得太大:

有人知道我该怎么纠正吗


谢谢

之所以发生这种情况,是因为当您使用
axvspan
绘制矩形时,它通过添加边距来调整两个子地块的x轴。您可以通过以下方式手动将x边距设置为0来停止此情况:

完整代码:

mn = np.random.rand(2467, 2467)
list_coordinates = [0, 51, 220, 289, 605, 720, 776, 995, 1108, 1195, 1348, 1485, 1702, 1888, 2047, 2269,2467]
list_names = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII', 'XIII', 'XIV', 'XV', 'XVI', ]

# Prepare figure and axes
fig = plt.figure(figsize=(12, 14))
gs = gridspec.GridSpec(3, 1, hspace=0.05, wspace=0.1, height_ratios=[1, 12, 1])
main_ax = plt.subplot(gs[1])
legend_ax = plt.subplot(gs[0], sharex=main_ax)
legend_ax.margins(x=0) # manually set x margin to 0

# Contact map
im = main_ax.imshow(mn**0.2, cmap='afmhot_r', vmin=0, vmax=0.9)

# Legend
colors = [name for name, hex in mcolors.cnames.items()]
for i, pos in enumerate(list_coordinates[:-1]):
    legend_ax.axvspan(list_coordinates[i], list_coordinates[i+1], 0, 0.1, color=colors[i])
    legend_ax.text((list_coordinates[i] + list_coordinates[i+1])/2, 0.2, list_names[i].replace('chr', ''), horizontalalignment='center', size='small')
legend_ax.set_axis_off()

# Show
plt.show()
给出类似于:


您能提供
列表坐标
列表名称
以便我们运行代码吗?是的,当然,对不起。好了,完成了。
mn = np.random.rand(2467, 2467)
list_coordinates = [0, 51, 220, 289, 605, 720, 776, 995, 1108, 1195, 1348, 1485, 1702, 1888, 2047, 2269,2467]
list_names = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII', 'XIII', 'XIV', 'XV', 'XVI', ]

# Prepare figure and axes
fig = plt.figure(figsize=(12, 14))
gs = gridspec.GridSpec(3, 1, hspace=0.05, wspace=0.1, height_ratios=[1, 12, 1])
main_ax = plt.subplot(gs[1])
legend_ax = plt.subplot(gs[0], sharex=main_ax)
legend_ax.margins(x=0) # manually set x margin to 0

# Contact map
im = main_ax.imshow(mn**0.2, cmap='afmhot_r', vmin=0, vmax=0.9)

# Legend
colors = [name for name, hex in mcolors.cnames.items()]
for i, pos in enumerate(list_coordinates[:-1]):
    legend_ax.axvspan(list_coordinates[i], list_coordinates[i+1], 0, 0.1, color=colors[i])
    legend_ax.text((list_coordinates[i] + list_coordinates[i+1])/2, 0.2, list_names[i].replace('chr', ''), horizontalalignment='center', size='small')
legend_ax.set_axis_off()

# Show
plt.show()