したがって、私の問題は、いくつかのヒートマップをマルチプロットとしてプロットし、X、Y 軸の対数を持ち、ヒートマップにパッチを追加して、アルファ値を調整して特定の領域を強調表示することです (添付の画像を参照)。問題は、pcolormesh を使用すると、正確なデータを使用して表示するため、X、Y に logscale を簡単に使用できますが、seaborn はデータのインデックスを使用できることです。一方、pcolormesh では pacthes を追加する方法が見つかりませんでしたが、seaborn では簡単に実行できます...
したがって、対数スケーリングとプロットへのパッチの追加の両方を実行できるソリューションを考え出したいと思います...
コードの一部を追加し、seaborn でまだ何ができるかの画像を添付します。
したがって、jupyter-notebook で行われた sime カスタム x,ytics を使用した seaborn ヒートマップ マルチプロットのコードは次のとおりです。
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バージョンのコード...
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")
理解できることを願っています。この問題を検索しようとしました。たくさんのものを読みましたが、何も見つかりませんでした...