简体   繁体   English

matplotlib热图的中心对齐刻度标签

[英]Center-align tick labels of matplotlib heatmap

I have plotted a heat map with imshow, but I can't get the x tick labels to be center aligned even though I made ha='center' . 我已经用imshow绘制了一张热图,但即使我做了ha='center' ,我也无法让x tick标签居中对齐。

Here is my code: 这是我的代码:

font= mpl.rcParams['font.size']=8.0
lineWidth= mpl.rcParams['lines.linewidth']= 1.0
absolute_max=abs(max_num)
absolute_min=abs(min_num)
cb_boundary=max(absolute_max,absolute_min)
tree=Phylo.read("reorder.nwk","newick")
a=1.0

cdict = {'red':  ((0.0, 0.0, 0.0),
               (0.25,0.0, 0.0),
               (0.5, 0.8, 1.0),
               (0.75,1.0, 1.0),
               (1.0, 0.4, 1.0)),

     'green': ((0.0, 0.0, 0.0),
               (0.25,0.0, 0.0),
               (0.5, 0.9, 0.9),
               (0.75,0.0, 0.0),
               (1.0, 0.0, 0.0)),

     'blue':  ((0.0, 0.0, 0.4),
               (0.25,1.0, 1.0),
               (0.5, 1.0, 0.8),
               (0.75,0.0, 0.0),
               (1.0, 0.0, 0.0))
    }

plt.register_cmap(name='BlueRed', data=cdict)
norm = mpl.colors.Normalize(vmin=-3.0, vmax=3.0)
cmap = plt.get_cmap('BlueRed')
masked_array = np.ma.masked_where(full_len==np.NaN,full_len)
cmap.set_bad('green',1.0)
fig= plt.figure(figsize=(19,10))
rect_phyl = [-0.7, 0.3, 0.2, 0.6]
rect_ht = [-0.5,0.3 , 0.3, 0.6]
phyl_ax = plt.axes(rect_phyl,frameon=True)
ht_ax = plt.axes(rect_ht)    
##fig.suptitle(file_handle.replace('_del.csv',''),fontsize=22)
phyl_ax.add_patch(Rectangle((8.1,17.6),6.5,16.9,edgecolor="brown", fill=False))
phyl_ax.add_patch(Rectangle((8.1,10.4),6.5,7.0,edgecolor="magenta", fill=False))
phyl_ax.add_patch(Rectangle((8.1,7.6),6.5,2.6,edgecolor="black", fill=False))
phyl_ax.add_patch(Rectangle((8.1,0.3),6.5,6.9,edgecolor="turquoise", fill=False))    
fig.subplots_adjust(hspace=0,wspace=0)
Phylo.draw(tree, axes=phyl_ax, do_show=False,show_confidence=False)    

ht_ax.set_xlim(0,34)
ht_ax.set_ylim(0,34)
phyl_ax.set_xlim(0,15)

divider = make_axes_locatable(ht_ax)
cbax = divider.append_axes("right", size="5%", pad=0.10)
phyl_ax.set(xlabel='',ylabel='') 
plt.setp(phyl_ax.get_xticklabels(),visible=False)
plt.setp(phyl_ax.get_yticklabels(),visible=False)
plt.setp(ht_ax.get_xticklabels(),visible=True)
plt.setp(ht_ax.get_yticklabels(),visible=False)
plt.setp(phyl_ax.get_xticklines(),visible=False)
plt.setp(phyl_ax.get_yticklines(),visible=False)
plt.setp(ht_ax.get_xticklines(),visible=False)
plt.setp(ht_ax.get_yticklines(),visible=False)
img = ht_ax.imshow(masked_array, cmap=cmap, interpolation='none',aspect='auto',vmin=-cb_boundary,vmax=cb_boundary,extent=[34,0,34,0],origin='lower')
xticks=range(34)
ht_ax.xaxis.set_ticks(xticks)
ht_ax.yaxis.set_ticks(xticks)
ht_ax.grid(True, which='both')
ha = ['right', 'center', 'left']
ht_ax.set_xticklabels(txtnames,rotation=45,fontsize=8,ha=ha[1],minor=False)

plt.colorbar(img, cax=cbax)
heatmap_file=fig.savefig('/home/Desktop/heatmap/'+file_handle.replace('.csv','')+'.pdf',bbox_inches='tight',dpi=150)

Can someone help me find the error? 有人可以帮我找到错误吗?

The example below shows that the alignment setting is not too difficult. 下面的示例显示对齐设置并不太难。 It is possible that you have some interfering commands before the ht_ax.set_xticklabels call; ht_ax.set_xticklabels调用之前,您可能有一些干扰命令; and in particular, manipulating an axis before the call to imshow rather than after might not have the desired response, since imshow , like many/most plotting commands, includes a basic configuration of the axes. 特别是,在调用imshow 之前操纵轴而不是之后可能没有所需的响应,因为imshow像许多/大多数绘图命令一样,包括轴的基本配置。

Can you strip back much of your configuration? 你可以剥掉你的大部分配置吗?

# some random data to show
I = np.random.rand(25, 35) 
# and some labels that are quite long (so differences are visible)
labels = range(0, 30000, 5000)

fig, ax = plt.subplots(3, 1, num=1)
ha = 'center'
ax[0].imshow(I, interpolation='none',aspect='auto')
ax[0].set_xticklabels(labels, rotation=45, ha='left', minor=False)

ax[1].imshow(I, interpolation='none',aspect='auto')
ax[1].set_xticklabels(labels, rotation=45, ha='center', minor=False)

ax[2].imshow(I, interpolation='none',aspect='auto')
ax[2].set_xticklabels(labels, rotation=45, ha='right', minor=False)

plt.subplots_adjust(hspace=0.5)
plt.show()

标签方向

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM