Skip to content

Commit 0d49938

Browse files
fix: yscale_legend to use paths and not rectangles (#482)
* fix: yscale_legend to use paths and not rectangles * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 4347e78 commit 0d49938

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

Diff for: src/mplhep/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
sort_legend,
2424
ylow,
2525
yscale_legend,
26-
yscale_text,
26+
yscale_anchored_text,
2727
)
2828
from .styles import set_style
2929

@@ -65,7 +65,7 @@
6565
"hist2dplot",
6666
"mpl_magic",
6767
"yscale_legend",
68-
"yscale_text",
68+
"yscale_anchored_text",
6969
"ylow",
7070
"rescale_to_axessize",
7171
"box_aspect",

Diff for: src/mplhep/plot.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,7 @@ def overlap(ax, bbox, get_vertices=False):
921921
"""
922922
from matplotlib.lines import Line2D
923923
from matplotlib.patches import Patch, Rectangle
924+
from matplotlib.text import Text
924925

925926
# From
926927
# https://github.com/matplotlib/matplotlib/blob/08008d5cb4d1f27692e9aead9a76396adc8f0b19/lib/matplotlib/legend.py#L845
@@ -936,13 +937,15 @@ def overlap(ax, bbox, get_vertices=False):
936937

937938
for handle in ax.patches:
938939
assert isinstance(handle, Patch)
939-
940940
if isinstance(handle, Rectangle):
941941
transform = handle.get_data_transform()
942942
bboxes.append(handle.get_bbox().transformed(transform))
943943
else:
944-
transform = handle.get_transform()
945-
bboxes.append(handle.get_path().get_extents(transform))
944+
lines.append(handle.get_path().interpolated(20))
945+
946+
for handle in ax.texts:
947+
assert isinstance(handle, Text)
948+
bboxes.append(handle.get_window_extent())
946949

947950
# TODO Possibly other objects
948951

@@ -1000,20 +1003,24 @@ def yscale_legend(ax=None, otol=0):
10001003

10011004
scale_factor = 10 ** (1.05) if ax.get_yscale() == "log" else 1.05
10021005
while overlap(ax, _draw_leg_bbox(ax)) > otol:
1006+
logging.debug(
1007+
f"Legend overlap with other artists is {overlap(ax, _draw_leg_bbox(ax))}."
1008+
)
10031009
logging.info("Scaling y-axis by 5% to fit legend")
10041010
ax.set_ylim(ax.get_ylim()[0], ax.get_ylim()[-1] * scale_factor)
10051011
ax.figure.canvas.draw()
10061012
return ax
10071013

10081014

1009-
def yscale_text(ax=None):
1015+
def yscale_anchored_text(ax=None, otol=0):
10101016
"""
10111017
Automatically scale y-axis up to fit AnchoredText
1018+
Set `otol > 0` for less strict scaling.
10121019
"""
10131020
if ax is None:
10141021
ax = plt.gca()
10151022

1016-
while overlap(ax, _draw_text_bbox(ax)) > 0:
1023+
while overlap(ax, _draw_text_bbox(ax)) > otol:
10171024
ax.set_ylim(ax.get_ylim()[0], ax.get_ylim()[-1] * 1.1)
10181025
ax.figure.canvas.draw()
10191026
return ax
@@ -1060,7 +1067,7 @@ def mpl_magic(ax=None, info=True):
10601067

10611068
ax = ylow(ax)
10621069
ax = yscale_legend(ax)
1063-
ax = yscale_text(ax)
1070+
ax = yscale_anchored_text(ax)
10641071

10651072
return ax
10661073

0 commit comments

Comments
 (0)