Skip to content

Scatter Plot 3D

1
scatter(x,y,z);

example_scatter3_1

 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <matplot/matplot.h>
#include <random>
#include <tuple>

std::tuple<std::vector<double>, std::vector<double>, std::vector<double>>
generate_data();

int main() {
    using namespace matplot;

    auto [x, y, z] = generate_data();
    scatter3(x, y, z);

    show();
    return 0;
}

std::tuple<std::vector<double>, std::vector<double>, std::vector<double>>
generate_data() {
    using namespace matplot;
    int n = 16;
    auto r = iota(static_cast<double>(-n), 2., static_cast<double>(n));
    auto theta = transform(r, [n](double x) { return x / n * pi; });
    auto phi = transform(r, [n](double x) { return x / n * pi / 2.; });
    auto sinphi = transform(phi, [](double x) { return sin(x); });
    auto cosphi = transform(phi, [](double x) { return cos(x); });
    cosphi.front() = 0;
    cosphi.back() = 0;
    auto sintheta = transform(theta, [](double x) { return sin(x); });
    sintheta.front() = 0;
    sintheta.back() = 0;
    auto costheta = transform(theta, [](double x) { return cos(x); });
    std::vector<std::vector<double>> X(17, std::vector<double>(17, 0.));
    std::vector<std::vector<double>> Y(17, std::vector<double>(17, 0.));
    std::vector<std::vector<double>> Z(17, std::vector<double>(17, 0.));
    for (int i = 0; i < n + 1; ++i) {
        for (int j = 0; j < n + 1; ++j) {
            X[i][j] = cosphi[i] * costheta[j];
            Y[i][j] = cosphi[i] * sintheta[j];
            Z[i][j] = sinphi[i];
        }
    }
    auto X1d = reshape(X);
    auto Y1d = reshape(Y);
    auto Z1d = reshape(Z);
    std::vector<double> x =
        concat(concat(transform(X1d, [](double x) { return x * 0.5; }),
                      transform(X1d, [](double x) { return x * 0.75; })),
               X1d);
    std::vector<double> y =
        concat(concat(transform(Y1d, [](double y) { return y * 0.5; }),
                      transform(Y1d, [](double y) { return y * 0.75; })),
               Y1d);
    std::vector<double> z =
        concat(concat(transform(Z1d, [](double z) { return z * 0.5; }),
                      transform(Z1d, [](double z) { return z * 0.75; })),
               Z1d);

    return std::make_tuple(x, y, z);
}

More examples

example_scatter3_2

 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <matplot/matplot.h>
#include <random>
#include <tuple>

std::tuple<std::vector<double>, std::vector<double>, std::vector<double>>
generate_data();

int main() {
    using namespace matplot;

    auto [x, y, z] = generate_data();
    std::vector<double> sizes(x.size() / 3, 16);
    std::fill_n(std::back_inserter(sizes), x.size() / 3, 8);
    std::fill_n(std::back_inserter(sizes), x.size() / 3, 2);

    scatter3(x, y, z, sizes);
    view(40, 35);

    show();
    return 0;
}

std::tuple<std::vector<double>, std::vector<double>, std::vector<double>>
generate_data() {
    using namespace matplot;
    int n = 16;
    auto r = iota(-n, 2., n);
    auto theta = transform(r, [n](double x) { return x / n * pi; });
    auto phi = transform(r, [n](double x) { return x / n * pi / 2.; });
    auto sinphi = transform(phi, [](double x) { return sin(x); });
    auto cosphi = transform(phi, [](double x) { return cos(x); });
    cosphi.front() = 0;
    cosphi.back() = 0;
    auto sintheta = transform(theta, [](double x) { return sin(x); });
    sintheta.front() = 0;
    sintheta.back() = 0;
    auto costheta = transform(theta, [](double x) { return cos(x); });
    std::vector<std::vector<double>> X(17, std::vector<double>(17, 0.));
    std::vector<std::vector<double>> Y(17, std::vector<double>(17, 0.));
    std::vector<std::vector<double>> Z(17, std::vector<double>(17, 0.));
    for (int i = 0; i < n + 1; ++i) {
        for (int j = 0; j < n + 1; ++j) {
            X[i][j] = cosphi[i] * costheta[j];
            Y[i][j] = cosphi[i] * sintheta[j];
            Z[i][j] = sinphi[i];
        }
    }
    auto X1d = reshape(X);
    auto Y1d = reshape(Y);
    auto Z1d = reshape(Z);
    std::vector<double> x =
        concat(concat(transform(X1d, [](double x) { return x * 0.5; }),
                      transform(X1d, [](double x) { return x * 0.75; })),
               X1d);
    std::vector<double> y =
        concat(concat(transform(Y1d, [](double y) { return y * 0.5; }),
                      transform(Y1d, [](double y) { return y * 0.75; })),
               Y1d);
    std::vector<double> z =
        concat(concat(transform(Z1d, [](double z) { return z * 0.5; }),
                      transform(Z1d, [](double z) { return z * 0.75; })),
               Z1d);

    return std::make_tuple(x, y, z);
}

example_scatter3_3

 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <matplot/matplot.h>
#include <random>
#include <tuple>

std::tuple<std::vector<double>, std::vector<double>, std::vector<double>>
generate_data();

int main() {
    using namespace matplot;

    auto [x, y, z] = generate_data();

    std::vector<double> sizes(x.size() / 3, 16);
    std::fill_n(std::back_inserter(sizes), x.size() / 3, 8);
    std::fill_n(std::back_inserter(sizes), x.size() / 3, 2);

    std::vector<double> colors(x.size() / 3, 1);
    std::fill_n(std::back_inserter(colors), x.size() / 3, 2);
    std::fill_n(std::back_inserter(colors), x.size() / 3, 3);

    scatter3(x, y, z, sizes, colors);
    view(40, 35);

    show();
    return 0;
}

std::tuple<std::vector<double>, std::vector<double>, std::vector<double>>
generate_data() {
    using namespace matplot;
    int n = 16;
    auto r = iota(-n, 2., n);
    auto theta = transform(r, [n](double x) { return x / n * pi; });
    auto phi = transform(r, [n](double x) { return x / n * pi / 2.; });
    auto sinphi = transform(phi, [](double x) { return sin(x); });
    auto cosphi = transform(phi, [](double x) { return cos(x); });
    cosphi.front() = 0;
    cosphi.back() = 0;
    auto sintheta = transform(theta, [](double x) { return sin(x); });
    sintheta.front() = 0;
    sintheta.back() = 0;
    auto costheta = transform(theta, [](double x) { return cos(x); });
    std::vector<std::vector<double>> X(17, std::vector<double>(17, 0.));
    std::vector<std::vector<double>> Y(17, std::vector<double>(17, 0.));
    std::vector<std::vector<double>> Z(17, std::vector<double>(17, 0.));
    for (int i = 0; i < n + 1; ++i) {
        for (int j = 0; j < n + 1; ++j) {
            X[i][j] = cosphi[i] * costheta[j];
            Y[i][j] = cosphi[i] * sintheta[j];
            Z[i][j] = sinphi[i];
        }
    }
    auto X1d = reshape(X);
    auto Y1d = reshape(Y);
    auto Z1d = reshape(Z);
    std::vector<double> x =
        concat(concat(transform(X1d, [](double x) { return x * 0.5; }),
                      transform(X1d, [](double x) { return x * 0.75; })),
               X1d);
    std::vector<double> y =
        concat(concat(transform(Y1d, [](double y) { return y * 0.5; }),
                      transform(Y1d, [](double y) { return y * 0.75; })),
               Y1d);
    std::vector<double> z =
        concat(concat(transform(Z1d, [](double z) { return z * 0.5; }),
                      transform(Z1d, [](double z) { return z * 0.75; })),
               Z1d);

    return std::make_tuple(x, y, z);
}

example_scatter3_4

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

int main() {
    using namespace matplot;

    auto z = linspace(0, 4 * pi, 250);
    auto x = transform(z, [](double z) { return 2 * cos(z) + rand(0, 1); });
    auto y = transform(z, [](double z) { return 2 * sin(z) + rand(0, 1); });

    scatter3(x, y, z, "filled");
    view(-30, 10);

    show();
    return 0;
}

example_scatter3_5

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

int main() {
    using namespace matplot;

    auto z = linspace(0, 4 * pi, 250);
    auto x = transform(z, [](double z) { return 2 * cos(z) + rand(0, 1); });
    auto y = transform(z, [](double z) { return 2 * sin(z) + rand(0, 1); });

    scatter3(x, y, z, "*");
    view(-30, 10);

    show();
    return 0;
}

example_scatter3_6

 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <matplot/matplot.h>
#include <random>
#include <tuple>

std::tuple<std::vector<double>, std::vector<double>, std::vector<double>>
generate_data();

int main() {
    using namespace matplot;

    auto [x, y, z] = generate_data();

    std::vector<double> sizes(x.size() / 3, 16);
    std::fill_n(std::back_inserter(sizes), x.size() / 3, 8);
    std::fill_n(std::back_inserter(sizes), x.size() / 3, 2);

    std::vector<double> colors(x.size() / 3, 1);
    std::fill_n(std::back_inserter(colors), x.size() / 3, 2);
    std::fill_n(std::back_inserter(colors), x.size() / 3, 3);

    scatter3(x, y, z, sizes, colors)->marker_face_color({0, .5, .5});

    show();
    return 0;
}

std::tuple<std::vector<double>, std::vector<double>, std::vector<double>>
generate_data() {
    using namespace matplot;
    int n = 16;
    auto r = iota(-n, 2., n);
    auto theta = transform(r, [n](double x) { return x / n * pi; });
    auto phi = transform(r, [n](double x) { return x / n * pi / 2.; });
    auto sinphi = transform(phi, [](double x) { return sin(x); });
    auto cosphi = transform(phi, [](double x) { return cos(x); });
    cosphi.front() = 0;
    cosphi.back() = 0;
    auto sintheta = transform(theta, [](double x) { return sin(x); });
    sintheta.front() = 0;
    sintheta.back() = 0;
    auto costheta = transform(theta, [](double x) { return cos(x); });
    std::vector<std::vector<double>> X(17, std::vector<double>(17, 0.));
    std::vector<std::vector<double>> Y(17, std::vector<double>(17, 0.));
    std::vector<std::vector<double>> Z(17, std::vector<double>(17, 0.));
    for (int i = 0; i < n + 1; ++i) {
        for (int j = 0; j < n + 1; ++j) {
            X[i][j] = cosphi[i] * costheta[j];
            Y[i][j] = cosphi[i] * sintheta[j];
            Z[i][j] = sinphi[i];
        }
    }
    auto X1d = reshape(X);
    auto Y1d = reshape(Y);
    auto Z1d = reshape(Z);
    std::vector<double> x =
        concat(concat(transform(X1d, [](double x) { return x * 0.5; }),
                      transform(X1d, [](double x) { return x * 0.75; })),
               X1d);
    std::vector<double> y =
        concat(concat(transform(Y1d, [](double y) { return y * 0.5; }),
                      transform(Y1d, [](double y) { return y * 0.75; })),
               Y1d);
    std::vector<double> z =
        concat(concat(transform(Z1d, [](double z) { return z * 0.5; }),
                      transform(Z1d, [](double z) { return z * 0.75; })),
               Z1d);

    return std::make_tuple(x, y, z);
}