Skip to content

Commit e2902b4

Browse files
committed
added tests
1 parent 861b066 commit e2902b4

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

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

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import plotly.express as px
22
import plotly.graph_objects as go
33
from numpy.testing import assert_array_equal
4+
import numpy as np
45

56

67
def _compare_figures(go_trace, px_fig):
@@ -52,29 +53,45 @@ def test_pie_like_px():
5253
_compare_figures(trace, fig)
5354

5455

55-
def test_pie_like_colors():
56+
def test_sunburst_treemap_colorscales():
5657
labels = ["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"]
5758
parents = ["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve"]
5859
values = [10, 14, 12, 10, 2, 6, 6, 4, 4]
59-
# Sunburst
60-
fig = px.sunburst(
61-
names=labels,
62-
parents=parents,
63-
values=values,
64-
color=values,
65-
color_continuous_scale="Viridis",
66-
range_color=(5, 15),
67-
)
68-
assert fig.layout.coloraxis.cmin, fig.layout.coloraxis.cmax == (5, 15)
69-
assert fig.layout.coloraxis.colorscale[0] == (0.0, "#440154")
70-
# Treemap
71-
fig = px.treemap(
72-
names=labels,
73-
parents=parents,
74-
values=values,
75-
color=values,
76-
color_continuous_scale="Viridis",
77-
range_color=(5, 15),
78-
)
79-
assert fig.layout.coloraxis.cmin, fig.layout.coloraxis.cmax == (5, 15)
80-
assert fig.layout.coloraxis.colorscale[0] == (0.0, "#440154")
60+
for func, colorway in zip(
61+
[px.sunburst, px.treemap], ["sunburstcolorway", "treemapcolorway"]
62+
):
63+
# Continuous colorscale
64+
fig = func(
65+
names=labels,
66+
parents=parents,
67+
values=values,
68+
color=values,
69+
color_continuous_scale="Viridis",
70+
range_color=(5, 15),
71+
)
72+
assert fig.layout.coloraxis.cmin, fig.layout.coloraxis.cmax == (5, 15)
73+
# Discrete colorscale, color arg passed
74+
color_seq = px.colors.sequential.Reds
75+
fig = func(
76+
names=labels,
77+
parents=parents,
78+
values=values,
79+
color=labels,
80+
color_discrete_sequence=color_seq,
81+
)
82+
assert np.all([col in color_seq for col in fig.data[0].marker.colors])
83+
# Numerical color arg passed, fall back to continuous
84+
fig = func(names=labels, parents=parents, values=values, color=values,)
85+
assert [
86+
el[0] == px.colors.sequential.Viridis
87+
for i, el in enumerate(fig.layout.coloraxis.colorscale)
88+
]
89+
# Discrete colorscale, no color arg passed
90+
color_seq = px.colors.sequential.Reds
91+
fig = func(
92+
names=labels,
93+
parents=parents,
94+
values=values,
95+
color_discrete_sequence=color_seq,
96+
)
97+
assert list(fig.layout[colorway]) == color_seq

0 commit comments

Comments
 (0)