DingWB / PyComplexHeatmap

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

Legends not saved properly when using plt.savefig() #73

Closed GordianArnav closed 2 months ago

GordianArnav commented 2 months ago

Describe the bug Plt.show() displays the correct legends, however when I save the plot, legends are not saved

Screenshots image image

DingWB commented 2 months ago

Please upload your data and code so I can reproduce and fix this bug @GordianArnav .

I guess there may be something wrong with your code cause such a bug has never been reported.

GordianArnav commented 2 months ago

I can only provide the code, plt.show() produces the plot perfectly, however only when saving issue arrises

col_ha = HeatmapAnnotation(Int=anno_simple(col_df['Int_Type'],cmap='Set1',
                                           add_text=False,legend=True, text_kws={'fontsize':label_size}),
                                           nCells=anno_barplot(col_df['nCells'],colors = 'gray',legend=False, height = 20),
                                           label_side='left',legend_side = 'right',legend_width=40,axis=1,label_kws= 
                                           {'fontsize':label_size},ticklabels_kws={'labelsize':label_size})

row_ha = HeatmapAnnotation(
            n_genes=anno_simple(row_df['nGenes'], cmap='RdPu',
                                               height=10, add_text=True, text_kws={'rotation':0, 'fontsize':12,'color':'black'},
                                                     majority=False, linecolor='black',linewidths=0.005),
           mean_pct_total_counts=anno_simple(row_df['mean_pct_total_counts'],cmap='RdPu', 
                                                               height=10,linecolor='black',linewidths=0.005),
            axis=0,legend_hpad=20,legend_width=10,label_side='bottom',wgap=3,label_kws={'fontsize':label_size})

plt.figure(figsize=fig_size)
cm = ClusterMapPlotter(data=cross_tab,
                        col_cluster=True, col_cluster_metric='correlation',
                        row_cluster=True,row_cluster_metric='correlation',
                        left_annotation=row_ha, top_annotation=col_ha,
                        label=heatmap_metric,row_dendrogram=True,
                         col_dendrogram=True,show_rownames=True,
                        show_colnames=True,verbose=0,legend=True,legend_anchor='ax_heatmap',legend_width=50,
                        cmap='bwr',xticklabels_kws={'labelrotation':-90,'labelsize':label_size},
                        yticklabels_kws={'labelsize':label_size},annot = cross_tab_annot, 
                        row_names_side='right',row_dendrogram_size=10,linecolor='black',linewidths=0.005,
                        fmt=f'',annot_kws = {'weight':'bold'})`

cm.ax_heatmap.set_title(f"Method - {method}", fontsize = title_size ,y=1.08)
plt.yticks(weight='bold')`
if save == True:
    plt.savefig(f"heatmap.pdf", dpi=600, format='pdf', bbox_inches='tight')
DingWB commented 2 months ago

If you ran any example from the documentation website and saved the figure, would you get the same issue?

GordianArnav commented 2 months ago

Nope the examples work fine

DingWB commented 2 months ago

Then, there may be something wrong with your data or the code. You have to debug yourself cause I can not see your data. Here are some suggestions: (1). Only the heatmap, without any annotations, save it into pdf, and see whether it works. (2). Add annotations one by one. (3). Remove all optional parameters and add them one by one.

GordianArnav commented 2 months ago

Thanks, even the heatmap without any annotations, the colorbar is not getting saved

DingWB commented 2 months ago

Please run:

plt.figure()
cm = ClusterMapPlotter(data=cross_tab,
                        cmap='bwr',annot = True)
plt.savefig(f"heatmap.pdf", bbox_inches='tight')

and let's see what happens. If the figure is OK, then add parameter one by one.

GordianArnav commented 2 months ago

Okay that works

  1. Added col, row dendograms and clusters back - That Works
  2. Added left_annotation=row_ha, top_annotation=col_ha, - That Works
  3. Added xticklabels_kws={'labelrotation':-90,'labelsize':label_size} - Works
  4. Added yticklabels_kws={'labelsize':label_size} - Fails

Update: Added everything back and it is related to yticklabel size

DingWB commented 2 months ago

OK. Congrats. You know what to do next.

DingWB commented 2 months ago

Is this the issue on your side, or is there any bug with the package?

GordianArnav commented 2 months ago

I mean there is a limit to the yticklabel size you can put depending upon the figure size, I was trying to come up with an algorithm that

1) Automatically finds best figure size based on length of xticks and yticks such that all ticks are displayed -> (100,45) is my dataset 2) Increase label size so that everything is visible and proportionate

So maybe issue on my side, however colors should be saved regardless of yticklabel size right?