DingWB / PyComplexHeatmap

PyComplexHeatmap: A Python package to plot complex heatmap (clustermap)
https://dingwb.github.io/PyComplexHeatmap/
MIT License
295 stars 32 forks source link

Question about the legend visualization. #5

Closed DBinary closed 1 year ago

DBinary commented 1 year ago

Dear wubin:

Thank you so much for creating this wonderful package , I have a question about the legend visualization . In my datasets , the legend was not fully visualized , see the code below , and the datasets were uploading here .

# 横注释
col_ha = HeatmapAnnotation(#label=anno_label(obs.loc[:,'T/NK_type'], colors=type_color_TNK, merge=True,rotation=15), # 设置是否有对T/NK_type的文字注释
               T_NK_type=anno_simple(obs.loc[:,'T/NK_type'], colors=type_color_TNK, add_text=False, legend=True, height=8),axis=1, # 绘制T/NK_type注释
               T_NK_celltype=anno_simple(obs.loc[:,'T/NK_celltype'], colors=type_color_rna, add_text=False, legend=True, height=8), # 绘制T/NK_celltype注释
               plot=False,legend=False,legend_gap=3,legend_hpad=1,legend_width=3
                           )
# 纵注释
row_ha = HeatmapAnnotation(#label=anno_label(obs.loc[:,'T/NK_type'], colors=type_color_TNK, merge=True,rotation=15), # 设置是否有对T/NK_type的文字注释
               Gene=anno_simple(var.loc[:,'celltype'], colors=type_color_rna, add_text=False, legend=True, height=8),axis=0, # 绘制T/NK_type注释
               plot=False,legend=False,legend_gap=3,legend_hpad=1,legend_width=3
                           )
plt.figure(figsize=(8, 16))
# 绘制注释
# 绘制基因表达图
cm = ClusterMapPlotter(data=Gene_Expression.T, #z_score = True,
                       top_annotation = col_ha, # 设置上注释
                       left_annotation = row_ha, # 设置左注释
                       row_cluster = False, # 设置禁止自动聚类!!!
                       col_cluster = False, # 设置禁止自动聚类!!! 这个逼函数聚类参数默认是True
                       row_names_side = 'right',  # 基因名放右边
                       col_split = obs.loc[:,'T/NK_type'] , # 设置纵轴分割
                       row_split = var.loc[:,'celltype'] , # 设置横轴分割
                       label='values',  # 热图颜色设置
                       row_dendrogram = False,
                       show_rownames = True,
                       show_colnames = False,
                       cmap ='PuBu' , # Values的色卡
                       legend = False, # 不画图例
                      # legend_hpad = 0.5,#热图和图例之间间隙
                      # legend_vpad = 0.5,
                      # legend_side = 'left', # 图例放左边
                       row_split_gap = 2, # 横向分割间距
                       col_split_gap = 0.4, # 纵向分割间距
                    # tree_kws={'row_cmap': 'Dark2'}
                    )

# [Gene_Expression.csv](https://github.com/DingWB/PyComplexHeatmap/files/10553588/Gene_Expression.csv)
plt.savefig("Figure2_marker.png",dpi=300,bbox_inches = 'tight')
plt.savefig("/content/drive/MyDrive/Gastric/Figure/Figure2/Figure2_marker.png",dpi=300,bbox_inches = 'tight')
plt.show()

clusterheatmap

datasets: var.csv obs.csv Gene_Expression.csv

DingWB commented 1 year ago

You can use legend_width to control the width of the legend. For the order of cols and rows, it's already fixed. Please try it again, and you can also see this example: https://github.com/DingWB/PyComplexHeatmap/blob/main/notebooks/advanced_usage.ipynb