Skip to content

Commit

Permalink
more changes for mpl
Browse files Browse the repository at this point in the history
  • Loading branch information
scarlehoff committed Nov 22, 2024
1 parent 0a586ea commit 2885f3d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 28 deletions.
29 changes: 13 additions & 16 deletions validphys2/src/validphys/dataplots.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,6 @@ def _plot_fancy_impl(
# the rest.
next_color = itertools.chain(['#262626'], plotutils.color_iter())

# Extract the y-limits for this particular plot. This is needed in order
# to define the y-range because `ScaledTranslation` acts at the level of
# display coordinates, so there is no way the ranges of the offset data
# can be known a priori.
ymin, ymax = plotutils.extract_ylims(line_data, len(results))

for i, (res, lb, color) in enumerate(zip(results, labellist, next_color)):
if labels:
if lb:
Expand All @@ -367,14 +361,8 @@ def _plot_fancy_impl(
transform=next(offset_iter),
)

# This has to be called before instantiating the drawing
if normalize_to is not None and not np.isnan([ymin, ymax]).any():
margin_fraction = 0.05 # 5% margin
margin = (ymax - ymin) * margin_fraction
ax.set_ylim([ymin - margin, ymax + margin])

# We 'plot' the empty lines to get the labels. But
# if everything is rmpty we skip the plot.
# if everything is empty we skip the plot.
if np.any(np.isfinite(cv)):
max_vals.append(np.nanmax(cv + err))
min_vals.append(np.nanmin(cv - err))
Expand All @@ -401,13 +389,22 @@ def _plot_fancy_impl(
if info.x_scale:
ax.set_xscale(info.x_scale)

if info.y_scale:
ax.set_yscale(info.y_scale)

if normalize_to is None:
if info.y_label:
ax.set_ylabel(info.y_label)
if info.y_scale:
ax.set_yscale(info.y_scale)
else:
# Rebuild the limits of the plot looking at the max and min values
margin_fraction = 0.05
ymax = max(max_vals)
ymin = min(min_vals)
margin = (ymax - ymin) * margin_fraction
ax.set_ylim((ymin - margin, ymax + margin))

# Normalized plots should always be linear in the y axis
ax.set_yscale("linear")

lb = labellist[normalize_to]
ax.set_ylabel(f"Ratio to {lb if lb else norm_result.label}")

Expand Down
12 changes: 0 additions & 12 deletions validphys2/src/validphys/plotutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,18 +302,6 @@ def offset_xcentered(n, ax, *, offset_prop=0.05):
yield offset_transform


def extract_ylims(line_data, ndata):
"""Extract the lowest and maximum values of all the data and predictions
by accounting for the error bars.
"""
min_vals = []
max_vals = []
for i in range(ndata):
min_vals.append(line_data[('cv', i)].values - line_data[('err', i)].values)
max_vals.append(line_data[('cv', i)].values + line_data[('err', i)].values)
return np.nanmin(min_vals), np.nanmax(max_vals)


def centered_range(n, value=0, distance=1):
"""Generte a range of ``n`` points centered
around ``value``, unifirmely sampled at
Expand Down

0 comments on commit 2885f3d

Please sign in to comment.