Skip to content

Commit 45813ca

Browse files
committed
update alignment of legend title
1 parent 169372f commit 45813ca

1 file changed

Lines changed: 25 additions & 32 deletions

File tree

PyComplexHeatmap/utils.py

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ def adjust_cmap(cmap,vmin,vmax,center=None,na_col='white'):
427427
return cmap
428428
# =============================================================================
429429
def plot_color_dict_legend(
430-
D=None, ax=None, title=None, color_text=True, label_side="right", kws=None
430+
D=None, ax=None, title=None, color_text=True, kws=None
431431
):
432432
"""
433433
plot legned for color dict
@@ -438,7 +438,6 @@ def plot_color_dict_legend(
438438
ax: axes to plot the legend.
439439
title: title of legend.
440440
color_text: whether to change the color of text based on the color in D.
441-
label_side: right of left.
442441
kws: kws passed to plt.legend.
443442
444443
Returns
@@ -455,7 +454,7 @@ def plot_color_dict_legend(
455454
lgd_kws["bbox_transform"] = ax.figure.transFigure
456455
lgd_kws.setdefault("borderpad", 0.1 * mm2inch * 72) # 0.1mm
457456
lgd_kws.setdefault("markerscale", 1)
458-
lgd_kws.setdefault("handleheight", 1) # font size, units is points
457+
lgd_kws.setdefault("handleheight", 0.7) # font size, units is points
459458
lgd_kws.setdefault("handlelength", 1) # font size, units is points
460459
lgd_kws.setdefault(
461460
"borderaxespad", 0.1
@@ -466,15 +465,16 @@ def plot_color_dict_legend(
466465
lgd_kws.setdefault(
467466
"labelspacing", 0.1
468467
) # gap height between two Patches, 0.05*mm2inch*72
469-
lgd_kws.setdefault("columnspacing", 1)
468+
lgd_kws.setdefault("columnspacing", 0.4)
470469
lgd_kws.setdefault("bbox_to_anchor", (0, 1))
471470
lgd_kws.setdefault("title", title)
472-
if label_side == "left":
473-
lgd_kws.setdefault("markerfirst", False)
474-
align = "right"
475-
else:
476-
lgd_kws.setdefault("markerfirst", True)
477-
align = "left"
471+
lgd_kws.setdefault("markerfirst", True)
472+
# if label_side == "left":
473+
# lgd_kws.setdefault("markerfirst", False)
474+
# align = "right"
475+
# else:
476+
# lgd_kws.setdefault("markerfirst", True)
477+
# align = "left"
478478

479479
availabel_height = (
480480
ax.figure.get_window_extent().height * lgd_kws["bbox_to_anchor"][1]
@@ -494,7 +494,9 @@ def plot_color_dict_legend(
494494
return None
495495
L = ax.legend(handles=l, **lgd_kws)
496496
ax.figure.canvas.draw()
497-
L._legend_box.align = align
497+
# L._legend_box.align = align
498+
L._legend_box.align = 'center'
499+
L.get_title().set_ha('center')
498500
if color_text:
499501
for text in L.get_texts():
500502
try:
@@ -576,7 +578,7 @@ def plot_cmap_legend(
576578

577579
# =============================================================================
578580
def plot_marker_legend(
579-
obj=None, ax=None, title=None, color_text=True, label_side="right", kws=None
581+
obj=None, ax=None, title=None, color_text=True, kws=None
580582
):
581583
"""
582584
plot legned for different marker
@@ -587,7 +589,6 @@ def plot_marker_legend(
587589
ax: axes to plot the legend.
588590
title: title of legend.
589591
color_text: whether to change the color of text based on the color in D.
590-
label_side: right of left.
591592
kws: kws passed to plt.legend.
592593
593594
Returns
@@ -628,7 +629,7 @@ def plot_marker_legend(
628629
ms_dict = ms
629630

630631
lgd_kws.setdefault("markerscale", 1)
631-
lgd_kws.setdefault("handleheight", 1) # font size, units is points
632+
lgd_kws.setdefault("handleheight", 0.7) # font size, units is points
632633
lgd_kws.setdefault("handlelength", 1) # font size, units is points
633634
lgd_kws.setdefault(
634635
"borderaxespad", 0.1
@@ -642,12 +643,7 @@ def plot_marker_legend(
642643
lgd_kws.setdefault("columnspacing", 1)
643644
lgd_kws.setdefault("bbox_to_anchor", (0, 1))
644645
lgd_kws.setdefault("title", title)
645-
if label_side == "left":
646-
lgd_kws.setdefault("markerfirst", False)
647-
align = "right"
648-
else:
649-
lgd_kws.setdefault("markerfirst", True)
650-
align = "left"
646+
lgd_kws.setdefault("markerfirst", True)
651647

652648
availabel_height = (
653649
ax.figure.get_window_extent().height * lgd_kws["bbox_to_anchor"][1]
@@ -666,30 +662,31 @@ def plot_marker_legend(
666662
for l, m in markers.items()
667663
] # kws:?mpatches.Patch; rasterized=True
668664
ms = lgd_kws.pop("markersize", 10)
669-
Lgd = ax.legend(handles=L, **lgd_kws)
665+
lgd = ax.legend(handles=L, **lgd_kws)
670666
ax.figure.canvas.draw()
671-
while Lgd.get_window_extent().height > availabel_height:
667+
while lgd.get_window_extent().height > availabel_height:
672668
print("Incresing ncol")
673669
lgd_kws["ncol"] += 1
674670
if lgd_kws["ncol"] >= 3:
675671
print("More than 3 cols is not supported")
676-
Lgd.remove()
672+
lgd.remove()
677673
return None
678-
Lgd = ax.legend(handles=L, **lgd_kws)
674+
lgd = ax.legend(handles=L, **lgd_kws)
679675
ax.figure.canvas.draw()
680-
Lgd._legend_box.align = align
676+
lgd._legend_box.align = 'center'
677+
lgd.get_title().set_ha('center')
681678
if color_text:
682-
for text in Lgd.get_texts():
679+
for text in lgd.get_texts():
683680
try:
684681
lum = _calculate_luminance(color_dict[text.get_text()])
685682
text_color = "black" if lum > 0.408 else color_dict[text.get_text()]
686683
text.set_color(text_color)
687684
except:
688685
pass
689-
ax.add_artist(Lgd)
686+
ax.add_artist(lgd)
690687
ax.grid(False)
691688
# print(availabel_height,L.get_window_extent().height)
692-
return Lgd
689+
return lgd
693690

694691

695692
# =============================================================================
@@ -870,7 +867,6 @@ def plot_legend_list(
870867
D=obj,
871868
ax=ax1,
872869
title=title,
873-
label_side=legend_side,
874870
color_text=color_text,
875871
kws=legend_kws,
876872
)
@@ -906,7 +902,6 @@ def plot_legend_list(
906902
D=obj,
907903
ax=ax1,
908904
title=title,
909-
label_side=legend_side,
910905
color_text=color_text,
911906
kws=legend_kws,
912907
)
@@ -929,7 +924,6 @@ def plot_legend_list(
929924
obj=obj,
930925
ax=ax1,
931926
title=title,
932-
label_side=legend_side,
933927
color_text=color_text,
934928
kws=legend_kws,
935929
) # obj is a tuple: markers and colors
@@ -965,7 +959,6 @@ def plot_legend_list(
965959
obj=obj,
966960
ax=ax1,
967961
title=title,
968-
label_side=legend_side,
969962
color_text=color_text,
970963
kws=legend_kws,
971964
)

0 commit comments

Comments
 (0)