Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: top right corner position, inside the frame, with loc=5 parameter #423

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/mplhep/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def exp_text(
- 1 : Top left corner
- 2 : Top left corner, multiline
- 3 : Split EXP above axes, rest of label in top left corner"
- 4 : Top left corner, inside the frame
- 5 : Top right corner, inside the frame
ax : matplotlib.axes.Axes, optional
Axes object (if None, last one is fetched)
fontname : string, optional
Expand Down Expand Up @@ -77,6 +79,7 @@ def exp_text(
loc1_dict = {
0: {"xy": (0.001, 1 + pad), "va": "bottom"},
1: {"xy": (0.05, 0.95 - pad), "va": "top"},
2: {"xy": (0.56, 0.95 - pad), "va": "top"},
}

loc2_dict = {
Expand All @@ -85,15 +88,18 @@ def exp_text(
2: {"xy": (0.05, 0.9450 - pad), "va": "top"},
3: {"xy": (0.05, 0.95 - pad), "va": "top"},
4: {"xy": (0.05, 0.9550 - pad), "va": "bottom"},
5: {"xy": (0.5650, 0.9550 - pad), "va": "bottom"},
}

if loc not in [0, 1, 2, 3, 4]:
if loc not in [0, 1, 2, 3, 4, 5]:
raise ValueError(
"loc must be in {0, 1, 2}:\n"
"loc must be in {0, 1, 2, 3, 4, 5}:\n"
"0 : Above axes, left aligned\n"
"1 : Top left corner\n"
"2 : Top left corner, multiline\n"
"3 : Split EXP above axes, rest of label in top left corner\n"
"4 : Top left corner, inside the frame \n"
"5 : Top right corner, inside the frame \n"
)

def pixel_to_axis(extent, ax=None):
Expand All @@ -119,6 +125,8 @@ def dist(tup):

if loc in [0, 3]:
_exp_loc = 0
elif loc == 5:
_exp_loc = 2
else:
_exp_loc = 1
_formater = ax.get_yaxis().get_major_formatter()
Expand Down Expand Up @@ -152,7 +160,7 @@ def dist(tup):
_t = mtransforms.offset_copy(
exptext._transform, x=_exp_xoffset, units="inches", fig=ax.figure
)
elif loc in [1, 4]:
elif loc in [1, 4, 5]:
_t = mtransforms.offset_copy(
exptext._transform,
x=_exp_xoffset,
Expand Down Expand Up @@ -256,8 +264,9 @@ def exp_label(
- 0 : Above axes, left aligned
- 1 : Top left corner
- 2 : Top left corner, multiline
- 3 : Split EXP above axes, rest of label in top left corner"
- 3 : Split EXP above axes, rest of label in top left corner
- 4 : (1) Top left corner, but align "rlabel" underneath
- 5 : (1) Top right corner, but align "rlabel" underneath
ax : matplotlib.axes.Axes, optional
Axes object (if None, last one is fetched)
data : bool, optional
Expand Down Expand Up @@ -339,7 +348,7 @@ def exp_label(
italic=italic,
pad=pad,
)
if loc == 4:
if loc >= 4:
_t = mtransforms.offset_copy(
exptext._transform,
y=-exptext.get_window_extent().height / ax.figure.dpi,
Expand Down
Binary file modified tests/baseline/test_label_loc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/test_styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def test_labeltext_loc():

@pytest.mark.mpl_image_compare(style="default")
def test_label_loc():
fig, axs = plt.subplots(1, 5, figsize=(20, 4))
fig, axs = plt.subplots(2, 3, figsize=(20, 4))
for i, ax in enumerate(axs.flatten()):
hep.cms.label("Preliminary", loc=i, ax=ax, lumi=50, data=True)
return fig
Expand Down