Skip to content

Text

The annotations category is meant to create individual objects on the plot rather than representations of data sets. An important difference between the annotations category and other categories is that, by default, the annotations do not replace the plot that already exists in the axes object, even if the user does not call the hold function.

1
text(x0, y0, str);

example_text_1

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

int main() {
    using namespace matplot;

    std::vector<double> x = iota(0, pi / 20, 2 * pi);
    std::vector<double> y = transform(x, [](auto x) { return sin(x); });
    plot(x, y);
    text(pi, 0, "← sin(π)");

    show();
    return 0;
}

More examples

example_text_2

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

int main() {
    using namespace matplot;

    std::vector<double> x = linspace(-5, +5);
    std::vector<double> y =
        transform(x, [](auto x) { return pow(x, 3) - 12 * x; });
    plot(x, y);

    std::vector<double> xt = {-2, +2};
    std::vector<double> yt = {16, -16};
    std::string str = "dy/dx = 0";
    text(xt, yt, str);

    show();
    return 0;
}

example_text_3

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

int main() {
    using namespace matplot;

    std::vector<double> x = linspace(-5, +5);
    std::vector<double> y =
        transform(x, [](auto x) { return pow(x, 3) - 12 * x; });
    plot(x, y);

    std::vector<double> xt = {-2, +2};
    std::vector<double> yt = {16, -16};
    std::vector<std::string> str = {"local max", "local min"};
    text(xt, yt, str);

    show();
    return 0;
}

example_text_4

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

int main() {
    using namespace matplot;

    plot(iota(1, 10));
    text(2, 7, "A simple plot\\nfrom 1 to 10");

    show();
    return 0;
}

example_text_5

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

int main() {
    using namespace matplot;

    plot(iota(1, 10));
    text({2, 8}, {7, 7},
         std::vector<std::string>({"A simple plot\\nfrom 1 to 10", "y=x"}));

    show();
    return 0;
}

example_text_6

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

int main() {
    using namespace matplot;

    plot(iota(1, 10));
    auto t = text(2, 8, "A simple plot")->color("red").font_size(14);

    show();
    return 0;
}

example_text_7

 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 x = linspace(-5, +5);
    auto y = transform(x, [](double x) { return pow(x, 3) - 12 * x; });
    plot(x, y);
    auto t = text({-2, 2}, {16, -16}, "dy/dx=0");
    t->colors({.0, 1.});
    t->sizes({14, 10});
    gca()->colormap({{1, 0, 0}, {0, 0, 0}});

    show();
    return 0;
}

example_text_8

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

int main() {
    using namespace matplot;

    auto y = transform(iota(1, 10), [](double x) { return pow(x, 2); });
    plot(y);

    text(3, 90, "λ_{12}^{3/2}/π - πΔ^{2/3}");
    text(4, 60, "lambda12^{3/2}/pi - pi delta^{2/3}");

    show();
    return 0;
}