Tickval 没有为 ticktext 分配正确的位置

Tickval does not assign correct position to ticktext

我正在使用下面的代码绘制带有颜色条的带注释的热图,部分灵感来自 this 非常有用的教程(它是我将放在最后的较大代码的一部分) .

final_df 是一个数据框,其 columns 是蛋白质 ID,index 是感兴趣的特征(本例中只有一个 - M100867)。热图单元格中的非 0 整数意味着与该单元格相关的蛋白质是与 row/index 相关的特征的一部分,并且每个非 0 整数(1 到 6 之间)映射到一个蛋白质分组,我'我想用它来为我的细胞着色。

#print (final_df) 
#         C8E97_RS35225  C8E97_RS12075  ...  C8E97_RS12225  C8E97_RS12230
#M100867              0              0  ...              0              0
#
#[1 rows x 31 columns]

我正在用下面的代码块绘制热图:

fig_df = ff.create_annotated_heatmap(final_df.values.tolist(), 
                                         x= list(final_df.columns), 
                                         y=list(final_df.index), 
                                         annotation_text  = cell_labels, #a 1x31 nested list of empty strings to remove cell annotations
                                         colorscale=dcolorsc,
                                         colorbar = dict(thickness=25, 
                                                         tickvals=tickvals, 
                                                         ticktext=ticktext,
                                                         tickmode = 'array'),
                                         showscale  = True,
                                         xgap = 10,
                                         ygap = 10)

不幸的是,我的 colorbar ticktexttickvals 不匹配,我不知道为什么 - 它应该是一个标签('biosynthetic',等等)图像右侧颜色栏中的每个颜色块:

有什么指点吗?

干杯!

蒂姆

完整代码:

def discrete_colorscale(bvals, colors):
        #https://chart-studio.plotly.com/~empet/15229/heatmap-with-a-discrete-colorscale/#/
        """
        bvals - list of values bounding intervals/ranges of interest
        colors - list of rgb or hex colorcodes for values in [bvals[k], bvals[k+1]],0<=k < len(bvals)-1
        returns the plotly  discrete colorscale
        """
        if len(bvals) != len(colors)+1:
            raise ValueError('len(boundary values) should be equal to  len(colors)+1')
        bvals = sorted(bvals)     
        nvals = [(v-bvals[0])/(bvals[-1]-bvals[0]) for v in bvals]  #normalized values
        
    dcolorscale = [] #discrete colorscale
    for k in range(len(colors)):
        dcolorscale.extend([[nvals[k], colors[k]], [nvals[k+1], colors[k]]])
    return dcolorscale


bvals = [0,1,2,3,4,5,6,7]

colors_map = ['rgb(255,255,255)', #white
              'rgb(255,0,0)', #red
              'rgb(255, 128, 0)', #orange
              'rgb(0, 0, 255)', #blue
              'rgb(128, 128, 128)', #grey
              'rgb(0, 255, 0)', #green
              'rgb(192, 192, 192)'] #light grey

dcolorsc = discrete_colorscale(bvals, colors_map)
#[[0.0, 'rgb(255,255,255)'], 
# [0.14285714285714285, 'rgb(255,255,255)'], 
# [0.14285714285714285, 'rgb(255,0,0)'], 
# [0.2857142857142857, 'rgb(255,0,0)'], 
# [0.2857142857142857, 'rgb(255, 128, 0)'], 
# [0.42857142857142855, 'rgb(255, 128, 0)'], 
# [0.42857142857142855, 'rgb(0, 0, 255)'], 
# [0.5714285714285714, 'rgb(0, 0, 255)'], 
# [0.5714285714285714, 'rgb(128, 128, 128)'], 
# [0.7142857142857143, 'rgb(128, 128, 128)'], 
# [0.7142857142857143, 'rgb(0, 255, 0)'], 
# [0.8571428571428571, 'rgb(0, 255, 0)'], 
# [0.8571428571428571, 'rgb(192, 192, 192)'], 
# [1.0, 'rgb(192, 192, 192)']]

bvals = np.array(bvals)
tickvals = [np.mean(bvals[k:k+2]) for k in range(len(bvals)-1)]
ticktext  = ['not in module', 
             'biosynthetic',
             'biosynthetic-additional',
             'other',
             'regulatory',
             'resistance',
             'transport']  

fig_df = ff.create_annotated_heatmap(final_df.values.tolist(), 
                                     x= list(final_df.columns), 
                                     y=list(final_df.index), 
                                     annotation_text  = cell_labels, #a 1x31 nested list of empty strings to remove cell annotations
                                     colorscale=dcolorsc,
                                     colorbar = dict(thickness=25, 
                                                     tickvals=tickvals, 
                                                     ticktext=ticktext,
                                                     tickmode = 'array'),
                                     showscale  = True,
                                     ygap = 10,
                                     xgap = 10)
fig_df.update_layout(
xaxis=dict(
    
    rangeslider=dict(
        visible=True
    )
)
)
fig_df.write_html(results_file_path)

已修复 - 需要指定的 zmin 和 zmax(max/min bvals 数组中的 val)以正确比例颜色条:

fig_df = ff.create_annotated_heatmap(final_df.values.tolist(), 
                                     x= list(final_df.columns), 
                                     y=list(final_df.index), 
                                     annotation_text  = cell_labels, 
                                     colorscale=dcolorsc,
                                     colorbar = dict(thickness=25, 
                                                     tickvals=tickvals, 
                                                     ticktext=ticktext),
                                     showscale  = True,
                                     zmin=0, ###
                                     zmax=7, ###
                                     ygap = 10,
                                     xgap = 10)