需要畫一個這樣的熱圖:
heatmap:
下面介紹具體過程:
1.原始數據為兩個矩陣:rmsd_array,tm_rmsd_array
<class 'numpy.ndarray'>
array([[ 0. , 15.289, 10.037, ..., 10.173, 18.827, 7.309], [15.289, 0. , 19.317, ..., 19.324, 24.559, 12.865], [10.037, 19.317, 0. , ..., 9.504, 15.224, 12.05 ], ..., [10.173, 19.324, 9.504, ..., 0. , 14.592, 12.491], [18.827, 24.559, 15.224, ..., 14.592, 0. , 20.118], [ 7.309, 12.865, 12.05 , ..., 12.491, 20.118, 0. ]])
array([[1. , 0.3929, 0.6781, ..., 0.494 , 0.3506, 0.6443], [0.3905, 1. , 0.3506, ..., 0.3536, 0.2721, 0.4333], [0.6914, 0.3591, 1. , ..., 0.5358, 0.3766, 0.4738], ..., [0.4976, 0.3587, 0.531 , ..., 1. , 0.3905, 0.4789], [0.3588, 0.2798, 0.3791, ..., 0.3962, 1. , 0.3304], [0.6454, 0.4366, 0.4668, ..., 0.476 , 0.3236, 1. ]])
2.將兩個矩陣分別畫出熱圖的方法:
1 import numpy as np 2 import seaborn as sns 3 import pandas as pd 4 import matplotlib.pyplot as plt 5 sns.set() 6 7 ax = sns.heatmap(tm_array,cmap='bwr_r') 8 # 將x軸刻度放置在top位置的幾種方法 9 # ax.xaxis.set_ticks_position(‘top‘) 10 #ax.xaxis.tick_top() 11 # ax.tick_params(axis=‘x‘,labelsize=6, colors=‘b‘, labeltop=True, labelbottom=False) 12 # 設置坐標軸刻度的字體大小 13 # matplotlib.axes.Axes.tick_params 14 ax.tick_params(labelsize=8) # y軸 15 # 旋轉軸刻度上文字方向的兩種方法 16 ax.set_yticklabels(ax.get_yticklabels(), rotation=0) 17 ax.set_xticklabels(ax.get_xticklabels(), rotation=0) 18 plt.savefig('tm_array.png', dpi=300) #指定分辨率保存 19 plt.show()
result:
3.保留熱圖的上三角或下三角:(triu和tril對應上下三角)
1 corr = np.corrcoef(tm_array) 2 mask = np.zeros_like(corr) 3 4 mask[np.tril_indices_from(mask)] = True 5 with sns.axes_style("white"): 6 ax = sns.heatmap(corr, mask=mask, vmax=1, vmin=0,cmap='bwr_r', square=True) 7 ax.tick_params(labelsize=8) # y軸 8 # 旋轉軸刻度上文字方向的兩種方法 9 ax.set_yticklabels(ax.get_yticklabels(), rotation=0) 10 ax.set_xticklabels(ax.get_xticklabels(), rotation=0) 11 #plt.savefig('tm_array.png', dpi=300) #指定分辨率保存 12 plt.show()
4.將兩個矩陣的上下三角分別取出並合並即可畫出合並圖:
1 up_rmsd=np.triu(rmsd_array, k=1)# Upper triangle of an array 2 lower_tm=np.tril(tm_array, k=-1)# Lower triangle of an array. 3 montage_array= up_rmsd+lower_tm 4 5 print(montage_array)
[[ 0. 15.289 10.037 ... 10.173 18.827 7.309 ] [ 0.3905 0. 19.317 ... 19.324 24.559 12.865 ] [ 0.6914 0.3591 0. ... 9.504 15.224 12.05 ] ... [ 0.4976 0.3587 0.531 ... 0. 14.592 12.491 ] [ 0.3588 0.2798 0.3791 ... 0.3962 0. 20.118 ] [ 0.6454 0.4366 0.4668 ... 0.476 0.3236 0. ]]
但是,我們的兩個矩陣元素不是一個衡量標准:rmsd_array和tm_array的值范圍分別為(0,25),(0,1)
如果不做處理畫出來是這樣的:
5,由上,需要先對rmsd_array的值做一下歸一化:
1 ''' 2 Tips:歸一化處理normalization也可以自己用python寫,這里為了方便就調用現成的包了 3 def MaxMinNormalization(x,Max,Min): 4 x = (x - Min) / (Max - Min); 5 return x; 6 ''' 7 from sklearn import preprocessing 8 #歸一化處理normalization 9 min_max_scaler = preprocessing.MinMaxScaler() 10 rmsd_array_normal=min_max_scaler.fit_transform(rmsd_array)
up_rmsd=np.triu(rmsd_array_normal, k=1)# Upper triangle of an array lower_tm=np.tril(tm_array, k=-1)# Lower triangle of an array. montage_array= up_rmsd+lower_tm print(montage_array)
[[0. 0.54586026 0.43536913 ... 0.44195847 0.71149994 0.29112563] [0.3905 0. 0.83790232 ... 0.8395169 0.92812063 0.51242731] [0.6914 0.3591 0. ... 0.41289426 0.57533729 0.47996495] ... [0.4976 0.3587 0.531 ... 0. 0.55145308 0.49753047] [0.3588 0.2798 0.3791 ... 0.3962 0. 0.80132239] [0.6454 0.4366 0.4668 ... 0.476 0.3236 0. ]]
6,將處理后的矩陣 montage_array重新畫一下熱圖,就得到了想要的結果。
over!