这是根据plotly官网的一个例子进行相关修改:
输入输出说明
画图数据
DesignElementAccession r14 h45 c11 r11 c07 ...
1053_at 6.373977 6.498170 6.271771 6.092041 6.095927 ...
117_at 5.986182 4.861167 5.566699 5.590227 5.442939 ...
121_at 7.468118 6.947996 6.943584 7.536698 6.891398 ...
1255_g_at 4.495150 4.485995 3.671166 3.934822 3.731431 ...
1294_at 6.474134 5.935137 6.053127 6.626716 6.047488 ...
样品表达量聚类热图
横坐标是样品,纵坐标是表达量
newplot.png
代码
import pandas as pd
import plotly.graph_objects as go
import plotly.figure_factory as ff
df = pd.read_table('ExpRawDataETABM84AAFFY44.tab')
df = df.set_index('DesignElementAccession')
df = df.head(10) ## 表达量
dft = df.T ## 样品
#fig = ff.create_dendrogram(df, orientation='right', labels=df._stat_axis.values.tolist())
# Initialize figure by creating upper dendrogram
fig = ff.create_dendrogram(dft, orientation='bottom', labels=dft._stat_axis.values.tolist())
fig.update_layout(plot_bgcolor = '#FFFFFF')
for i in range(len(fig['data'])):
fig['data'][i]['yaxis'] = 'y2'
# Create Side Dendrogram, 设置右边树图
dendro_side = ff.create_dendrogram(df, orientation='left', labels=df._stat_axis.values.tolist())
#dendro_side.update_layout(yaxis=dict(side = "left"))
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)
fig['layout']['yaxis'] = dendro_side['layout']['yaxis']
heatmap = [
go.Heatmap(
z= df,
x= df.columns.values, ## 列名
y= df._stat_axis.values.tolist(), ## 行名
hoverongaps = False,
colorbar=dict(x=-0.3, xanchor='left') ## 控制colorbar的位置
)
]
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':600,
'showlegend':False, 'hovermode': 'closest',})
# Edit xaxis, 设置热图的 x 轴
fig.update_layout(xaxis={'domain': [0, 0.87],
'mirror': False,
'showgrid': False,
'showline': False,
'zeroline': False,
'ticks':""})
# Edit xaxis2, 控制左边树的x 轴
fig.update_layout(xaxis2={'domain': [0.87, .975],
'mirror': False,
'showgrid': False,
'showline': False,
'zeroline': False,
'showticklabels': False,
'ticks':""})
# Edit yaxis
fig.update_layout(yaxis={'domain': [0, .87],
'mirror': False,
'showgrid': False,
'showline': False,
'zeroline': False,
'showticklabels': True,
'side': "left",
'ticks': ""})
# Edit yaxis2, 控制上面树图的 Y轴
fig.update_layout(yaxis2={'domain':[.87, .975],
'mirror': False,
'showgrid': False,
'showline': False,
'zeroline': False,
'showticklabels': False,
'ticks':""})
fig.show()
网友评论