Skip to content

Subplots

1
subplot(rows, cols, id);

Unlike other libraries, subplots uses 0-based indices.

example_subplot_1

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

int main() {
    using namespace matplot;

    subplot(2, 1, 0);
    fplot("sin(x)");

    subplot(2, 1, 1);
    fplot("sin(5*x)");

    show();
    return 0;
}

More examples

example_subplot_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
#include <iostream>
#include <matplot/matplot.h>
#include <set>
#include <thread>
#include <vector>

int main() {
    using namespace matplot;

    subplot(2, 2, 0);
    fplot("sin(x)");
    title("Subplot 1: sin(x)");

    subplot(2, 2, 1);
    fplot("sin(2*x)");
    title("Subplot 2: sin(2x)");

    subplot(2, 2, 2);
    fplot("sin(4*x)");
    title("Subplot 3: sin(4x)");

    subplot(2, 2, 3);
    fplot("sin(8*x)");
    title("Subplot 4: sin(8x)");

    show();
    return 0;
}

example_subplot_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
#include <iostream>
#include <matplot/matplot.h>
#include <set>
#include <thread>
#include <vector>

int main() {
    using namespace matplot;

    auto ax1 = subplot(2, 2, 0);
    fplot("cos(x)");
    title("Subplot 1: Cosine");

    auto ax2 = subplot(2, 2, 1);
    fplot("1 - x**2/2 + x**4/24");
    title("Subplot 2: Polynomial");

    auto ax3 = subplot(2, 2, {2, 3});
    fplot("cos(x)", "b");
    hold(on);
    fplot("1 - x**2/2 + x**4/24", "g");
    title("Subplot 3 and 4: Both");

    axis({ax1, ax2, ax3}, {-4, 4, inf, inf});

    show();
    return 0;
}

example_subplot_4

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

int main() {
    using namespace matplot;
    for (size_t i = 0; i < 4; ++i) {
        subplot(2, 2, i);
        std::string equation = "cos(x**" + num2str(i + 1) + ") + " + num2str(i);
        fplot(equation);
        title(equation);
    }
    subplot(2, 2, 1, true);
    fplot("sin(x)");
    title("sin(x)");
    show();
    return 0;
}

example_subplot_5

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

int main() {
    using namespace matplot;
    subplot({0.1f, 0.3f, 0.3f, 0.3f});
    fplot("sin(x)");
    title("First Subplot");

    subplot({0.5f, 0.15f, 0.4f, 0.7f});
    fplot("cos(x)");
    title("Second Subplot");

    show();
    return 0;
}

example_subplot_6

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

int main() {
    using namespace matplot;
    auto ax1 = subplot(2, 1, 0);
    fplot("sin(x)");
    title("First Subplot");

    auto ax2 = subplot(2, 1, 1);
    fplot("cos(x)");
    title("Second Subplot");

    ax1->font_size(15);
    ax2->line_width(2);

    show();
    return 0;
}

example_subplot_7

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

int main() {
    using namespace matplot;
    std::vector<axes_handle> ax;
    for (size_t i = 0; i < 4; ++i) {
        ax.emplace_back(subplot(2, 2, i));
    }

    subplot(ax[1]);
    auto p = fplot("sin(x)");
    p->line_spec().color({0.1f, 0.5f, 0.1f});
    p->line_spec().line_width(2);
    title("Second subplot");
    axis({0, 50, -1, 1});

    show();
    return 0;
}

example_subplot_8

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

int main() {
    using namespace matplot;

    fplot("sin(x)");
    title("Sine Plot");

    auto ax = gca();
    subplot(2, 1, 1, ax);

    show();
    return 0;
}

example_subplot_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
#include <iostream>
#include <matplot/matplot.h>
#include <set>
#include <thread>
#include <vector>

int main() {
    using namespace matplot;

    figure();
    fplot("sin(x)");
    title("Line Plot 1");
    auto ax1 = gca();

    figure();
    fplot("2*sin(x)");
    title("Line Plot 2");
    auto ax2 = gca();

    auto fnew = figure();
    auto ax1_copy = ax1->copy(fnew);
    subplot(2, 1, 0, ax1_copy);

    auto ax2_copy = ax2->copy(fnew);
    subplot(2, 1, 1, ax2_copy);

    show();
    return 0;
}

example_subplot_10

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

int main() {
    using namespace matplot;

    for (size_t k = 0; k < 4; ++k) {
        auto data = rand(10, 0, 1);
        subplot(2, 2, k);
        stem(data);
    }

    subplot(2, 2, 1, true);

    show();
    return 0;
}

example_subplot_11

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

int main() {
    using namespace matplot;

    subplot({0.1f, 0.3f, 0.3f, 0.3f});
    std::vector<std::vector<double>> y = {
        {16, 5, 9, 4}, {2, 11, 7, 14}, {3, 10, 6, 15}, {13, 8, 12, 1}};
    plot(y);
    title("First Subplot");

    subplot({0.5f, 0.15f, 0.4f, 0.7f});
    bar(y);
    title("Second Subplot");

    show();
    return 0;
}

example_subplot_12

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

int main() {
    using namespace matplot;

    figure();

    auto ax1 = subplot(2, 1, 0);
    auto theta = linspace(0, 2 * pi, 50);
    auto rho =
        transform(theta, [](double theta) { return sin(theta) * cos(theta); });
    polarplot(ax1, theta, rho);

    auto ax2 = subplot(2, 1, 1);
    polarscatter(ax2, theta, rho);

    show();
    return 0;
}

example_subplot_13

 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>

int main() {
    using namespace matplot;

    auto ax1 = subplot(2, 1, 0);
    auto [x, y, z] = peaks();
    (void) x;
    (void) y;
    z = transpose(z);
    plot(ax1, z);
    xlim(ax1, {0, 20});

    auto ax2 = subplot(2, 1, 1);
    plot(ax2, z);

    ax1->font_size(15);
    ax2->line_width(2);

    show();
    return 0;
}