Skip to content

Commit

Permalink
update plot_result submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-held committed Sep 17, 2022
1 parent f0f9593 commit bf65ea2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
29 changes: 14 additions & 15 deletions src/cabinetry/visualize/plot_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import List, Optional, Union

import matplotlib as mpl
import matplotlib.layout_engine
import matplotlib.pyplot as plt
import numpy as np

Expand Down Expand Up @@ -41,6 +42,7 @@ def correlation_matrix(
fig, ax = plt.subplots(
figsize=(round(5 + len(labels) / 1.6, 1), round(3 + len(labels) / 1.6, 1)),
dpi=100,
layout="tight",
)
im = ax.imshow(corr_mat, vmin=-1, vmax=1, cmap="RdBu")

Expand All @@ -54,7 +56,6 @@ def correlation_matrix(

fig.colorbar(im, ax=ax)
ax.set_aspect("auto") # to get colorbar aligned with matrix
fig.set_tight_layout(True)

# add correlation as text
for (j, i), corr in np.ndenumerate(corr_mat):
Expand Down Expand Up @@ -91,7 +92,7 @@ def pulls(
"""
num_pars = len(bestfit)
y_positions = np.arange(num_pars)[::-1]
fig, ax = plt.subplots(figsize=(6, 1 + num_pars / 4), dpi=100)
fig, ax = plt.subplots(figsize=(6, 1 + num_pars / 4), dpi=100, layout="tight")
ax.errorbar(bestfit, y_positions, xerr=uncertainty, fmt="o", color="black")

ax.fill_between([-2, 2], -0.5, len(bestfit) - 0.5, color="yellow")
Expand All @@ -106,7 +107,6 @@ def pulls(
ax.xaxis.set_minor_locator(mpl.ticker.AutoMinorLocator()) # minor ticks
ax.tick_params(axis="both", which="major", pad=8)
ax.tick_params(direction="in", top=True, right=True, which="both")
fig.set_tight_layout(True)

utils._save_and_close(fig, figure_path, close_figure)
return fig
Expand Down Expand Up @@ -147,8 +147,14 @@ def ranking(
"""
num_pars = len(bestfit)

mpl.style.use("seaborn-colorblind")
fig, ax_pulls = plt.subplots(figsize=(8, 2.5 + num_pars * 0.45), dpi=100)
# layout to make space for legend on top
leg_space = 1.0 / (num_pars + 3) + 0.03
layout = matplotlib.layout_engine.TightLayoutEngine(rect=[0, 0, 1.0, 1 - leg_space])

mpl.style.use("seaborn-v0_8-colorblind")
fig, ax_pulls = plt.subplots(
figsize=(8, 2.5 + num_pars * 0.45), dpi=100, layout=layout
)
ax_impact = ax_pulls.twiny() # second x-axis with shared y-axis, used for pulls

# since pull axis is below impact axis, flip them so pulls show up on top
Expand Down Expand Up @@ -228,9 +234,6 @@ def ranking(
ncol=3,
fontsize="large",
)
leg_space = 1.0 / (num_pars + 3) + 0.03
# there might be a way to use set_tight_layout here as well with a bounding box
fig.tight_layout(rect=[0, 0, 1.0, 1 - leg_space]) # make space for legend on top

utils._save_and_close(fig, figure_path, close_figure)
return fig
Expand Down Expand Up @@ -263,8 +266,8 @@ def scan(
Returns:
matplotlib.figure.Figure: the likelihood scan figure
"""
mpl.style.use("seaborn-colorblind")
fig, ax = plt.subplots()
mpl.style.use("seaborn-v0_8-colorblind")
fig, ax = plt.subplots(layout="tight")

y_lim = max(par_nlls) * 1.2 # upper y-axis limit, 20% headroom

Expand Down Expand Up @@ -308,8 +311,6 @@ def scan(

ax.legend(frameon=False, fontsize="large")

fig.set_tight_layout(True)

utils._save_and_close(fig, figure_path, close_figure)
return fig

Expand Down Expand Up @@ -339,7 +340,7 @@ def limit(
Returns:
matplotlib.figure.Figure: the CLs figure
"""
fig, ax = plt.subplots()
fig, ax = plt.subplots(layout="tight")

xmin = min(poi_values)
xmax = max(poi_values)
Expand Down Expand Up @@ -403,7 +404,5 @@ def limit(
ax.tick_params(axis="both", which="major", pad=8)
ax.tick_params(direction="in", top=True, right=True, which="both")

fig.set_tight_layout(True)

utils._save_and_close(fig, figure_path, close_figure)
return fig
Binary file modified tests/visualize/reference/scan.png
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/visualize/test_visualize_plot_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def test_scan(tmp_path):

# compare figure returned by function
fname = tmp_path / "fig_from_return.png"
fig.set_tight_layout(False) # https://github.com/matplotlib/matplotlib/issues/21742
fig.set_layout_engine(None) # https://github.com/matplotlib/matplotlib/issues/21742
fig.savefig(fname)
assert compare_images("tests/visualize/reference/scan.png", str(fname), 0) is None

Expand Down

0 comments on commit bf65ea2

Please sign in to comment.