Skip to content

Stem Plot 3D

1
stem3(z);

example_stem3_1

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

int main() {
    using namespace matplot;
    std::vector<double> x = linspace(-pi / 2, pi / 2, 40);
    auto z = transform(x, [](auto x) { return cos(x); });
    stem3(z);

    show();
    return 0;
}

More examples

example_stem3_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(40, 1.);
    std::vector<double> y = linspace(-pi / 2, pi / 2, 40);
    auto z = transform(y, [](auto x) { return cos(x); });
    stem3(x, y, z);

    show();
    return 0;
}

example_stem3_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(-pi / 2, pi / 2, 40);

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

    stem3(Z);

    show();
    return 0;
}

example_stem3_4

 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(-5, +5, 60);
    std::vector<double> y = transform(x, [](double x) { return cos(x); });
    std::vector<double> z = transform(x, [](double x) { return pow(x, 2); });

    stem3(x, y, z);
    view(-8, 30);

    show();
    return 0;
}

example_stem3_5

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

int main() {
    using namespace matplot;
    std::vector<double> grid_ticks = iota(0, .1, 1);
    std::vector<double> x;
    std::vector<double> y;
    std::vector<double> z;
    for (const auto &x_i : grid_ticks) {
        for (const auto &y_i : grid_ticks) {
            x.emplace_back(x_i);
            y.emplace_back(y_i);
            z.emplace_back(exp(x_i + y_i));
        }
    }

    stem3(x, y, z);

    show();
    return 0;
}

example_stem3_6

 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(-pi, pi, 40);
    std::vector<double> z = transform(x, [](double x) { return cos(x); });

    stem3(z, "filled");

    show();
    return 0;
}

example_stem3_7

 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(-pi, pi, 40);
    std::vector<double> z = transform(x, [](double x) { return cos(x); });

    stem3(z, "--*m");

    show();
    return 0;
}

example_stem3_8

 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> theta = linspace(0, 2 * pi);
    std::vector<double> x = transform(theta, [](double t) { return cos(t); });
    std::vector<double> y = transform(theta, [](double t) { return sin(t); });
    std::vector<double> z = theta;

    stem3(x, y, z, ":*m");

    show();
    return 0;
}

example_stem3_9

 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(-pi, pi, 40);
    std::vector<double> z = transform(x, [](double x) { return cos(x); });

    stem3(z)->marker("s").marker_color("m").marker_face_color("g");

    show();
    return 0;
}

example_stem3_10

 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 = linspace(-2, +2, 50);
    std::vector<double> y = transform(x, [](double x) { return pow(x, 3); });
    std::vector<double> z = transform(x, [](double x) { return exp(x); });

    tiledlayout(2, 1);

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

    auto ax2 = nexttile();
    stem3(ax2, x, y, z);

    show();
    return 0;
}

example_stem3_11

 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;

    std::vector<double> x = linspace(0, +2);
    std::vector<double> y = transform(x, [](double x) { return pow(x, 3); });
    std::vector<double> z =
        transform(x, y, [](double x, double y) { return exp(x) * cos(y); });

    stem3(x, y, z, "filled")->color("m").marker_face_color("y");
    view(-10, 35);

    show();
    return 0;
}