Skip to content

Commit 3eef6d4

Browse files
committed
fix bug for cal_legend_width
1 parent cd81cba commit 3eef6d4

File tree

3 files changed

+43
-51
lines changed

3 files changed

+43
-51
lines changed

PyComplexHeatmap/utils.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import matplotlib
99
import matplotlib.pylab as plt
1010
from matplotlib.colors import LinearSegmentedColormap
11+
from matplotlib.font_manager import FontProperties
1112
import matplotlib.lines as mlines
1213
import matplotlib.patches as mpatches
1314

@@ -658,16 +659,15 @@ def plot_marker_legend(
658659
# =============================================================================
659660
def cal_legend_width(legend_list):
660661
lgd_w = 4.5
661-
if isinstance(plt.rcParams["legend.fontsize"],int):
662-
lgd_fontsize=plt.rcParams["legend.fontsize"]
663-
else:
664-
lgd_fontsize=10
662+
lgd_fontsize=plt.rcParams["legend.fontsize"]
663+
if isinstance(lgd_fontsize, str): ##fontsize can be str, such as medium
664+
lgd_fontsize = FontProperties(size=lgd_fontsize).get_size_in_points()
665665
legend_width = 0
666666
for lgd in legend_list:
667667
obj, title, legend_kws, n, lgd_t = lgd
668668
if lgd_t == "color_dict":
669669
max_text_len = max(len(str(title)), max([len(str(k)) for k in obj]))
670-
fontsize = legend_kws.get("fontsize", plt.rcParams["legend.fontsize"])
670+
fontsize = legend_kws.get("fontsize", lgd_fontsize)
671671
lgd_w = (
672672
max_text_len * fontsize * 0.65 / 72 / mm2inch
673673
) # point to inches to mm. in average, width = height * 0.6
@@ -731,14 +731,14 @@ def plot_legend_list(
731731
) # labelpad unit is points
732732
left = ax.get_position().x1 + pad
733733
if legend_width is None:
734-
try:
735-
legend_width = (
736-
cal_legend_width(legend_list) + 3
737-
) # base width for color rectangle is set to 3 mm
738-
if verbose > 0:
739-
print(f"Estimated legend width: {legend_width} mm")
740-
except:
741-
legend_width=15
734+
# try:
735+
legend_width = (
736+
cal_legend_width(legend_list) + 3
737+
) # base width for color rectangle is set to 3 mm
738+
if verbose > 0:
739+
print(f"Estimated legend width: {legend_width} mm")
740+
# except:
741+
# legend_width=15
742742
legend_width = (
743743
legend_width * mm2inch * ax.figure.dpi / ax.figure.get_window_extent().width
744744
) # mm to px to fraction

notebooks/clustermap.ipynb

Lines changed: 29 additions & 37 deletions
Large diffs are not rendered by default.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
author_email="ding.wu.bin.gm@gmail.com",
2929
url="https://github.com/DingWB/PyComplexHeatmap",
3030
packages=["PyComplexHeatmap"], # src
31-
install_requires=["matplotlib","numpy","pandas>=1.3.5", "scipy","palettable"], #matplotlib<=3.7.5
31+
install_requires=["matplotlib","numpy","pandas>=1.3.5", "scipy","palettable"],
3232
include_package_data=True,
3333
)
3434
# rm -rf dist && rm -rf PyComplexHeatmap/PyComplexHeatmap.egg-info/

0 commit comments

Comments
 (0)