Skip to content

Commit e4071a9

Browse files
px.Range
1 parent d57d035 commit e4071a9

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

packages/python/plotly/plotly/express/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@
5555
get_trendline_results,
5656
)
5757

58-
from ._special_inputs import ( # noqa: F401
59-
IdentityMap,
60-
Constant,
61-
)
58+
from ._special_inputs import IdentityMap, Constant, Range # noqa: F401
6259

6360
from . import data, colors # noqa: F401
6461

@@ -102,4 +99,5 @@
10299
"get_trendline_results",
103100
"IdentityMap",
104101
"Constant",
102+
"Range",
105103
]

packages/python/plotly/plotly/express/_core.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import plotly.graph_objs as go
22
import plotly.io as pio
33
from collections import namedtuple, OrderedDict
4-
from ._special_inputs import IdentityMap, Constant
4+
from ._special_inputs import IdentityMap, Constant, Range
55

66
from _plotly_utils.basevalidators import ColorscaleValidator
77
from .colors import qualitative, sequential
@@ -961,6 +961,7 @@ def build_dataframe(args, constructor):
961961
df_output[df_input.columns] = df_input[df_input.columns]
962962

963963
constants = dict()
964+
ranges = list()
964965

965966
# Loop over possible arguments
966967
for field_name in all_attrables:
@@ -993,13 +994,16 @@ def build_dataframe(args, constructor):
993994
"pandas MultiIndex is not supported by plotly express "
994995
"at the moment." % field
995996
)
996-
# ----------------- argument is a constant ----------------------
997-
if isinstance(argument, Constant):
997+
# ----------------- argument is a special value ----------------------
998+
if isinstance(argument, Constant) or isinstance(argument, Range):
998999
col_name = _check_name_not_reserved(
9991000
str(argument.label) if argument.label is not None else field,
10001001
reserved_names,
10011002
)
1002-
constants[col_name] = argument.value
1003+
if isinstance(argument, Constant):
1004+
constants[col_name] = argument.value
1005+
else:
1006+
ranges.append(col_name)
10031007
# ----------------- argument is a col name ----------------------
10041008
elif isinstance(argument, str) or isinstance(
10051009
argument, int
@@ -1087,6 +1091,9 @@ def build_dataframe(args, constructor):
10871091
for col_name in constants:
10881092
df_output[col_name] = constants[col_name]
10891093

1094+
for col_name in ranges:
1095+
df_output[col_name] = range(len(df_output))
1096+
10901097
if wide_mode:
10911098
# TODO multi-level index
10921099
# TODO multi-level columns

packages/python/plotly/plotly/express/_special_inputs.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,14 @@ class Constant(object):
2727
def __init__(self, value, label=None):
2828
self.value = value
2929
self.label = label
30+
31+
32+
class Range(object):
33+
"""
34+
Objects of this class can be passed to Plotly Express functions that expect column
35+
identifiers or list-like objects to indicate that this attribute should be mapped
36+
onto integers starting at 0. An optional label can be provided.
37+
"""
38+
39+
def __init__(self, label=None):
40+
self.label = label

packages/python/plotly/plotly/tests/test_core/test_px/test_px_input.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,21 @@ def test_constants():
374374
assert fig.layout.legend.title.text == "symbol"
375375

376376

377+
def test_ranges():
378+
fig = px.scatter(x=px.Range(), y=[1, 2], hover_data=[px.Range()])
379+
assert fig.data[0].x[0] == 0
380+
assert fig.data[0].x[1] == 1
381+
assert fig.data[0].customdata[0][0] == 0
382+
assert fig.data[0].customdata[1][0] == 1
383+
assert "x=" in fig.data[0].hovertemplate
384+
385+
fig = px.scatter(x=px.Range(label="time"), y=[1, 2])
386+
assert fig.data[0].x[0] == 0
387+
assert fig.data[0].x[1] == 1
388+
assert "x=" not in fig.data[0].hovertemplate
389+
assert "time=" in fig.data[0].hovertemplate
390+
391+
377392
def test_auto_orient():
378393
categorical = ["a", "a", "b", "b"]
379394
numerical = [1, 2, 3, 4]

0 commit comments

Comments
 (0)