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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
import numpy as np
import matplotlib as mpl
from matplotlib.colors import same_color, to_rgba
from numpy.testing import assert_array_equal, assert_array_almost_equal
from seaborn._core.plot import Plot
from seaborn._core.moves import Dodge
from seaborn._marks.line import Dash, Line, Path, Lines, Paths, Range
class TestPath:
def test_xy_data(self):
x = [1, 5, 3, np.nan, 2]
y = [1, 4, 2, 5, 3]
g = [1, 2, 1, 1, 2]
p = Plot(x=x, y=y, group=g).add(Path()).plot()
line1, line2 = p._figure.axes[0].get_lines()
assert_array_equal(line1.get_xdata(), [1, 3, np.nan])
assert_array_equal(line1.get_ydata(), [1, 2, np.nan])
assert_array_equal(line2.get_xdata(), [5, 2])
assert_array_equal(line2.get_ydata(), [4, 3])
def test_shared_colors_direct(self):
x = y = [1, 2, 3]
color = ".44"
m = Path(color=color)
p = Plot(x=x, y=y).add(m).plot()
line, = p._figure.axes[0].get_lines()
assert same_color(line.get_color(), color)
assert same_color(line.get_markeredgecolor(), color)
assert same_color(line.get_markerfacecolor(), color)
def test_separate_colors_direct(self):
x = y = [1, 2, 3]
y = [1, 2, 3]
m = Path(color=".22", edgecolor=".55", fillcolor=".77")
p = Plot(x=x, y=y).add(m).plot()
line, = p._figure.axes[0].get_lines()
assert same_color(line.get_color(), m.color)
assert same_color(line.get_markeredgecolor(), m.edgecolor)
assert same_color(line.get_markerfacecolor(), m.fillcolor)
def test_shared_colors_mapped(self):
x = y = [1, 2, 3, 4]
c = ["a", "a", "b", "b"]
m = Path()
p = Plot(x=x, y=y, color=c).add(m).plot()
ax = p._figure.axes[0]
colors = p._theme["axes.prop_cycle"].by_key()["color"]
for i, line in enumerate(ax.get_lines()):
assert same_color(line.get_color(), colors[i])
assert same_color(line.get_markeredgecolor(), colors[i])
assert same_color(line.get_markerfacecolor(), colors[i])
def test_separate_colors_mapped(self):
x = y = [1, 2, 3, 4]
c = ["a", "a", "b", "b"]
d = ["x", "y", "x", "y"]
m = Path()
p = Plot(x=x, y=y, color=c, fillcolor=d).add(m).plot()
ax = p._figure.axes[0]
colors = p._theme["axes.prop_cycle"].by_key()["color"]
for i, line in enumerate(ax.get_lines()):
assert same_color(line.get_color(), colors[i // 2])
assert same_color(line.get_markeredgecolor(), colors[i // 2])
assert same_color(line.get_markerfacecolor(), colors[i % 2])
def test_color_with_alpha(self):
x = y = [1, 2, 3]
m = Path(color=(.4, .9, .2, .5), fillcolor=(.2, .2, .3, .9))
p = Plot(x=x, y=y).add(m).plot()
line, = p._figure.axes[0].get_lines()
assert same_color(line.get_color(), m.color)
assert same_color(line.get_markeredgecolor(), m.color)
assert same_color(line.get_markerfacecolor(), m.fillcolor)
def test_color_and_alpha(self):
x = y = [1, 2, 3]
m = Path(color=(.4, .9, .2), fillcolor=(.2, .2, .3), alpha=.5)
p = Plot(x=x, y=y).add(m).plot()
line, = p._figure.axes[0].get_lines()
assert same_color(line.get_color(), to_rgba(m.color, m.alpha))
assert same_color(line.get_markeredgecolor(), to_rgba(m.color, m.alpha))
assert same_color(line.get_markerfacecolor(), to_rgba(m.fillcolor, m.alpha))
def test_other_props_direct(self):
x = y = [1, 2, 3]
m = Path(marker="s", linestyle="--", linewidth=3, pointsize=10, edgewidth=1)
p = Plot(x=x, y=y).add(m).plot()
line, = p._figure.axes[0].get_lines()
assert line.get_marker() == m.marker
assert line.get_linestyle() == m.linestyle
assert line.get_linewidth() == m.linewidth
assert line.get_markersize() == m.pointsize
assert line.get_markeredgewidth() == m.edgewidth
def test_other_props_mapped(self):
x = y = [1, 2, 3, 4]
g = ["a", "a", "b", "b"]
m = Path()
p = Plot(x=x, y=y, marker=g, linestyle=g, pointsize=g).add(m).plot()
line1, line2 = p._figure.axes[0].get_lines()
assert line1.get_marker() != line2.get_marker()
# Matplotlib bug in storing linestyle from dash pattern
# assert line1.get_linestyle() != line2.get_linestyle()
assert line1.get_markersize() != line2.get_markersize()
def test_capstyle(self):
x = y = [1, 2]
rc = {"lines.solid_capstyle": "projecting", "lines.dash_capstyle": "round"}
p = Plot(x, y).add(Path()).theme(rc).plot()
line, = p._figure.axes[0].get_lines()
assert line.get_dash_capstyle() == "projecting"
p = Plot(x, y).add(Path(linestyle="--")).theme(rc).plot()
line, = p._figure.axes[0].get_lines()
assert line.get_dash_capstyle() == "round"
p = Plot(x, y).add(Path({"solid_capstyle": "butt"})).theme(rc).plot()
line, = p._figure.axes[0].get_lines()
assert line.get_solid_capstyle() == "butt"
class TestLine:
# Most behaviors shared with Path and covered by above tests
def test_xy_data(self):
x = [1, 5, 3, np.nan, 2]
y = [1, 4, 2, 5, 3]
g = [1, 2, 1, 1, 2]
p = Plot(x=x, y=y, group=g).add(Line()).plot()
line1, line2 = p._figure.axes[0].get_lines()
assert_array_equal(line1.get_xdata(), [1, 3])
assert_array_equal(line1.get_ydata(), [1, 2])
assert_array_equal(line2.get_xdata(), [2, 5])
assert_array_equal(line2.get_ydata(), [3, 4])
class TestPaths:
def test_xy_data(self):
x = [1, 5, 3, np.nan, 2]
y = [1, 4, 2, 5, 3]
g = [1, 2, 1, 1, 2]
p = Plot(x=x, y=y, group=g).add(Paths()).plot()
lines, = p._figure.axes[0].collections
verts = lines.get_paths()[0].vertices.T
assert_array_equal(verts[0], [1, 3, np.nan])
assert_array_equal(verts[1], [1, 2, np.nan])
verts = lines.get_paths()[1].vertices.T
assert_array_equal(verts[0], [5, 2])
assert_array_equal(verts[1], [4, 3])
def test_set_properties(self):
x = y = [1, 2, 3]
m = Paths(color=".737", linewidth=1, linestyle=(3, 1))
p = Plot(x=x, y=y).add(m).plot()
lines, = p._figure.axes[0].collections
assert same_color(lines.get_color().squeeze(), m.color)
assert lines.get_linewidth().item() == m.linewidth
assert lines.get_linestyle()[0] == (0, list(m.linestyle))
def test_mapped_properties(self):
x = y = [1, 2, 3, 4]
g = ["a", "a", "b", "b"]
p = Plot(x=x, y=y, color=g, linewidth=g, linestyle=g).add(Paths()).plot()
lines, = p._figure.axes[0].collections
assert not np.array_equal(lines.get_colors()[0], lines.get_colors()[1])
assert lines.get_linewidths()[0] != lines.get_linewidth()[1]
assert lines.get_linestyle()[0] != lines.get_linestyle()[1]
def test_color_with_alpha(self):
x = y = [1, 2, 3]
m = Paths(color=(.2, .6, .9, .5))
p = Plot(x=x, y=y).add(m).plot()
lines, = p._figure.axes[0].collections
assert same_color(lines.get_colors().squeeze(), m.color)
def test_color_and_alpha(self):
x = y = [1, 2, 3]
m = Paths(color=(.2, .6, .9), alpha=.5)
p = Plot(x=x, y=y).add(m).plot()
lines, = p._figure.axes[0].collections
assert same_color(lines.get_colors().squeeze(), to_rgba(m.color, m.alpha))
def test_capstyle(self):
x = y = [1, 2]
rc = {"lines.solid_capstyle": "projecting"}
with mpl.rc_context(rc):
p = Plot(x, y).add(Paths()).plot()
lines = p._figure.axes[0].collections[0]
assert lines.get_capstyle() == "projecting"
p = Plot(x, y).add(Paths(linestyle="--")).plot()
lines = p._figure.axes[0].collections[0]
assert lines.get_capstyle() == "projecting"
p = Plot(x, y).add(Paths({"capstyle": "butt"})).plot()
lines = p._figure.axes[0].collections[0]
assert lines.get_capstyle() == "butt"
class TestLines:
def test_xy_data(self):
x = [1, 5, 3, np.nan, 2]
y = [1, 4, 2, 5, 3]
g = [1, 2, 1, 1, 2]
p = Plot(x=x, y=y, group=g).add(Lines()).plot()
lines, = p._figure.axes[0].collections
verts = lines.get_paths()[0].vertices.T
assert_array_equal(verts[0], [1, 3])
assert_array_equal(verts[1], [1, 2])
verts = lines.get_paths()[1].vertices.T
assert_array_equal(verts[0], [2, 5])
assert_array_equal(verts[1], [3, 4])
def test_single_orient_value(self):
x = [1, 1, 1]
y = [1, 2, 3]
p = Plot(x, y).add(Lines()).plot()
lines, = p._figure.axes[0].collections
verts = lines.get_paths()[0].vertices.T
assert_array_equal(verts[0], x)
assert_array_equal(verts[1], y)
class TestRange:
def test_xy_data(self):
x = [1, 2]
ymin = [1, 4]
ymax = [2, 3]
p = Plot(x=x, ymin=ymin, ymax=ymax).add(Range()).plot()
lines, = p._figure.axes[0].collections
for i, path in enumerate(lines.get_paths()):
verts = path.vertices.T
assert_array_equal(verts[0], [x[i], x[i]])
assert_array_equal(verts[1], [ymin[i], ymax[i]])
def test_auto_range(self):
x = [1, 1, 2, 2, 2]
y = [1, 2, 3, 4, 5]
p = Plot(x=x, y=y).add(Range()).plot()
lines, = p._figure.axes[0].collections
paths = lines.get_paths()
assert_array_equal(paths[0].vertices, [(1, 1), (1, 2)])
assert_array_equal(paths[1].vertices, [(2, 3), (2, 5)])
def test_mapped_color(self):
x = [1, 2, 1, 2]
ymin = [1, 4, 3, 2]
ymax = [2, 3, 1, 4]
group = ["a", "a", "b", "b"]
p = Plot(x=x, ymin=ymin, ymax=ymax, color=group).add(Range()).plot()
lines, = p._figure.axes[0].collections
colors = p._theme["axes.prop_cycle"].by_key()["color"]
for i, path in enumerate(lines.get_paths()):
verts = path.vertices.T
assert_array_equal(verts[0], [x[i], x[i]])
assert_array_equal(verts[1], [ymin[i], ymax[i]])
assert same_color(lines.get_colors()[i], colors[i // 2])
def test_direct_properties(self):
x = [1, 2]
ymin = [1, 4]
ymax = [2, 3]
m = Range(color=".654", linewidth=4)
p = Plot(x=x, ymin=ymin, ymax=ymax).add(m).plot()
lines, = p._figure.axes[0].collections
for i, path in enumerate(lines.get_paths()):
assert same_color(lines.get_colors()[i], m.color)
assert lines.get_linewidths()[i] == m.linewidth
class TestDash:
def test_xy_data(self):
x = [0, 0, 1, 2]
y = [1, 2, 3, 4]
p = Plot(x=x, y=y).add(Dash()).plot()
lines, = p._figure.axes[0].collections
for i, path in enumerate(lines.get_paths()):
verts = path.vertices.T
assert_array_almost_equal(verts[0], [x[i] - .4, x[i] + .4])
assert_array_equal(verts[1], [y[i], y[i]])
def test_xy_data_grouped(self):
x = [0, 0, 1, 2]
y = [1, 2, 3, 4]
color = ["a", "b", "a", "b"]
p = Plot(x=x, y=y, color=color).add(Dash()).plot()
lines, = p._figure.axes[0].collections
idx = [0, 2, 1, 3]
for i, path in zip(idx, lines.get_paths()):
verts = path.vertices.T
assert_array_almost_equal(verts[0], [x[i] - .4, x[i] + .4])
assert_array_equal(verts[1], [y[i], y[i]])
def test_set_properties(self):
x = [0, 0, 1, 2]
y = [1, 2, 3, 4]
m = Dash(color=".8", linewidth=4)
p = Plot(x=x, y=y).add(m).plot()
lines, = p._figure.axes[0].collections
for color in lines.get_color():
assert same_color(color, m.color)
for linewidth in lines.get_linewidth():
assert linewidth == m.linewidth
def test_mapped_properties(self):
x = [0, 1]
y = [1, 2]
color = ["a", "b"]
linewidth = [1, 2]
p = Plot(x=x, y=y, color=color, linewidth=linewidth).add(Dash()).plot()
lines, = p._figure.axes[0].collections
palette = p._theme["axes.prop_cycle"].by_key()["color"]
for color, line_color in zip(palette, lines.get_color()):
assert same_color(color, line_color)
linewidths = lines.get_linewidths()
assert linewidths[1] > linewidths[0]
def test_width(self):
x = [0, 0, 1, 2]
y = [1, 2, 3, 4]
p = Plot(x=x, y=y).add(Dash(width=.4)).plot()
lines, = p._figure.axes[0].collections
for i, path in enumerate(lines.get_paths()):
verts = path.vertices.T
assert_array_almost_equal(verts[0], [x[i] - .2, x[i] + .2])
assert_array_equal(verts[1], [y[i], y[i]])
def test_dodge(self):
x = [0, 1]
y = [1, 2]
group = ["a", "b"]
p = Plot(x=x, y=y, group=group).add(Dash(), Dodge()).plot()
lines, = p._figure.axes[0].collections
paths = lines.get_paths()
v0 = paths[0].vertices.T
assert_array_almost_equal(v0[0], [-.4, 0])
assert_array_equal(v0[1], [y[0], y[0]])
v1 = paths[1].vertices.T
assert_array_almost_equal(v1[0], [1, 1.4])
assert_array_equal(v1[1], [y[1], y[1]])
|