DingWB / PyComplexHeatmap

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

Annotation label & legends with spaces in the title #101

Open mwe-bixeng opened 4 days ago

mwe-bixeng commented 4 days ago

Is there a way to set each row of the annotation with text that has spaces? In my example, I would like the legend title to say "Enrol Group" with a space between the words. And similarly for the label of the row. Or is there a way for title in legend_kws to overwrite the variable value?

I've tried using legend_kws={'color_text':False, 'title': "Enrol Group"} in anno_simple but because it's taking the variable name as the title and label, I get this error:

TypeError: matplotlib.axes._axes.Axes.legend() got multiple values for keyword argument 'title'

# variable = EnrolGroup which is the title in the legend and label of that row col_ha = HeatmapAnnotation(EnrolGroup=anno_simple(df["Enrolment Group"],add_text=True,legend=True , legend_kws={'color_text':False, 'title': "Enrol Group"} plot=True,legend=True,legend_gap=3 , legend_order=True , label_side='left' )

Screenshot 2024-11-24 at 8 40 39 AM
DingWB commented 4 days ago

Thanks for your feedback, it has already been fixed. You can pass title to legend_kws now (you need to re-install from Github). In addition, you can also pass a dict to HeatmapAnnotation so that the key can contain space, please see example here: https://dingwb.github.io/PyComplexHeatmap/build/html/notebooks/advanced_usage.html#Add-multiple-heatmap-annotations-using-for-loop

mwe-bixeng commented 3 days ago

Thanks so much for such a quick response! I can confirm that using title in legend_kws works but I think that may have broken the label argument. I added both title and label and get this error: TypeError: Legend.__init__() got an unexpected keyword argument 'label'

Same as above except I added label col_ha = HeatmapAnnotation(EnrolGroup=anno_simple(df["Enrolment Group"],add_text=True,legend=True , legend_kws={'color_text':False, 'title': "Enrol Group", 'label':'Enrol Group label'} plot=True,legend=True,legend_gap=3 , legend_order=True , label_side='left' )

DingWB commented 3 days ago

Why did you add the parameter label? There is no such kind of argument label for legend_kws: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.legend.html

mwe-bixeng commented 3 days ago

I'm following the example link you gave in the previous message (advanced usage) M1=anno_simple(df_cols['M1'],cmap='jet',legend=lgd,vmax=1,vmin=0,legend_kws={'label':'M1'}), to change the side label text. the legend title has changed but not the side label. did I misunderstand your instruction?

mwe-bixeng commented 3 days ago

Oh sorry, I misread which dict you meant. I see what I can do with using a dict into the Heatmap. thanks. But I think the above will still be confusing for others if there is no label directive but it's in the adv usage instructions. cheers

DingWB commented 2 days ago

I also added the parameter label to all annotation functions. You can re-install the latest version from Github and try this parameter label.

mwe-bixeng commented 2 days ago

Excellent! Now I can have 2 different texts for the label and legend. Thank you so much again for such a quick response and fix. Here's an example of where these changes have come in very handy. I used the dict method to change the label text.

col_ha_dict = {"Cancer predisposition": anno_simple(df["Cancer predisposition"] , colors={'Yes': 'purple'} , legend_kws={'color_text':False, 'title': "Identification of a \npreviously unknown \ncancer predisposition"} , add_text=False,legend=True)}

col_ha = HeatmapAnnotation(**col_ha_dict , axis=1 , plot=True,legend=True,legend_gap=3 , label_side='left' ) Screenshot 2024-11-25 at 12 46 50 PM

DingWB commented 2 days ago

You don't have to pass a dict to HeatmapAnnotation, you can set parameter label with the latest version.