0

所以我的问题是,我想将几​​个热图绘制为一个多图,其中我有 X、Y 轴对数,并且还向热图添加补丁,以突出显示具有调整 alpha 值的某些区域(见附图)。问题是,使用 pcolormesh,您可以轻松地对 X,Y 使用 logscale,因为它使用精确的数据来显示,但是 seaborn 可以只使用数据的索引。另一方面,使用 pcolormesh 我没有找到添加 pacthes 的方法,但是使用 seaborn 很容易做到......

所以我想提出一个解决方案,其中可以完成对数缩放和添加 pacthes 到绘图......

我添加了一部分代码,并附上了一张我可以用 seaborn 做的图片。

所以这里是 seaborn heatmap multiplot 的代码,在 jupyter-notebook 中完成了 sime custom x,ytics:

def HeatMapPlotter(alphaval,im_w,im_h,tissue_type,hl_x_min,hl_x_max,hl_y_min,hl_y_max): # alpha value of heatmap highlights, image width and height, type of tissue: blood or colon


    data_type = ["n","gamma","d","r0"]
    c_labels =["$n_\mathrm{drift}$","$\gamma_\mathrm{drift}$","$d_\mathrm{drift}$","$r_0^\mathrm{drift}$"] 
    vminR = [0.99,1.99,0.99,1e-4]
    sb.set_style('white', {'axes.linewidth': 0.5})
    plt.rcParams['xtick.major.size'] = 20
    plt.rcParams['xtick.major.width'] = 4
    plt.rcParams['xtick.bottom'] = True
    plt.rcParams['ytick.left'] = True
    
    if tissue_type=="blood":
        points = p_b
        n_points = n_p_b
        Ns = Nsb
        mus = musb
        Nsl = ['{:.1e}'.format(i) for i in Nsb]
        Nsl = [reformatE(i) for i in Nsl]
        musl = ['{:.1e}'.format(j) for j in musb]
        musl = [reformatE(i) for i in musl]
    else:
        points = p_c
        n_points = n_p_c        
        Ns = Nsc
        mus = musc
        Nsl = ['{:.1e}'.format(i) for i in Nsc]
        Nsl = [reformatE(i) for i in Nsl]
        musl = ['{:.1e}'.format(j) for j in musc]
        musl = [reformatE(i) for i in musl]
        
    dataColl = [0]*8
    d_index = 0
    for d in range(8):

        if d_index==4:
            d_index=0

        if d<4:

            dataColl[d] = array([point[data_type[d_index]] for point in points]).reshape(RES,RES)
        else:

            dataColl[d] = array([point[data_type[d_index]] for point in n_points]).reshape(RES,RES)

        d_index+=1

    y, x = np.meshgrid(mus, Ns)
    fig, axes = plt.subplots(figsize=(im_w,im_h), nrows=2, ncols=4);

    m_index=0
    #fig.suptitle(tissue_type+" "+"scd (top) and neutral (bottom)") 

    for m, ax in zip(range(0,8), axes.flat):
        plt.figure(m);
        sb.set(font_scale=cb_scale);  # set colorbar font scale
        
        
        if m_index==4:
            m_index=0
        if m==3 or m==7:
            sb.heatmap(dataColl[m], cmap = ListedColormap(newcolors),norm=LogNorm(),cbar_kws={'label': c_labels[m_index]},vmin=vminR[m_index], vmax=amax(dataColl[m]),ax=ax)
        else:
            sb.heatmap(dataColl[m], cmap = ListedColormap(newcolors),cbar_kws={'label': c_labels[m_index]},vmin=vminR[m_index], vmax=amax(dataColl[m]),ax=ax)
        if m%4==0:
            
            ax.set_ylabel("$\mu$",fontsize=mu_l_s);
        else:
            ax.set_ylabel(" ",fontsize=mu_l_s);

        ax.set_xticklabels(Nsl,rotation=x_rot); #set xtics label (default is 0,1,2...)
        ax.set_yticklabels(musl,rotation=y_rot); #set ytics label (default is 0,1,2...)            
            

        plt.setp(ax.get_xticklabels()[1::2], visible=False);  # every 2nd tic is highlighted for xtics
        plt.setp(ax.get_yticklabels()[1::2], visible=False);  # every 2nd tic is highlighted for ytics  
        ax.set_xlabel("\n$N$",fontsize=N_l_s); # set xlabel   
        ax.tick_params(direction='out', length=16, width=6, colors='black',
               grid_color='black', grid_alpha=1.0,labelsize=tick_ls);     # tick settings
        
        
        # set the highlighted box and set alpha by user input (see description in a cell below)
        ax.add_patch(Rectangle((hl_x_min, hl_y_min), hl_x_max-hl_x_min, hl_y_max-hl_y_min, fill=False, edgecolor='black', linestyle = '--',  lw=5,alpha=1.0));
        for r in range(RES):
            for c in range(RES):
                if (r>=hl_x_min and r<hl_x_max) and (c<hl_y_max and c>=hl_y_min):
                    continue
                else:
                    ax.add_patch(Rectangle((r, c), 1, 1, fill=True, color = 'gray', edgecolor=None, lw=0, alpha=alphaval));

        m_index +=1
    plt.tight_layout();

以及 pcolormesh 版本的代码,其中 logscale 工作并添加补丁不...

import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.ticker as plticker
from matplotlib import cm
from matplotlib.colors import ListedColormap
from matplotlib.colors import LogNorm
from matplotlib.patches import Rectangle
import matplotlib.patches as patches
import matplotlib as mpl

data_type = ["n","gamma","d","r0"]
c_labels =["$n_\mathrm{drift}$","$\gamma_\mathrm{drift}$","$d_\mathrm{drift}$","$r_0^\mathrm{drift}$"] 
vminR = [1.0,1.99,1.0,1e-4]


hl_x_min,hl_x_max,hl_y_min,hl_y_max = 3,10,2,8

alphaval = 0.6

labels_size = 24 # X,Y,Z labels size

cb_ts = 15 # colorbar tick number font size

tick_ls = 15

title_s = 60 # size of title
im_w,im_h = 40,12  # image width and height
cb_scale = 4.8   # scale of colorbar font size
x_rot,y_rot = 90,0  # rotation of x and y tick labels

dataColl = [0]*8
d_index = 0
for d in range(8):

    if d_index==4:
        d_index=0

    if d<4:

        dataColl[d] = array([point[data_type[d_index]] for point in points]).reshape(RES,RES)
    else:

        dataColl[d] = array([point[data_type[d_index]] for point in n_points]).reshape(RES,RES)

    d_index+=1


fig, axes = plt.subplots(figsize=(im_w,im_h), nrows=2, ncols=4);

m_index=0
#fig.suptitle(tissue_type+" "+"scd (top) and neutral (bottom)") 

fig.text(0.483,0.86,"blood \n \n",fontsize = 35)
fig.text(0.488,0.82,"scd \n \n",fontsize = 35)
fig.text(0.48,0.38,"neutral \n \n",fontsize = 35)
plt.subplots_adjust(wspace=0.1, hspace=0.5) 

for m, ax in zip(range(0,8), axes.flat):
    
    y, x = np.meshgrid(mus, Ns)
    z = dataColl[m]
    z=z.T
    
    if m_index==4:
        m_index=0
    
    if m==3 or m==7:
        cmesh = ax.pcolormesh(x, y, z,norm=LogNorm(),cmap=ListedColormap(newcolors),vmin=vminR[m_index],vmax=amax(z),antialiased=True, shading="nearest" , snap=True,edgecolors="face") 
    else:
        cmesh = ax.pcolormesh(x, y, z,cmap=ListedColormap(newcolors),vmin=vminR[m_index],vmax=amax(z),antialiased=True, shading="nearest" , snap=True,edgecolors="face") 
    cb=fig.colorbar(cmesh,ax=ax)
    cb.set_label(label=c_labels[m_index],fontsize=labels_size,rotation = 90)

    ax.set_xscale('log')
    ax.set_yscale('log')
    ax.tick_params(direction='out', length=4, width=2, colors='black',
       grid_color='black', grid_alpha=1.0,labelsize=tick_ls);     # tick settings
        
    cb.ax.tick_params(labelsize=cb_ts)
    
    
    if m>3:
        ax.set_xlabel("$N$",fontsize=labels_size)
    if m_index ==0:
        ax.set_ylabel("$\mu$",fontsize=labels_size)
    
    # set the highlighted box and set alpha by user input (see description in a cell below)
    ax.add_patch(Rectangle((hl_x_min, hl_y_min), hl_x_max-hl_x_min, hl_y_max-hl_y_min, fill=False, edgecolor='black', linestyle = '--',  lw=5,alpha=1.0));
    for r in range(RES):
        for c in range(RES):
            if (r>=hl_x_min and r<hl_x_max) and (c<hl_y_max and c>=hl_y_min):
                continue
            else:
                ax.add_patch(Rectangle((r, c), 1, 1, fill=True, color = 'gray', edgecolor=None, lw=0, alpha=alphaval));
    m_index +=1


#plt.tight_layout();

plt.show()

fig.savefig("pcmesh.pdf")

希望这是可以理解的,我试图搜索这个问题,我已经阅读了大量的东西,但我找不到任何东西......

先感谢您! 这是海底的情节

这是 pcolormesh 版本,对数尺度抽动很重要

4

1 回答 1

0

好的,经过更多小时的搜索,我找到了解决方案。我的错误是我用 pcolormesh 处理了 add_patch(),因为它会使用索引坐标而不是实际的 X、Y 网格数据,并且我必须将 zorder >0 添加到 Rectangle() 函数中。

在这里引用这个问题:在图形的顶层绘制圆圈

ax.add_patch(Rectangle((hl_x_min, hl_y_min), hl_x_max-hl_x_min, hl_y_max-hl_y_min, fill=False, edgecolor='black', linestyle = '--',  lw=5,alpha=1.0,zorder = 10));

(hl_x_min, hl_y_min), hl_x_max-hl_x_min, hl_y_max-hl_y_min坐标必须不是索引 (0,1,2...) 而是实际数据间隔(如 (1e-4;1e-2) )。

于 2021-02-04T16:59:21.570 回答