Skip to content

X Ticks

1
xticks(xs);

example_xticks_1

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

int main() {
    using namespace matplot;

    auto x = linspace(0, 10);
    auto y = transform(x, [](double x) { return pow(x, 2); });
    plot(x, y);
    xticks({0, 5, 10});
    xticklabels({"x=0", "x=5", "x=10"});

    show();
    return 0;
}

More examples

example_xticks_2

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

int main() {
    using namespace matplot;

    auto x = linspace(-5, +5);
    auto y = transform(x, [](double x) { return pow(x, 2); });
    plot(x, y);
    xticks({-5, -2.5, -1, 0, 1, 2.5, 5});

    show();
    return 0;
}

example_xticks_3

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

int main() {
    using namespace matplot;

    auto x = linspace(0, 50);
    auto y = transform(x, [](double x) { return sin(x / 2.); });
    plot(x, y);
    xticks(iota(0, 10, 50));

    show();
    return 0;
}

example_xticks_4

 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 = linspace(0, 6 * pi);
    auto y = transform(x, [](double x) { return sin(x); });
    plot(x, y);
    xlim({0, 6 * pi});
    xticks(iota(0, pi, 6 * pi));
    xticklabels({"0", "π", "2π", "3π", "4π", "5π", "6π"});

    show();
    return 0;
}

example_xticks_5

 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 = iota(0, 0.5, 3);
    auto y = rand(7, 0, 1);
    plot(x, y);
    auto ticks = iota(0, 0.25, 3);
    xticks(ticks);
    xlabel("min");

    show();
    return 0;
}

example_xticks_6

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

int main() {
    using namespace matplot;

    stem(iota(1, 10));
    xticks({0, 4, 6, 10});
    xticks(automatic);

    show();
    return 0;
}

example_xticks_7

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

int main() {
    using namespace matplot;

    tiledlayout(2, 1);
    auto ax1 = nexttile();
    plot(rand(3, 3, 0, 1));
    auto ax2 = nexttile();
    plot(rand(3, 3, 0, 1));
    xticks({1, 2, 3});

    show();
    return 0;
}

example_xticks_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 = linspace(0, 10);
    auto y = transform(x, [](double x) { return sin(x); });
    plot(x, y);
    xticks({});

    show();
    return 0;
}