DingWB / PyComplexHeatmap

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

Text Labels are not displaying properly #94

Open SaniyaKhullar opened 4 weeks ago

SaniyaKhullar commented 4 weeks ago

Hi there, I have tried running the example from https://github.com/DingWB/PyComplexHeatmap/blob/main/notebooks/kwargs.ipynb and I was unable to generate the same plot for the given data. I was seeing how to display the text labels, but it did not work.

Instead, I did not get the labels for all of the cells. This is what I got: example_pch

when I ran

label_kws in HeatmapAnnotation control the heatmap annotaiton labels

col_ha = pch.HeatmapAnnotation( Groups=pch.anno_simple(df.Groups,add_text=True,text_kws={'color':'black'}), AB=pch.anno_simple(df.AB,add_text=True),axis=1, Exp=pch.anno_boxplot(df_box, cmap='turbo'), verbose=0,label_side='right',

            label_kws=dict(visible=True,color='red',rotation=45,fontsize=7,fontstyle='italic',
                          alpha=0.9,verticalalignment='center',horizontalalignment='left'),
            #for more parameters of label_kws, see ax.yaxis.label.properties()

            ticklabels_kws=dict(which='major',color='red',labelcolor='blue',left=True,direction='in',
                               width=2,pad=2,labelsize=8) 
            #for more parameters of ticklabels_kws,see matplotlib.axes.Axes.tick_params,

)

xticklabels_kws and yticklabels_kws control the ticklabels for the heatmap.

plt.figure(figsize=(6, 8)) cm = pch.ClusterMapPlotter(data=df_heatmap, top_annotation=col_ha, col_split=df.Groups, col_split_order="cluster_between_groups", col_split_gap=0.5,col_cluster=True, row_cluster=True,col_dendrogram=True, label='values',show_rownames=True,show_colnames=True, tree_kws={'colors':'blue'},verbose=0,legend_gap=7, annot=True,fmt='.1g',linewidths=0.05,linecolor='gold',cmap='RdYlBu_r',

                   xticklabels_kws=dict(labelrotation=-45,labelcolor='purple',labelsize=14),
                   #more parameters for [x/y]_ticklabels_kws, see: matplotlib.axes.Axes.tick_params or ?ax.tick_params
                  )

plt.show()

So you can see some incomplete text in cells. I even tried out

df_heatmap = df_heatmap.astype(float)

image

I have been struggling to create a proper pch heatmap with the values displayed on top. Please tell me how to do this and if this is indeed a bug that can be fixed.

For instance, for my work, this is what I'm uncovering:

image

when I run this:

plt.figure(figsize=(10, 12))

cm = pch.ClusterMapPlotter(data=conf_matrix, cmap='Reds',annot=True,fmt='.0f', linewidths=0.5,linecolor='black', col_cluster = False, row_cluster=False,col_dendrogram=False, row_dendrogram = False, show_rownames=True,show_colnames=True) cm plt.show()

Thanks!

DingWB commented 4 weeks ago

Which version are you using? Can you try the latest version? Or just install from the github: pip uninstall -y PyComplexHeatmap && pip install git+https://github.com/DingWB/PyComplexHeatmap

SaniyaKhullar commented 4 weeks ago

I think that could help. I also wanted to ask if you know how to make the diagonal elements of the symmetric matrix have a red border.

Using seaborn, it is:

    # Highlight the diagonal in red
    for i in range(conf_matrix.shape[0]):
        ax.add_patch(plt.Rectangle((i, i), 1, 1, fill=False, edgecolor='red', lw=2))

And how to make the legend below the plot.

I was looking for any documentation for how to do this using PCH.

DingWB commented 4 weeks ago

Please check the documentation here (https://dingwb.github.io/PyComplexHeatmap/build/html/notebooks/cpg_modules.html#Plotting-the-Dot-clustermap) about how to plot the red border.

SaniyaKhullar commented 4 weeks ago

Hi there, I checked that out. That is for different splits of the heatmap (i.e. spines). I was wondering for cases where we have just 1 heatmap. So something like this:

image

I implemented that using seaborn but was looking to your package for help with annotations.

So I saw this in your code:

plot custom spines

for i in range(cm.heatmap_axes.shape[0]): for j in range(cm.heatmap_axes.shape[1]): if i != j: continue ax = cm.heatmap_axes[i][j] for side in ["top", "right", "left", "bottom"]: ax.spines[side].set_visible(True) ax.spines[side].set_color('red') ax.spines[side].set_linewidth(2)

But am not sure if it is applicable to 1 heatmap.

Also, it is not working when I'm using the new pip install approach. I'm getting this:

image

image

Further, how may I mention the legend to be horizontal.

DingWB commented 2 weeks ago

I have no idea why this happened on your side. Everything is good on my side.

jsharrison94 commented 1 week ago

I'm seeing the same issue with version 1.8.0, only the first row of annotations are showing up when using annot=True

image

DingWB commented 1 week ago

Please also report the version of Python and matplotlib.

jsharrison94 commented 1 week ago

I'm using matplotlib v3.9.2 and python v3.10.12

I tried upgrading to v1.9.0 of PyComplexHeatmap but it says this distribution cannot be found. Has this version been released yet?

SaniyaKhullar commented 1 week ago

I found a workaround, but I had to write my code based on PyComplexHeatmap and make some adjustments. I will see how to share that code.

DingWB commented 1 week ago

Hi @jsharrison94 ,

Thanks for reporting the versions. I can reproduce the issue now. This is related to the matplotlib version. If you downgrade to a lower version of matplotlib, there will be no problem.

I don't know why matpltolib 3.9.2 has such kind of issue, but I will look into it.

DingWB commented 1 week ago

I just tested different versions of matplotlib, it works for matpltolib <= 3.7.5. I would recommend you install matplotlib 3.7.5.

DingWB commented 1 week ago

I think I have already fixed this. Can you re-install it from GitHub and test whether it works for you? @jsharrison94 @SaniyaKhullar pip uninstall -y PyComplexHeatmap && pip install git+https://github.com/DingWB/PyComplexHeatmap

jsharrison94 commented 6 days ago

I've installed the new version and can confirm this has fixed the issue on my end. Thank you very much for the fast response!