|
8 | 8 | import matplotlib |
9 | 9 | import matplotlib.pylab as plt |
10 | 10 | from matplotlib.colors import LinearSegmentedColormap |
| 11 | +from matplotlib.font_manager import FontProperties |
11 | 12 | import matplotlib.lines as mlines |
12 | 13 | import matplotlib.patches as mpatches |
13 | 14 |
|
@@ -658,16 +659,15 @@ def plot_marker_legend( |
658 | 659 | # ============================================================================= |
659 | 660 | def cal_legend_width(legend_list): |
660 | 661 | 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() |
665 | 665 | legend_width = 0 |
666 | 666 | for lgd in legend_list: |
667 | 667 | obj, title, legend_kws, n, lgd_t = lgd |
668 | 668 | if lgd_t == "color_dict": |
669 | 669 | 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) |
671 | 671 | lgd_w = ( |
672 | 672 | max_text_len * fontsize * 0.65 / 72 / mm2inch |
673 | 673 | ) # point to inches to mm. in average, width = height * 0.6 |
@@ -731,14 +731,14 @@ def plot_legend_list( |
731 | 731 | ) # labelpad unit is points |
732 | 732 | left = ax.get_position().x1 + pad |
733 | 733 | 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 |
742 | 742 | legend_width = ( |
743 | 743 | legend_width * mm2inch * ax.figure.dpi / ax.figure.get_window_extent().width |
744 | 744 | ) # mm to px to fraction |
|
0 commit comments