Skip to content

X Label

1
xlabel(str);

example_xlabel_1

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

int main() {
    using namespace matplot;
    auto x = transform(iota(1, 10), [](double x) { return pow(x, 2); });
    plot(x);
    xlabel("Population");

    show();
    return 0;
}

More examples

example_xlabel_2

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

int main() {
    using namespace matplot;
    auto x = transform(iota(1, 10), [](double x) { return pow(x, 2); });
    plot(x);
    xlabel("Population\\n(in thousands)");

    show();
    return 0;
}

example_xlabel_3

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

int main() {
    using namespace matplot;
    auto x = linspace(-2 * pi, +2 * pi);
    auto y = transform(x, [](double x) { return sin(x); });
    plot(x, y);
    xlabel("-2π ≤ x ≤ 2π");

    show();
    return 0;
}

example_xlabel_4

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

int main() {
    using namespace matplot;
    auto x = transform(iota(1, 10), [](double x) { return pow(x, 2); });
    plot(x);
    int year = 2014;
    xlabel("Population for Year " + num2str(year));

    show();
    return 0;
}

example_xlabel_5

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

int main() {
    using namespace matplot;
    auto t = linspace(0, 1);
    auto y = transform(t, [](double t) { return exp(t); });
    plot(t, y);
    xlabel("t_{seconds}");
    ylabel("e^t");

    show();
    return 0;
}

example_xlabel_6

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

int main() {
    using namespace matplot;
    auto x = transform(iota(1, 10), [](double x) { return pow(x, 2); });
    plot(x);
    xlabel("Population");
    auto ax = gca();
    ax->x_axis().label_font_size(12);
    ax->x_axis().label_weight("bold");
    ax->x_axis().label_color({0, 1, 0, 0});

    show();
    return 0;
}

example_xlabel_7

 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;

    auto x1 = transform(iota(1, 10), [](double x) { return pow(x, 2); });
    auto x2 = transform(iota(1, 10), [](double x) { return pow(x, 3); });

    tiledlayout(2, 1);
    auto ax1 = nexttile();
    plot(x1);
    xlabel(ax1, "Population");

    auto ax2 = nexttile();
    plot(x2);

    show();
    return 0;
}

example_xlabel_8

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

int main() {
    using namespace matplot;

    auto x = transform(iota(1, 10), [](double x) { return pow(x, 2); });
    plot(x);
    xlabel("Population");
    gca()->x_axis().label_color("red");

    show();
    return 0;
}