Skip to content

Commit 5945467

Browse files
committed
fix: yscale fixes
1 parent 5eb7e61 commit 5945467

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

Diff for: src/mplhep/plot.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import collections.abc
44
import inspect
55
import warnings
6+
import logging
67
from collections import OrderedDict, namedtuple
78
from typing import TYPE_CHECKING, Any, Union
89

@@ -962,6 +963,10 @@ def _draw_leg_bbox(ax):
962963
"""
963964
fig = ax.figure
964965
leg = ax.get_legend()
966+
if leg is None:
967+
leg = [
968+
c for c in ax.get_children() if isinstance(c, plt.matplotlib.legend.Legend)
969+
][0]
965970

966971
fig.canvas.draw()
967972
return leg.get_frame().get_bbox()
@@ -973,6 +978,7 @@ def _draw_text_bbox(ax):
973978
"""
974979
fig = ax.figure
975980
textboxes = [k for k in ax.get_children() if isinstance(k, AnchoredText)]
981+
fig.canvas.draw()
976982
if len(textboxes) > 1:
977983
print("Warning: More than one textbox found")
978984
for box in textboxes:
@@ -984,15 +990,17 @@ def _draw_text_bbox(ax):
984990
return bbox
985991

986992

987-
def yscale_legend(ax=None):
993+
def yscale_legend(ax=None, otol=0):
988994
"""
989-
Automatically scale y-axis up to fit in legend()
995+
Automatically scale y-axis up to fit in legend().
996+
Set `otol > 0` for less strict scaling.
990997
"""
991998
if ax is None:
992999
ax = plt.gca()
9931000

9941001
scale_factor = 10 ** (1.05) if ax.get_yscale() == "log" else 1.05
995-
while overlap(ax, _draw_leg_bbox(ax)) > 0:
1002+
while overlap(ax, _draw_leg_bbox(ax)) > otol:
1003+
logging.info("Scaling y-axis by 5% to fit legend")
9961004
ax.set_ylim(ax.get_ylim()[0], ax.get_ylim()[-1] * scale_factor)
9971005
ax.figure.canvas.draw()
9981006
return ax

0 commit comments

Comments
 (0)