Skip to content

Commit 476c599

Browse files
Merge pull request #333 from tpvasconcelos/y_labels
Add support for custom row labels
2 parents 4da7ba2 + 34ba839 commit 476c599

16 files changed

Lines changed: 164 additions & 68 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ samples = [
144144
# And finish by styling it up to your liking!
145145
fig = ridgeplot(
146146
samples=samples,
147-
labels=months,
147+
row_labels=months,
148148
colorscale="Inferno",
149149
bandwidth=4,
150150
kde_points=np.linspace(-40, 110, 400),

cicd_utils/ridgeplot_examples/_lincoln_weather.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def main(
3333

3434
fig = ridgeplot(
3535
samples=samples,
36-
labels=months,
36+
row_labels=months,
3737
colorscale=colorscale,
3838
colormode=colormode,
3939
bandwidth=4,

docs/getting_started/getting_started.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ samples = [
134134
# And finish by styling it up to your liking!
135135
fig = ridgeplot(
136136
samples=samples,
137-
labels=months,
137+
row_labels=months,
138138
colorscale="Inferno",
139139
bandwidth=4,
140140
kde_points=np.linspace(-40, 110, 400),
@@ -228,7 +228,7 @@ Finally, we can pass the {py:paramref}`~ridgeplot.ridgeplot.samples` list to the
228228
```python
229229
fig = ridgeplot(
230230
samples=samples,
231-
labels=months,
231+
row_labels=months,
232232
colorscale="Inferno",
233233
bandwidth=4,
234234
kde_points=np.linspace(-40, 110, 400),

docs/reference/changelog.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ This document outlines the list of changes to ridgeplot between each release. Fo
55
Unreleased changes
66
------------------
77

8-
- ...
8+
### Features
9+
10+
- Add support for custom row labels via a new `row_labels` argument ({gh-pr}`333`)
11+
12+
### Deprecations
13+
14+
- Deprecated the `show_yticklabels` argument in favor of the new more general and flexible `row_labels` argument ({gh-pr}`333`)
915

1016
---
1117

mypy.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ plugins = numpy.typing.mypy_plugin
4444

4545
[mypy-importlib_resources.*]
4646
ignore_missing_imports = True
47-
[mypy-plotly.*]
48-
ignore_missing_imports = True
4947
[mypy-_plotly_utils.*]
5048
ignore_missing_imports = True
5149
[mypy-pre_commit_hooks.*]

requirements/typing.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ types-pytz
77
types-PyYAML
88
types-requests
99
types-tqdm
10+
plotly-stubs; python_version >= "3.10"
1011
pandas-stubs
1112

1213
# pyright also needs to inherit other environment dependencies in

src/ridgeplot/_figure_factory.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,14 @@ def normalise_trace_labels(
7474
return trace_labels
7575

7676

77-
def normalise_y_labels(trace_labels: LabelsArray) -> LabelsArray:
78-
return [ordered_dedup(row) for row in trace_labels]
77+
def normalise_row_labels(trace_labels: LabelsArray) -> Collection[str]:
78+
return [",".join(ordered_dedup(row)) for row in trace_labels]
7979

8080

8181
def update_layout(
8282
fig: go.Figure,
83-
y_labels: LabelsArray,
83+
row_labels: Collection[str] | Literal[False],
8484
tickvals: list[float],
85-
show_yticklabels: bool,
8685
xpad: float,
8786
x_max: float,
8887
x_min: float,
@@ -96,11 +95,15 @@ def update_layout(
9695
showgrid=True,
9796
)
9897
fig.update_yaxes(
99-
showticklabels=show_yticklabels,
100-
tickvals=tickvals,
101-
ticktext=y_labels,
98+
showticklabels=row_labels is not False,
10299
**axes_common,
103100
)
101+
if row_labels is not False:
102+
fig.update_yaxes(
103+
tickmode="array",
104+
tickvals=tickvals,
105+
ticktext=row_labels,
106+
)
104107
x_padding = xpad * (x_max - x_min)
105108
fig.update_xaxes(
106109
range=[x_min - x_padding, x_max + x_padding],
@@ -122,14 +125,14 @@ def update_layout(
122125
def create_ridgeplot(
123126
densities: Densities,
124127
trace_types: TraceTypesArray | ShallowTraceTypesArray | TraceType,
128+
row_labels: Collection[str] | None | Literal[False],
125129
colorscale: ColorScale | Collection[Color] | str | None,
126130
opacity: float | None,
127131
colormode: Literal["fillgradient"] | SolidColormode,
128132
trace_labels: LabelsArray | ShallowLabelsArray | None,
129133
line_color: Color | Literal["fill-color"],
130134
line_width: float | None,
131135
spacing: float,
132-
show_yticklabels: bool,
133136
xpad: float,
134137
) -> go.Figure:
135138
# ==============================================================
@@ -152,12 +155,14 @@ def create_ridgeplot(
152155
trace_labels=trace_labels,
153156
n_traces=n_traces,
154157
)
155-
y_labels = normalise_y_labels(trace_labels)
158+
if row_labels is None:
159+
row_labels = normalise_row_labels(trace_labels)
160+
elif row_labels is not False and len(row_labels) != n_rows:
161+
raise ValueError(f"Expected {n_rows} row_labels, got {len(row_labels)} instead.")
156162

157163
# Force cast certain arguments to the expected types
158164
line_width = float(line_width) if line_width is not None else None
159165
spacing = float(spacing)
160-
show_yticklabels = bool(show_yticklabels)
161166
xpad = float(xpad)
162167
colorscale = validate_coerce_colorscale(colorscale)
163168

@@ -182,13 +187,13 @@ def create_ridgeplot(
182187
tickvals: list[float] = []
183188
fig = go.Figure()
184189
ith_trace = 0
185-
for ith_row, (row_traces, row_trace_types, row_labels, row_colors) in enumerate(
190+
for ith_row, (row_traces, row_trace_types, row_trace_labels, row_colors) in enumerate(
186191
zip_strict(densities, trace_types, trace_labels, solid_colors)
187192
):
188193
y_base = float(-ith_row * y_max * spacing)
189194
tickvals.append(y_base)
190195
for trace, trace_type, label, color in zip_strict(
191-
row_traces, row_trace_types, row_labels, row_colors
196+
row_traces, row_trace_types, row_trace_labels, row_colors
192197
):
193198
trace_drawer = get_trace_cls(trace_type)(
194199
trace=trace,
@@ -212,9 +217,8 @@ def create_ridgeplot(
212217

213218
fig = update_layout(
214219
fig,
215-
y_labels=y_labels,
220+
row_labels=row_labels,
216221
tickvals=tickvals,
217-
show_yticklabels=show_yticklabels,
218222
xpad=xpad,
219223
x_max=x_max,
220224
x_min=x_min,

src/ridgeplot/_ridgeplot.py

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def ridgeplot(
101101
densities: Densities | ShallowDensities | None = None,
102102
trace_type: TraceTypesArray | ShallowTraceTypesArray | TraceType | None = None,
103103
labels: LabelsArray | ShallowLabelsArray | None = None,
104+
row_labels: Collection[str] | None | Literal[False] = None,
104105
# KDE parameters
105106
kernel: str = "gau",
106107
bandwidth: KDEBandwidth = "normal_reference",
@@ -117,11 +118,11 @@ def ridgeplot(
117118
line_color: Color | Literal["fill-color"] = "black",
118119
line_width: float | None = None,
119120
spacing: float = 0.5,
120-
show_yticklabels: bool = True,
121121
xpad: float = 0.05,
122122
# Deprecated parameters
123123
coloralpha: float | None | MissingType = MISSING,
124124
linewidth: float | MissingType = MISSING,
125+
show_yticklabels: bool | MissingType = MISSING,
125126
) -> go.Figure:
126127
r"""Return an interactive ridgeline (Plotly) |~go.Figure|.
127128
@@ -196,10 +197,24 @@ def ridgeplot(
196197
.. versionadded:: 0.3.0
197198
198199
labels : LabelsArray or ShallowLabelsArray or None
199-
A list of string labels for each trace. If not specified (default), the
200-
labels will be automatically generated as ``"Trace {n}"``, where ``n``
201-
is the trace's index. If instead a list of labels is specified, it
202-
should have the same shape as the samples array.
200+
A collection of string labels for each trace. If not specified
201+
(default), the labels will be automatically generated as
202+
``"Trace {i}"``, where ``i`` is the trace's index. If instead a
203+
collection of labels is specified, it should have the same shape as the
204+
samples array.
205+
206+
row_labels : Collection[str] or None or False
207+
A collection of string labels for each row in the ridgeline plot. If
208+
specified, the length of this collection should match the number of rows
209+
in the plot (i.e., the :math:`R` dimension in the :paramref:`.samples`
210+
or :paramref:`.densities` parameter). If not specified (default), the
211+
row labels displayed on the y-axis will be automatically generated based
212+
on the :paramref:`.labels` argument. If set to ``False``, the row
213+
labels won't be displayed at all.
214+
215+
.. versionadded:: 0.4.0
216+
Added support for custom row labels, and replaced the deprecated
217+
:paramref:`.show_yticklabels` parameter.
203218
204219
kernel : str
205220
The Kernel to be used during Kernel Density Estimation. The default is
@@ -226,14 +241,14 @@ def ridgeplot(
226241
``"scott"`` bandwidth for gaussian kernels. See `bandwidths.py`_.
227242
- If a float is given, its value is used as the bandwidth.
228243
- If a callable is given, it's return value is used. The callable
229-
should take exactly two parameters, i.e., ``fn(x, kern)``, and return
244+
should take exactly two arguments, i.e., ``fn(x, kern)``, and return
230245
a float, where:
231246
232247
- ``x``: the clipped input data
233248
- ``kern``: the kernel instance used
234249
235250
kde_points : KDEPoints
236-
This argument controls the points at which KDE is computed. If an
251+
This parameter controls the points at which KDE is computed. If an
237252
``int`` value is passed (default=500), the densities will be evaluated
238253
at ``kde_points`` evenly spaced points between the min and max of each
239254
set of samples. Optionally, you can also pass a custom 1D numerical
@@ -278,7 +293,7 @@ def ridgeplot(
278293
template.
279294
280295
colormode : "fillgradient" or SolidColormode
281-
This argument controls the logic used for the coloring of each
296+
This parameter controls the logic used for the coloring of each
282297
ridgeline trace.
283298
284299
The ``"fillgradient"`` mode (default) will fill each trace with a
@@ -323,13 +338,13 @@ def ridgeplot(
323338
``"fillgradient"``.
324339
325340
opacity : float or None
326-
If None (default), this argument will be ignored and the transparency
341+
If None (default), this parameter will be ignored and the transparency
327342
values of the specified color-scale will remain untouched. Otherwise,
328343
if a float value is passed, it will be used to overwrite the
329344
opacity/transparency of the color-scale's colors.
330345
331346
.. versionadded:: 0.2.0
332-
Replaces the deprecated :paramref:`.coloralpha` argument.
347+
Replaces the deprecated :paramref:`.coloralpha` parameter.
333348
334349
line_color : Color or "fill-color"
335350
The color of the traces' lines. Any valid CSS color is allowed
@@ -347,20 +362,14 @@ def ridgeplot(
347362
of 0.5 px.
348363
349364
.. versionadded:: 0.2.0
350-
Replaces the deprecated :paramref:`.linewidth` argument.
365+
Replaces the deprecated :paramref:`.linewidth` parameter.
351366
352367
.. versionchanged:: 0.2.0
353368
The default value changed from 1 to 1.5
354369
355370
spacing : float
356371
The vertical spacing between density traces, which is defined in units
357-
of the highest distribution (i.e. the maximum y-value).
358-
359-
show_yticklabels : bool
360-
Whether to show the tick labels on the y-axis. The default is True.
361-
362-
.. versionadded:: 0.1.21
363-
Replaces the deprecated :paramref:`.show_annotations` argument.
372+
of the highest distribution (i.e., the maximum y-value).
364373
365374
xpad : float
366375
Specifies the extra padding to use on the x-axis. It is defined in
@@ -377,6 +386,11 @@ def ridgeplot(
377386
.. deprecated:: 0.2.0
378387
Use :paramref:`.line_width` instead.
379388
389+
show_yticklabels : bool
390+
391+
.. deprecated:: 0.4.0
392+
Use :paramref:`.row_labels` instead.
393+
380394
Returns
381395
-------
382396
:class:`plotly.graph_objects.Figure`
@@ -437,6 +451,21 @@ def ridgeplot(
437451
)
438452
line_width = linewidth
439453

454+
if show_yticklabels is not MISSING:
455+
if row_labels is not None:
456+
raise ValueError(
457+
"You may not specify both the 'show_yticklabels' and 'row_labels' arguments! "
458+
"HINT: Use the new 'row_labels' argument instead of the deprecated "
459+
"'show_yticklabels'."
460+
)
461+
warnings.warn(
462+
"The 'show_yticklabels' argument has been deprecated in favor of 'row_labels'. "
463+
"Support for the deprecated argument will be removed in a future version.",
464+
DeprecationWarning,
465+
stacklevel=2,
466+
)
467+
row_labels = False if not show_yticklabels else None
468+
440469
if colorscale == "default":
441470
warnings.warn(
442471
"colorscale='default' is deprecated and support for it will be removed in a future "
@@ -454,13 +483,13 @@ def ridgeplot(
454483
densities=densities,
455484
trace_labels=labels,
456485
trace_types=trace_type,
486+
row_labels=row_labels,
457487
colorscale=colorscale,
458488
opacity=opacity,
459489
colormode=colormode,
460490
line_color=line_color,
461491
line_width=line_width,
462492
spacing=spacing,
463-
show_yticklabels=show_yticklabels,
464493
xpad=xpad,
465494
)
466495
return fig

tests/e2e/artifacts/basic.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)