我想使用自定义调色板 (
color_list
) 来根据 train_clusters["Cluster_Label"]
中的唯一值绘制相关热图。
注意:train_clusters["Sample"] 对应于
train_combined_df.index
。绘图数据位于train_combined_df
。
问题:我在此示例中只有两种颜色
color_list
(即,train_clusters["Cluster_Label"]
中的两个唯一值各有一种颜色),但输出却有四种颜色。
代码:
import plotly.figure_factory as ff
import plotly.graph_objects as go
from scipy.spatial.distance import pdist, squareform
color_list = ['#003f5c', '#ffa600']
# Initialize figure by creating upper dendrogram
fig = ff.create_dendrogram(train_combined_df.values, orientation='bottom')
fig.for_each_trace(lambda trace: trace.update(visible=False))
for i in range(len(fig['data'])):
fig['data'][i]['yaxis'] = 'y2'
# Create Side Dendrogram
dendro_side = ff.create_dendrogram(train_combined_df.to_numpy(), orientation='right', colorscale=color_list)
for i in range(len(dendro_side['data'])):
dendro_side['data'][i]['xaxis'] = 'x2'
# Add Side Dendrogram Data to Figure
for data in dendro_side['data']:
fig.add_trace(data)
# Create Heatmap
dendro_leaves = dendro_side['layout']['yaxis']['ticktext']
dendro_leaves = list(map(int, dendro_leaves))
data_dist = pdist(train_combined_df.values)
heat_data = squareform(data_dist)
heat_data = heat_data[dendro_leaves,:]
heat_data = heat_data[:,dendro_leaves]
# Change to upper triangular form
for i in range(len(heat_data)):
for j in range(len(heat_data)):
if i > j:
heat_data[i][j] = None
heatmap = [
go.Heatmap(
x = dendro_leaves,
y = dendro_leaves,
z = heat_data,
colorscale = [
[0.0, '#f1f1f1'],
[0.1, '#ffa600'],
[0.2, '#ff7c43'],
[0.3, '#f95d6a'],
[0.4, '#d45087'],
[0.5, '#a05195'],
[0.6, '#665191'],
[0.7, '#2f4b7c'],
[0.8, '#003f5c'],
[1.0, '#0e0e0e']
]
)
]
heatmap[0]['x'] = fig['layout']['xaxis']['tickvals']
heatmap[0]['y'] = dendro_side['layout']['yaxis']['tickvals']
# Add Heatmap Data to Figure
for data in heatmap:
fig.add_trace(data)
# Edit Layout
fig.update_layout({'width':800, 'height':800,
'showlegend':False, 'hovermode': 'closest',
})
# Edit xaxis
fig.update_layout(xaxis={'domain': [.15, 1],
'mirror': False,
'showgrid': False,
'showline': False,
'zeroline': False,
'ticks':""})
# Edit xaxis2
fig.update_layout(xaxis2={'domain': [0, .15],
'mirror': False,
'showgrid': False,
'showline': False,
'zeroline': False,
'showticklabels': False,
'ticks':""})
# Edit yaxis
fig.update_layout(yaxis={'domain': [0, 1],
'mirror': False,
'showgrid': False,
'showline': False,
'zeroline': False,
'showticklabels': False,
'ticks': ""
})
# # Edit yaxis2
fig.update_layout(yaxis2={'domain':[.825, .975],
'mirror': False,
'showgrid': False,
'showline': False,
'zeroline': False,
'showticklabels': False,
'ticks':""})
fig.update_layout(paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
xaxis_tickfont = dict(color = 'rgba(0,0,0,0)'))
# Modify dendrogram colors based on cluster labels
for i, label in enumerate(train_clusters['Cluster_Label']):
sample_index = train_clusters.loc[i, 'Sample']
for j, dendro_data in enumerate(fig['data']):
if dendro_data['x'][0] == sample_index:
fig['data'][j]['line']['color'] = color_list[label]
break
fig.show()
# Save plot as PDF
fig.write_image("VAENMFspectral_consensus_heatmap_KIRP.pdf")
输入:
train_clusters
pd.DataFrame({'Sample': {48: 'TCGA.A3.3367.01',
51: 'TCGA.A3.3387.01',
18: 'TCGA.B0.4710.01',
0: 'TCGA.B0.4818.01',
23: 'TCGA.B0.4821.01'},
'Cluster_Label': {48: 1, 51: 1, 18: 1, 0: 2, 23: 2}})
train_combined_df
{'AASDH': {'TCGA.A3.3367.01': 0.5320696873033979,
'TCGA.A3.3387.01': 0.4562592849092732,
'TCGA.B0.4710.01': 0.4931201843239812,
'TCGA.B0.4818.01': 0.4741494239287809,
'TCGA.B0.4821.01': 0.4037527663931228},
'AATK': {'TCGA.A3.3367.01': 0.409362763143499,
'TCGA.A3.3387.01': 0.33823090552783364,
'TCGA.B0.4710.01': 0.4781561801809045,
'TCGA.B0.4818.01': 0.42083527521271474,
'TCGA.B0.4821.01': 0.3520165708740812},
'ABCD3': {'TCGA.A3.3367.01': 0.6804749459297832,
'TCGA.A3.3387.01': 0.6502961267286307,
'TCGA.B0.4710.01': 0.629857235378111,
'TCGA.B0.4818.01': 0.6768727064918065,
'TCGA.B0.4821.01': 0.5811571216803231},
'ABHD10': {'TCGA.A3.3367.01': 0.6096662921739531,
'TCGA.A3.3387.01': 0.6022759681938674,
'TCGA.B0.4710.01': 0.6348323089026715,
'TCGA.B0.4818.01': 0.6413746053093422,
'TCGA.B0.4821.01': 0.5779754640688728},
'ABHD14B': {'TCGA.A3.3367.01': 0.665834495540351,
'TCGA.A3.3387.01': 0.6129179752341758,
'TCGA.B0.4710.01': 0.6366922055392048,
'TCGA.B0.4818.01': 0.6739641283003278,
'TCGA.B0.4821.01': 0.6648293093129545}}
颜色的数量取决于树状图识别的簇的数量,您必须将
color_threshold
arg 设置为在树状图中仅显示 2 个簇,然后它将仅具有您在 color_list
中指定的颜色。