Skip to content

Commit 7ce467e

Browse files
committed
Add parameters for ylim & yline, place line at median if 0 not in range
1 parent c291dee commit 7ce467e

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

src/extra/utils/misc.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,10 @@ def hyperslicer2(arr, *args, ax=None, lognorm=False, colorbar=True, **kwargs):
196196
return controls
197197

198198

199-
def ridgeline_plot(data, *, fig=None, overlap=0.5, xlabel=None, ylabel="Per-line values",
200-
stack_label=None, stack_ticklabels=None):
199+
def ridgeline_plot(
200+
data, *, fig=None, overlap=0.5, xlabel=None, ylabel="Per-line values",
201+
ylim=None, yline=None, stack_label=None, stack_ticklabels=None
202+
):
201203
"""Make a ridgeline plot showing a sequence of similar lines
202204
203205
A ridgeline plot spreads out the different lines vertically to make their
@@ -213,6 +215,8 @@ def ridgeline_plot(data, *, fig=None, overlap=0.5, xlabel=None, ylabel="Per-line
213215
plot's area covered by the next plot.
214216
xlabel (str): Label for the shared x axis.
215217
ylabel (str): Label for the y axis (drawn on the bottom plot).
218+
ylim (tuple): Lower & upper limits for the y axis of each line.
219+
yline (float): Y value at which to draw a horizontal marker for each line.
216220
stack_label (str): Label for the stacking axis (shown on the right)
217221
stack_ticklabels (array_like): Labels for each line (shown on the right
218222
next to the zero line of each plot).
@@ -241,7 +245,20 @@ def ridgeline_plot(data, *, fig=None, overlap=0.5, xlabel=None, ylabel="Per-line
241245
x_data = np.arange(data.shape[1])
242246

243247
x_range = x_data.min(), x_data.max()
244-
y_range = data.min(), data.max()
248+
if ylim is not None:
249+
y_min, y_max = ylim
250+
else:
251+
y_min, y_max = data.min(), data.max()
252+
if y_min > 0 and (y_max / y_min) > 20:
253+
y_min = 0 # Data from just above 0
254+
elif y_max < 0 and (y_min / y_max) > 20:
255+
y_max = 0 # Data from just below 0
256+
257+
if yline is None:
258+
if y_min <= 0 <= y_max:
259+
yline = 0
260+
else:
261+
yline = np.median(data)
245262

246263
for i, trace in enumerate(data):
247264
ax = fig.add_subplot(gs[i:i + 1, 0:])
@@ -251,11 +268,11 @@ def ridgeline_plot(data, *, fig=None, overlap=0.5, xlabel=None, ylabel="Per-line
251268

252269
ax.plot(x_data, trace)
253270

254-
# Draw a light line at zero for each axis
255-
ax.axhline(color='0.7', linewidth=1., zorder=0)
271+
# Draw a light line to mark each separate dataset
272+
ax.axhline(yline, color='0.7', linewidth=1., zorder=0)
256273

257274
# Use the same scale on each axes
258-
ax.set_ylim(*y_range)
275+
ax.set_ylim(y_min, y_max)
259276
ax.set_xlim(*x_range)
260277

261278
if i < len(data) - 1:
@@ -274,7 +291,7 @@ def ridgeline_plot(data, *, fig=None, overlap=0.5, xlabel=None, ylabel="Per-line
274291
# the x coords of this transformation are axes, and the y coords are data
275292
if stack_ticklabels is not None:
276293
trans = blended_transform_factory(ax.transAxes, ax.transData)
277-
ax.text(1.02, 0, str(stack_ticklabels[i]), ha="left", va="center", transform=trans)
294+
ax.text(1.02, yline, str(stack_ticklabels[i]), ha="left", va="center", transform=trans)
278295

279296
if stack_label:
280297
fig.supylabel(stack_label, x=1., ha="right")

0 commit comments

Comments
 (0)