Skip to content

Legend

1
legend({str1,str2,str3});

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

int main() {
    using namespace matplot;

    std::vector<double> x = linspace(0, pi);
    std::vector<double> y1 = transform(x, [](auto x) { return cos(x); });
    plot(x, y1);

    hold(on);
    std::vector<double> y2 = transform(x, [](auto x) { return cos(2 * x); });
    plot(x, y2);

    ::matplot::legend({"cos(x)", "cos(2x)"});

    std::vector<double> y3 = transform(x, [](auto x) { return cos(3 * x); });
    auto p = plot(x, y3);
    p->display_name("cos(3x)");
    hold(off);

    show();

    ::matplot::legend(off);
    show();

    return 0;
}

More examples

example_legend_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;

    tiledlayout(2, 1);
    auto y1 = rand(3, 3, 0, 1);
    auto ax1 = nexttile();
    plot(y1);

    auto y2 = rand(3, 3, 0, 1);
    auto ax2 = nexttile();
    plot(y2);

    ::matplot::legend(ax1, {"Line 1", "Line 2", "Line 3"});

    show();
    return 0;
}

example_legend_3

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

int main() {
    using namespace matplot;

    auto x = linspace(0, pi);
    auto y1 = transform(x, [](double x) { return cos(x); });
    auto y2 = transform(x, [](double x) { return cos(2 * x); });

    auto p1 = plot(x, y1);
    p1->display_name("cos(x)");

    hold(on);
    auto p2 = plot(x, y2);
    p2->display_name("cos(2x)");
    hold(off);

    ::matplot::legend({});

    show();
    return 0;
}

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

int main() {
    using namespace matplot;

    auto x = linspace(0, pi);
    auto y1 = transform(x, [](double x) { return cos(x); });
    auto y2 = transform(x, [](double x) { return cos(2 * x); });
    auto y3 = transform(x, [](double x) { return cos(3 * x); });
    auto y4 = transform(x, [](double x) { return cos(4 * x); });

    plot(x, y1);
    hold(on);
    plot(x, y2);
    plot(x, y3);
    plot(x, y4);
    hold(off);

    auto l = ::matplot::legend({"cos(x)", "cos(2x)", "cos(3x)", "cos(4x)"});
    l->location(legend::general_alignment::topleft);
    l->num_rows(2);

    show();
    return 0;
}

example_legend_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;

    auto x = linspace(0, pi);
    auto y1 = transform(x, [](double x) { return cos(x); });
    auto y2 = transform(x, [](double x) { return cos(2 * x); });
    auto y3 = transform(x, [](double x) { return cos(3 * x); });

    auto p1 = plot(x, y1);
    hold(on);
    plot(x, y2);
    auto p3 = plot(x, y3);
    hold(off);

    ::matplot::legend({p1, p3}, {"First", "Third"});

    show();
    return 0;
}

example_legend_6

 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;

    auto x = linspace(0, pi);
    auto y1 = transform(x, [](double x) { return cos(x); });
    auto y2 = transform(x, [](double x) { return cos(2 * x); });

    plot(x, y1);
    hold(on);
    plot(x, y2);
    hold(off);

    auto lgd = ::matplot::legend({"cos(x)", "cos(2x)"});
    title(lgd, "My legend title");

    show();
    return 0;
}

example_legend_7

 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;

    auto x = linspace(0, pi);
    auto y1 = transform(x, [](double x) { return cos(x); });
    auto y2 = transform(x, [](double x) { return cos(2 * x); });

    plot(x, y1);
    hold(on);
    plot(x, y2);
    hold(off);

    auto lgd = ::matplot::legend({"cos(x)", "cos(2x)"});
    lgd->location(legend::general_alignment::bottomleft);
    lgd->box(false);

    show();
    return 0;
}

example_legend_8

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

int main() {
    using namespace matplot;

    auto rdm = rand(4, 4, 0, 1);

    plot(rdm);

    auto lgd = ::matplot::legend({"Line 1", "Line 2", "Line 3", "Line 4"});
    lgd->font_size(12);
    lgd->text_color("blue");
    lgd->num_columns(2);

    show();
    return 0;
}