def test_auto_edgewidth(self): x0 = np.arange(10) x1 = np.arange(1000) p0 = Plot(x0, x0).add(Bars()).plot() p1 = Plot(x1, x1).add(Bars()).plot() lw0 = p0._figure.axes[0].collections[0].get_linewidths() lw1 = p1._figure.axes[0].collections[0].get_linewidths() assert (lw0 > lw1).all()
def test_direct_parameters(self): x, y = [1, 2, 3], [1, 2, 1] mark = Area( color="C2", alpha=.3, edgecolor="k", edgealpha=.8, edgewidth=2, edgestyle=(0, (2, 1)), ) p = Plot(x=x, y=y).add(mark).plot() ax = p._figure.axes[0] poly, = ax.collections fc = poly.get_facecolor() assert_array_equal(fc, to_rgba_array(mark.color, mark.alpha)) ec = poly.get_edgecolor() assert_array_equal(ec, to_rgba_array(mark.edgecolor, mark.edgealpha)) lw = poly.get_linewidth() assert_array_equal(lw, mark.edgewidth) ls = poly.get_linestyle() dash_on, dash_off = mark.edgestyle[1] expected = [(0, [mark.edgewidth * dash_on, mark.edgewidth * dash_off])] assert ls == expected
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_mapped(self): x, y = [1, 2, 3, 2, 3, 4], [1, 2, 1, 1, 3, 2] g = ["a", "a", "a", "b", "b", "b"] p = Plot(x=x, y=y, color=g, edgewidth=g).add(Area()).plot() ax = p._figure.axes[0] polys, = ax.collections paths = polys.get_paths() expected_x = [1, 2, 3, 3, 2, 1, 1], [2, 3, 4, 4, 3, 2, 2] expected_y = [0, 0, 0, 1, 2, 1, 0], [0, 0, 0, 2, 3, 1, 0] for i, path in enumerate(paths): verts = path.vertices.T assert_array_equal(verts[0], expected_x[i]) assert_array_equal(verts[1], expected_y[i]) fc = polys.get_facecolor() assert_array_equal(fc, to_rgba_array(["C0", "C1"], .2)) ec = polys.get_edgecolor() assert_array_equal(ec, to_rgba_array(["C0", "C1"], 1)) lw = polys.get_linewidths() assert lw[0] > lw[1]
def test_unfilled(self): x, y = [1, 2, 3], [1, 2, 1] p = Plot(x=x, y=y).add(Area(fill=False)).plot() ax = p._figure.axes[0] poly = ax.patches[0] assert poly.get_facecolor() == to_rgba("C0", 0)
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_unfilled(self, x, y): p = Plot(x, y).add(Bars(fill=False, edgecolor="C4")).plot() ax = p._figure.axes[0] fcs = ax.collections[0].get_facecolors() ecs = ax.collections[0].get_edgecolors() assert_array_equal(fcs, to_rgba_array(["C0"] * len(x), 0)) assert_array_equal(ecs, to_rgba_array(["C4"] * len(x), 1))
def test_mapped_color_direct_alpha(self, x, y, color): alpha = .5 p = Plot(x, y, color=color).add(Bars(alpha=alpha)).plot() ax = p._figure.axes[0] fcs = ax.collections[0].get_facecolors() expected = to_rgba_array(["C0", "C1", "C2", "C0", "C2"], alpha) assert_array_equal(fcs, expected)
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"
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_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_shared_colors_direct(self): x = y = [1, 2, 3] m = Path(color="r") p = Plot(x=x, y=y).add(m).plot() line, = p._figure.axes[0].get_lines() assert same_color(line.get_color(), "r") assert same_color(line.get_markeredgecolor(), "r") assert same_color(line.get_markerfacecolor(), "r")
def test_width(self, x, y): p = Plot(x, y).add(Bars(width=.4)).plot() ax = p._figure.axes[0] paths = ax.collections[0].get_paths() for i, path in enumerate(paths): verts = path.vertices assert verts[0, 0] == pytest.approx(x[i] - .2) assert verts[1, 0] == pytest.approx(x[i] + .2)
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])
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 self.check_offsets(points, x, y) self.check_colors("face", points, ["C0"] * 3, 1) self.check_colors("edge", points, ["C0"] * 3, 1)
def test_separate_colors_direct(self): x = y = [1, 2, 3] y = [1, 2, 3] m = Path(color="r", edgecolor="g", fillcolor="b") 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_props_mapped(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_props_direct(self): x = y = [1, 2, 3] m = Paths(color="r", 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_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])
def test_stroke(self): x = [1, 2, 3] y = [4, 5, 2] s = 3 p = Plot(x=x, y=y).add(Scatter(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_color_direct(self): x = [1, 2, 3] y = [4, 5, 2] p = Plot(x=x, y=y).add(Scatter(color="g")).plot() ax = p._figure.axes[0] points, = ax.collections self.check_offsets(points, x, y) self.check_colors("face", points, ["g"] * 3, .2) self.check_colors("edge", points, ["g"] * 3, 1)
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"
def test_mapped_properties(self): x = ["a", "b"] y = [1, 2] mark = Bar(alpha=.2) p = Plot(x, y, color=x, edgewidth=y).add(mark).plot() ax = p._figure.axes[0] for i, bar in enumerate(ax.patches): assert bar.get_facecolor() == to_rgba(f"C{i}", mark.alpha) assert bar.get_edgecolor() == to_rgba(f"C{i}", 1) assert ax.patches[0].get_linewidth() < ax.patches[1].get_linewidth()
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(Scatter(fill=False)).plot() ax = p._figure.axes[0] points, = ax.collections 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_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_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] for i, line in enumerate(ax.get_lines()): assert same_color(line.get_color(), f"C{i}") assert same_color(line.get_markeredgecolor(), f"C{i}") assert same_color(line.get_markerfacecolor(), f"C{i}")
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_ribbon(self): x, ymin, ymax = [1, 2, 4], [2, 1, 4], [3, 3, 5] p = Plot(x=x, ymin=ymin, ymax=ymax).add(Ribbon()).plot() ax = p._figure.axes[0] verts = ax.patches[0].get_path().vertices.T expected_x = [1, 2, 4, 4, 2, 1, 1] assert_array_equal(verts[0], expected_x) expected_y = [2, 1, 4, 5, 3, 3, 2] assert_array_equal(verts[1], expected_y)
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])
def test_positions_horizontal(self, x, y): p = Plot(x=y, y=x).add(Bars(), orient="h").plot() ax = p._figure.axes[0] paths = ax.collections[0].get_paths() assert len(paths) == len(x) for i, path in enumerate(paths): verts = path.vertices assert verts[0, 1] == pytest.approx(x[i] - .5) assert verts[3, 1] == pytest.approx(x[i] + .5) assert verts[0, 0] == 0 assert verts[1, 0] == y[i]