Skip to content

Stairs

1
stairs(x,y);

The stair object renders the line with stairs between data points to denote discrete data.

example_stairs_2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;

    auto X = linspace(0, 4 * pi, 40);
    std::vector<std::vector<double>> Y(2);
    Y[0] = transform(X, [](double x) { return 0.5 * cos(x); });
    Y[1] = transform(X, [](double x) { return 2 * cos(x); });

    figure();
    stairs(Y);

    show();
    return 0;
}

More examples

example_stairs_3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;

    auto X = linspace(0, 4 * pi, 40);
    auto Y = transform(X, [](double x) { return sin(x); });

    figure();
    stairs(X, Y);

    show();
    return 0;
}

example_stairs_4

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;

    auto X = linspace(0, 4 * pi, 50);
    std::vector<std::vector<double>> Y(2);
    Y[0] = transform(X, [](double x) { return 0.5 * cos(x); });
    Y[1] = transform(X, [](double x) { return 2 * cos(x); });

    figure();
    stairs(X, Y);

    show();
    return 0;
}

example_stairs_5

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;

    std::vector<double> x1 = linspace(0, 2 * pi);
    std::vector<double> x2 = linspace(0, pi);
    std::vector<std::vector<double>> X = {x1, x2};

    std::vector<std::vector<double>> Y(2);
    Y[0] = transform(x1, [](double x) { return sin(5 * x); });
    Y[1] = transform(x2, [](double x) { return exp(x) * sin(5 * x); });

    figure();
    stairs(X, Y);

    show();
    return 0;
}

example_stairs_6

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;

    std::vector<double> X = linspace(0, 4 * pi, 20);
    std::vector<double> Y = transform(X, [](double x) { return sin(x); });

    figure();
    stairs(Y, "-.or");

    show();
    return 0;
}

example_stairs_7

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;

    std::vector<double> X = linspace(0, 4 * pi, 20);
    std::vector<double> Y = transform(X, [](double x) { return sin(x); });

    auto s = stairs(Y)->line_width(2).marker("d").marker_face_color("c");

    show();
    return 0;
}

example_stairs_8

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;

    std::vector<double> x = linspace(0, 2 * pi);
    std::vector<double> y1 = transform(x, [](double x) { return 5 * sin(x); });
    std::vector<double> y2 = transform(x, [](double x) { return sin(5 * x); });
    tiledlayout(2, 1);

    auto ax1 = nexttile();
    stairs(ax1, x, y1);

    auto ax2 = nexttile();
    stairs(ax2, x, y2);

    show();
    return 0;
}

example_stairs_9

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;

    std::vector<double> x = linspace(0, 1, 30);
    std::vector<std::vector<double>> Y(2);
    Y[0] = transform(x, [](double x) { return cos(10 * x); });
    Y[1] = transform(x, [](double x) { return exp(x) * sin(10 * x); });

    auto h = stairs(x, Y);
    h[0]->marker(line_spec::marker_style::circle).marker_size(4);
    h[1]->marker(line_spec::marker_style::circle).marker_face_color("m");

    show();
    return 0;
}

example_stairs_10

 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
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;

    std::vector<double> x = {1, 3, 5, 7, 10};
    std::vector<double> y = {2, 5, 6, 7, 11};
    auto stair_handles = stairs(x, y, x, y, x, y, x, y);

    stair_handles[0]->stair_style(stair::stair_style::fill);

    stair_handles[1]
        ->stair_style(stair::stair_style::trace_x_first)
        .line_width(4);

    stair_handles[2]
        ->stair_style(stair::stair_style::trace_y_first)
        .line_width(2);

    stair_handles[3]
        ->stair_style(stair::stair_style::histogram)
        .marker_style(line_spec::marker_style::circle)
        .line_width(1)
        .marker_color("m")
        .marker_face(true)
        .marker_size(10);

    axis({0, 12, 0, 13});

    show();
    return 0;
}