File: test_dot.py

package info (click to toggle)
seaborn 0.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,148 kB
  • sloc: python: 36,560; makefile: 183; javascript: 45; sh: 15
file content (178 lines) | stat: -rw-r--r-- 5,426 bytes parent folder | download | duplicates (2)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from matplotlib.colors import to_rgba, to_rgba_array

import pytest
from numpy.testing import assert_array_equal

from seaborn.palettes import color_palette
from seaborn._core.plot import Plot
from seaborn._marks.dot import Dot, Dots


@pytest.fixture(autouse=True)
def default_palette():
    with color_palette("deep"):
        yield


class DotBase:

    def check_offsets(self, points, x, y):

        offsets = points.get_offsets().T
        assert_array_equal(offsets[0], x)
        assert_array_equal(offsets[1], y)

    def check_colors(self, part, points, colors, alpha=None):

        rgba = to_rgba_array(colors, alpha)

        getter = getattr(points, f"get_{part}colors")
        assert_array_equal(getter(), rgba)


class TestDot(DotBase):

    def test_simple(self):

        x = [1, 2, 3]
        y = [4, 5, 2]
        p = Plot(x=x, y=y).add(Dot()).plot()
        ax = p._figure.axes[0]
        points, = ax.collections
        C0, *_ = p._theme["axes.prop_cycle"].by_key()["color"]
        self.check_offsets(points, x, y)
        self.check_colors("face", points, [C0] * 3, 1)
        self.check_colors("edge", points, [C0] * 3, 1)

    def test_filled_unfilled_mix(self):

        x = [1, 2]
        y = [4, 5]
        marker = ["a", "b"]
        shapes = ["o", "x"]

        mark = Dot(edgecolor="w", stroke=2, edgewidth=1)
        p = Plot(x=x, y=y).add(mark, marker=marker).scale(marker=shapes).plot()
        ax = p._figure.axes[0]
        points, = ax.collections
        C0, *_ = p._theme["axes.prop_cycle"].by_key()["color"]
        self.check_offsets(points, x, y)
        self.check_colors("face", points, [C0, to_rgba(C0, 0)], None)
        self.check_colors("edge", points, ["w", C0], 1)

        expected = [mark.edgewidth, mark.stroke]
        assert_array_equal(points.get_linewidths(), expected)

    def test_missing_coordinate_data(self):

        x = [1, float("nan"), 3]
        y = [5, 3, 4]

        p = Plot(x=x, y=y).add(Dot()).plot()
        ax = p._figure.axes[0]
        points, = ax.collections
        self.check_offsets(points, [1, 3], [5, 4])

    @pytest.mark.parametrize("prop", ["color", "fill", "marker", "pointsize"])
    def test_missing_semantic_data(self, prop):

        x = [1, 2, 3]
        y = [5, 3, 4]
        z = ["a", float("nan"), "b"]

        p = Plot(x=x, y=y, **{prop: z}).add(Dot()).plot()
        ax = p._figure.axes[0]
        points, = ax.collections
        self.check_offsets(points, [1, 3], [5, 4])


class TestDots(DotBase):

    def test_simple(self):

        x = [1, 2, 3]
        y = [4, 5, 2]
        p = Plot(x=x, y=y).add(Dots()).plot()
        ax = p._figure.axes[0]
        points, = ax.collections
        C0, *_ = p._theme["axes.prop_cycle"].by_key()["color"]
        self.check_offsets(points, x, y)
        self.check_colors("face", points, [C0] * 3, .2)
        self.check_colors("edge", points, [C0] * 3, 1)

    def test_set_color(self):

        x = [1, 2, 3]
        y = [4, 5, 2]
        m = Dots(color=".25")
        p = Plot(x=x, y=y).add(m).plot()
        ax = p._figure.axes[0]
        points, = ax.collections
        self.check_offsets(points, x, y)
        self.check_colors("face", points, [m.color] * 3, .2)
        self.check_colors("edge", points, [m.color] * 3, 1)

    def test_map_color(self):

        x = [1, 2, 3]
        y = [4, 5, 2]
        c = ["a", "b", "a"]
        p = Plot(x=x, y=y, color=c).add(Dots()).plot()
        ax = p._figure.axes[0]
        points, = ax.collections
        C0, C1, *_ = p._theme["axes.prop_cycle"].by_key()["color"]
        self.check_offsets(points, x, y)
        self.check_colors("face", points, [C0, C1, C0], .2)
        self.check_colors("edge", points, [C0, C1, C0], 1)

    def test_fill(self):

        x = [1, 2, 3]
        y = [4, 5, 2]
        c = ["a", "b", "a"]
        p = Plot(x=x, y=y, color=c).add(Dots(fill=False)).plot()
        ax = p._figure.axes[0]
        points, = ax.collections
        C0, C1, *_ = p._theme["axes.prop_cycle"].by_key()["color"]
        self.check_offsets(points, x, y)
        self.check_colors("face", points, [C0, C1, C0], 0)
        self.check_colors("edge", points, [C0, C1, C0], 1)

    def test_pointsize(self):

        x = [1, 2, 3]
        y = [4, 5, 2]
        s = 3
        p = Plot(x=x, y=y).add(Dots(pointsize=s)).plot()
        ax = p._figure.axes[0]
        points, = ax.collections
        self.check_offsets(points, x, y)
        assert_array_equal(points.get_sizes(), [s ** 2] * 3)

    def test_stroke(self):

        x = [1, 2, 3]
        y = [4, 5, 2]
        s = 3
        p = Plot(x=x, y=y).add(Dots(stroke=s)).plot()
        ax = p._figure.axes[0]
        points, = ax.collections
        self.check_offsets(points, x, y)
        assert_array_equal(points.get_linewidths(), [s] * 3)

    def test_filled_unfilled_mix(self):

        x = [1, 2]
        y = [4, 5]
        marker = ["a", "b"]
        shapes = ["o", "x"]

        mark = Dots(stroke=2)
        p = Plot(x=x, y=y).add(mark, marker=marker).scale(marker=shapes).plot()
        ax = p._figure.axes[0]
        points, = ax.collections
        C0, C1, *_ = p._theme["axes.prop_cycle"].by_key()["color"]
        self.check_offsets(points, x, y)
        self.check_colors("face", points, [to_rgba(C0, .2), to_rgba(C0, 0)], None)
        self.check_colors("edge", points, [C0, C0], 1)
        assert_array_equal(points.get_linewidths(), [mark.stroke] * 2)