Skip to content

Scatter Plot

1
scatter(x,y);

example_scatter_1

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

int main() {
    using namespace matplot;

    auto x = linspace(0, 3 * pi, 200);
    auto y = transform(x, [&](double x) { return cos(x) + rand(0, 1); });

    scatter(x, y);

    show();
    return 0;
}

More examples

example_scatter_2

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

int main() {
    using namespace matplot;

    auto x = linspace(0, 3 * pi, 200);
    auto y = transform(x, [&](double x) { return cos(x) + rand(0, 1); });
    auto c = linspace(1, 10, x.size());

    scatter(x, y, c);

    show();
    return 0;
}

example_scatter_3

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

int main() {
    using namespace matplot;

    auto x = linspace(0, 3 * pi, 200);
    auto y = transform(x, [&](double x) { return cos(x) + rand(0, 1); });
    auto c = linspace(1, 10, x.size());

    scatter(x, y, std::vector<double>{}, c);

    show();
    return 0;
}

example_scatter_4

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

int main() {
    using namespace matplot;

    auto x = linspace(0, 3 * pi, 200);
    auto y = transform(x, [&](double x) { return cos(x) + rand(0, 1); });
    auto c = linspace(1, 10, x.size());

    auto l = scatter(x, y, 6, c);
    l->marker_face(true);

    show();
    return 0;
}

example_scatter_5

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

int main() {
    using namespace matplot;

    auto theta = linspace(0, 2 * pi, 150);
    auto x =
        transform(theta, [&](double t) { return sin(t) + 0.75 * rand(0, 1); });
    auto y =
        transform(theta, [&](double t) { return cos(t) + 0.75 * rand(0, 1); });
    double sz = 23;

    auto l = scatter(x, y, sz);
    l->marker_style(line_spec::marker_style::diamond);

    show();
    return 0;
}

example_scatter_6

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

int main() {
    using namespace matplot;

    auto theta = linspace(0, 2 * pi, 300);
    auto x =
        transform(theta, [&](double t) { return sin(t) + 0.75 * rand(0, 1); });
    auto y =
        transform(theta, [&](double t) { return cos(t) + 0.75 * rand(0, 1); });
    double sz = 6;

    auto l = scatter(x, y, sz);
    l->marker_color({0.f, .5f, .5f});
    l->marker_face_color({0.f, .7f, .7f});

    show();
    return 0;
}

example_scatter_7

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

int main() {
    using namespace matplot;

    auto x = linspace(0, 3 * pi, 200);
    auto y = transform(x, [&](double x) { return cos(x) + rand(0, 1); });

    tiledlayout(2, 1);

    auto ax1 = nexttile();
    scatter(ax1, x, y);

    auto ax2 = nexttile();
    auto l = scatter(ax2, x, y);
    l->marker_face(true);
    l->marker_style(line_spec::marker_style::diamond);

    show();
    return 0;
}

example_scatter_8

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

int main() {
    using namespace matplot;

    auto theta = linspace(0, 1, 500);
    auto x = transform(
        theta, [&](double theta) { return exp(theta) * sin(100 * theta); });
    auto y = transform(
        theta, [&](double theta) { return exp(theta) * cos(100 * theta); });

    auto s = scatter(x, y);
    s->marker_color("b");
    s->marker_face_color({0, .5, .5});

    show();
    return 0;
}

Scatter plots also depend on the line object. As the line object can represent lines with markers, the scatter function simply creates markers without the lines.