【发布时间】:2022-01-19 08:20:06
【问题描述】:
我正在使用下面的代码绘制带有颜色条的带注释的热图,部分灵感来自 this 非常有用的教程(它是我将放在最后的较大代码的一部分)。
final_df 是一个数据框,其columns 是蛋白质 ID,index 是感兴趣的特征(本例中只有一个 - M100867)。热图单元格中的非 0 int 意味着与该单元格相关的蛋白质是与行/索引相关的特征的一部分,并且每个非 0 int(介于 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 ticktexts 与 tickvals 不匹配,我不知道为什么 - 它应该是右侧颜色栏中每个彩色块的一个标签(“生物合成”等)图片:
任何指针?
干杯!
提姆
完整代码:
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)
【问题讨论】:
标签: python formatting plotly heatmap colorbar