Skip to content

Line Plot 3D

1
plot3(x,y);

example_plot3_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> t = iota(0, pi / 50, 10 * pi);
    std::vector<double> st = transform(t, [](auto x) { return sin(x); });
    std::vector<double> ct = transform(t, [](auto x) { return cos(x); });
    auto l = plot3(st, ct, t);
    show();
    return 0;
}

Tip

With method chaining:

1
plot3(x,y)->line_width(2).color("red");

More examples

example_plot3_2

 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> t = iota(0, pi / 500, pi);
    std::vector<double> xt1 =
        transform(t, [](auto x) { return sin(x) * cos(10 * x); });
    std::vector<double> yt1 =
        transform(t, [](auto x) { return sin(x) * sin(10 * x); });
    std::vector<double> zt1 = transform(t, [](auto x) { return cos(x); });
    std::vector<double> xt2 =
        transform(t, [](auto x) { return sin(x) * cos(12 * x); });
    std::vector<double> yt2 =
        transform(t, [](auto x) { return sin(x) * sin(12 * x); });
    std::vector<double> zt2 = transform(t, [](auto x) { return cos(x); });
    plot3(xt1, yt1, zt1, xt2, yt2, zt2);
    show();
    return 0;
}

example_plot3_3

 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> t = iota(0, pi / 500, pi);
    std::vector<std::vector<double>> X(3);
    std::vector<std::vector<double>> Y(3);

    X[0] = transform(t, [](auto x) { return sin(x) * cos(10 * x); });
    X[1] = transform(t, [](auto x) { return sin(x) * cos(12 * x); });
    X[2] = transform(t, [](auto x) { return sin(x) * cos(20 * x); });
    Y[0] = transform(t, [](auto x) { return sin(x) * sin(10 * x); });
    Y[1] = transform(t, [](auto x) { return sin(x) * sin(12 * x); });
    Y[2] = transform(t, [](auto x) { return sin(x) * sin(20 * x); });
    std::vector<double> z = transform(t, [](auto x) { return cos(x); });
    plot3(X, Y, z);

    show();
    return 0;
}

example_plot3_4

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

int main() {
    using namespace matplot;
    auto t = iota(0, pi / 500, 40 * pi);
    auto xt =
        transform(t, [](auto t) { return (3. + cos(sqrt(32.) * t)) * cos(t); });
    auto yt = transform(t, [](auto t) { return sin(sqrt(32.) * t); });
    auto zt =
        transform(t, [](auto t) { return (3. + cos(sqrt(32.) * t)) * sin(t); });
    plot3(xt, yt, zt);
    axis(equal);
    xlabel("x(t)");
    ylabel("y(t)");
    zlabel("z(t)");

    auto ax = gca();
    float da = ax->azimuth();
    for (size_t i = 0; i <= 360; ++i) {
        ax->azimuth(da + 2 * i);
        std::this_thread::sleep_for(std::chrono::milliseconds(50));
    }

    show();
    return 0;
}

example_plot3_5

 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 t = iota(0, pi / 20, 10 * pi);
    auto xt = transform(t, [](auto t) { return sin(t); });
    auto yt = transform(t, [](auto t) { return cos(t); });
    auto l = plot3(xt, yt, t, "-ob");
    l->marker_size(10);
    l->marker_face_color("#D9FFFF");
    xlabel("x(t)");
    ylabel("y(t)");
    zlabel("t");
    show();
    return 0;
}

example_plot3_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;
    auto t = linspace(-10, 10, 1000);
    auto xt = transform(t, [](auto t) { return exp(-t / 10) * sin(5 * t); });
    auto yt = transform(t, [](auto t) { return exp(-t / 10) * cos(5 * t); });
    auto p = plot3(xt, yt, t);
    p->line_width(3);
    show();
    return 0;
}

example_plot3_8

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

int main() {
    using namespace matplot;
    tiledlayout(1, 2);

    auto ax1 = nexttile();
    auto t = iota(0, pi / 20, 10 * pi);
    auto xt1 = transform(t, [](auto t) { return sin(t); });
    auto yt1 = transform(t, [](auto t) { return cos(t); });
    plot3(ax1, xt1, yt1, t);
    title(ax1, "Helix with 5 Turns");

    auto ax2 = nexttile();
    t = iota(0, pi / 40, 10 * pi);
    auto xt2 = transform(t, [](auto t) { return sin(2 * t); });
    auto yt2 = transform(t, [](auto t) { return cos(2 * t); });
    plot3(ax2, xt2, yt2, t);
    ax2->box(false);
    title(ax2, "Helix with 10 Turns");

    show();
    return 0;
}

example_plot3_9

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

int main() {
    using namespace matplot;

    std::default_random_engine g(0);
    std::uniform_real_distribution<double> d;

    std::vector<double> x(10);
    std::generate(x.begin(), x.end(), [&]() { return d(g); });

    std::vector<double> y(10);
    std::generate(y.begin(), y.end(), [&]() { return d(g); });

    std::uniform_real_distribution<double> d2(0, 90);
    std::vector<double> z(10);
    std::generate(z.begin(), z.end(), [&]() { return d2(g); });

    auto p = plot3(x, y, z, "o");
    p->parent()->x_axis().tick_label_format("s");
    xlabel("X");
    ylabel("Y");
    zlabel("Duration");
    grid(on);

    show();
    return 0;
}

example_plot3_10

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

int main() {
    using namespace matplot;

    auto t = iota(0, pi / 500, pi);
    auto xt = transform(t, [](auto t) { return sin(t) * cos(10 * t); });
    auto yt = transform(t, [](auto t) { return sin(t) * sin(10 * t); });
    auto zt = transform(t, [](auto t) { return cos(t); });
    auto p = plot3(xt, yt, zt, "-o");
    p->marker_indices({200});

    show();
    return 0;
}