-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathtest_facets.py
130 lines (112 loc) · 4.69 KB
/
test_facets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import plotly.express as px
from pytest import approx
import pytest
import random
def test_facets(backend):
df = px.data.tips(return_type=backend)
fig = px.scatter(df, x="total_bill", y="tip")
assert "xaxis2" not in fig.layout
assert "yaxis2" not in fig.layout
assert fig.layout.xaxis.domain == (0.0, 1.0)
assert fig.layout.yaxis.domain == (0.0, 1.0)
fig = px.scatter(df, x="total_bill", y="tip", facet_row="sex", facet_col="smoker")
assert fig.layout.xaxis4.domain[0] - fig.layout.xaxis.domain[1] == approx(0.02)
assert fig.layout.yaxis4.domain[0] - fig.layout.yaxis.domain[1] == approx(0.03)
fig = px.scatter(df, x="total_bill", y="tip", facet_col="day", facet_col_wrap=2)
assert fig.layout.xaxis4.domain[0] - fig.layout.xaxis.domain[1] == approx(0.02)
assert fig.layout.yaxis4.domain[0] - fig.layout.yaxis.domain[1] == approx(0.07)
fig = px.scatter(
df,
x="total_bill",
y="tip",
facet_row="sex",
facet_col="smoker",
facet_col_spacing=0.09,
facet_row_spacing=0.08,
)
assert fig.layout.xaxis4.domain[0] - fig.layout.xaxis.domain[1] == approx(0.09)
assert fig.layout.yaxis4.domain[0] - fig.layout.yaxis.domain[1] == approx(0.08)
fig = px.scatter(
df,
x="total_bill",
y="tip",
facet_col="day",
facet_col_wrap=2,
facet_col_spacing=0.09,
facet_row_spacing=0.08,
)
assert fig.layout.xaxis4.domain[0] - fig.layout.xaxis.domain[1] == approx(0.09)
assert fig.layout.yaxis4.domain[0] - fig.layout.yaxis.domain[1] == approx(0.08)
def test_facets_with_marginals(backend):
df = px.data.tips(return_type=backend)
fig = px.histogram(df, x="total_bill", facet_col="sex", marginal="rug")
assert len(fig.data) == 4
fig = px.histogram(df, x="total_bill", facet_row="sex", marginal="rug")
assert len(fig.data) == 2
fig = px.histogram(df, y="total_bill", facet_col="sex", marginal="rug")
assert len(fig.data) == 2
fig = px.histogram(df, y="total_bill", facet_row="sex", marginal="rug")
assert len(fig.data) == 4
fig = px.scatter(df, x="total_bill", y="tip", facet_col="sex", marginal_x="rug")
assert len(fig.data) == 4
fig = px.scatter(
df, x="total_bill", y="tip", facet_col="day", facet_col_wrap=2, marginal_x="rug"
)
assert len(fig.data) == 8 # ignore the wrap when marginal is used
fig = px.scatter(df, x="total_bill", y="tip", facet_col="sex", marginal_y="rug")
assert len(fig.data) == 2 # ignore the marginal in the facet direction
fig = px.scatter(df, x="total_bill", y="tip", facet_row="sex", marginal_x="rug")
assert len(fig.data) == 2 # ignore the marginal in the facet direction
fig = px.scatter(df, x="total_bill", y="tip", facet_row="sex", marginal_y="rug")
assert len(fig.data) == 4
fig = px.scatter(
df, x="total_bill", y="tip", facet_row="sex", marginal_y="rug", marginal_x="rug"
)
assert len(fig.data) == 4 # ignore the marginal in the facet direction
fig = px.scatter(
df, x="total_bill", y="tip", facet_col="sex", marginal_y="rug", marginal_x="rug"
)
assert len(fig.data) == 4 # ignore the marginal in the facet direction
fig = px.scatter(
df,
x="total_bill",
y="tip",
facet_row="sex",
facet_col="sex",
marginal_y="rug",
marginal_x="rug",
)
assert len(fig.data) == 2 # ignore all marginals
def bad_facet_spacing_df(constructor_func):
NROWS = 101
NDATA = 1000
categories = [n % NROWS for n in range(NDATA)]
df = constructor_func(
{
"x": [random.random() for _ in range(NDATA)],
"y": [random.random() for _ in range(NDATA)],
"category": categories,
}
)
return df
def test_bad_facet_spacing_error(constructor):
df = bad_facet_spacing_df(constructor_func=constructor)
with pytest.raises(
ValueError, match="Use the facet_row_spacing argument to adjust this spacing."
):
px.scatter(df, x="x", y="y", facet_row="category", facet_row_spacing=0.01001)
with pytest.raises(
ValueError, match="Use the facet_col_spacing argument to adjust this spacing."
):
px.scatter(df, x="x", y="y", facet_col="category", facet_col_spacing=0.01001)
# Check error is not raised when the spacing is OK
try:
px.scatter(df, x="x", y="y", facet_row="category", facet_row_spacing=0.01)
except ValueError:
# Error shouldn't be raised, so fail if it is
assert False
try:
px.scatter(df, x="x", y="y", facet_col="category", facet_col_spacing=0.01)
except ValueError:
# Error shouldn't be raised, so fail if it is
assert False