Skip to content

Stem Plot

1
stem(Y);

example_stem_1

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

int main() {
    using namespace matplot;
    std::vector<double> x = linspace(0, 2 * pi, 50);

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

    stem(Y, "-o");

    show();
    return 0;
}

More examples

example_stem_2

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

int main() {
    using namespace matplot;
    std::vector<double> x = linspace(0, 2 * pi, 50);
    std::vector<double> y = transform(x, [](auto x) { return cos(x); });

    stem(x, y);

    show();
    return 0;
}

example_stem_3

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

int main() {
    using namespace matplot;
    std::vector<double> x = linspace(0, 2 * pi, 50);

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

    stem(x, Y);

    show();
    return 0;
}

example_stem_4

 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> x1 = linspace(0, 2 * pi, 50);
    std::vector<double> y1 = transform(x1, [](auto x) { return cos(x); });

    std::vector<double> x2 = linspace(pi, 3 * pi, 50);
    std::vector<double> y2 = transform(x2, [](auto x) { return 0.5 * sin(x); });

    stem(x1, y1);
    hold(on);
    stem(x2, y2);

    show();
    return 0;
}

example_stem_5

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

int main() {
    using namespace matplot;
    std::vector<double> x = linspace(0, 10, 20);
    std::vector<double> y = transform(x, [](auto x) { return exp(0.25 * x); });

    stem(x, y, "filled");

    show();
    return 0;
}

example_stem_6

 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, 2 * pi, 50);
    std::vector<double> y =
        transform(x, [](auto x) { return exp(x) * sin(x); });

    stem(x, y, ":dr");

    show();
    return 0;
}

example_stem_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, 2 * pi, 25);
    std::vector<double> y = transform(x, [](auto x) { return cos(2 * x); });

    auto s = stem(x, y)->line_style("-.").marker_face_color("red").marker_color(
        "green");

    show();
    return 0;
}

example_stem_8

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

int main() {
    using namespace matplot;
    std::vector<double> x = iota(0, 25);
    std::vector<double> y1 = transform(x, [](auto x) { return exp(0.1 * x); });
    std::vector<double> y2 =
        transform(x, [](auto x) { return -exp(0.05 * x); });

    tiledlayout(2, 1);

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

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

    show();
    return 0;
}

example_stem_9

 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, 2 * pi, 50);
    std::vector<double> y =
        transform(x, [](auto x) { return exp(0.3 * x) * sin(3 * x); });

    stem(x, y);
    gca()->x_axis().zero_axis(false);

    show();
    return 0;
}