Skip to content

YY-axis

1
plot(x, y)->use_y2(true);

example_yyaxis_1

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

    hold(on);
    auto z = transform(x, [](double x) { return sin(3 * x) * exp(0.5 * x); });
    plot(x, z)->use_y2(true);
    y2lim({-150, 150});

    show();
    return 0;
}

More examples

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

int main() {
    using namespace matplot;

    auto ind = linspace(0, 51);
    auto drivers =
        transform(ind, [](double ind) { return pow(ind, 2) + randn(0, 50); });
    scatter(ind, drivers);
    title("Highway data");
    xlabel("States");
    ylabel("Licensed Drivers");
    ylim({0, inf});

    hold(on);
    auto pop = transform(
        ind, [](double ind) { return 10000 * (pow(ind, 2) + randn(0, 100)); });
    scatter(ind, pop)->use_y2(true);
    y2label("Vehicle Miles Traveled");
    y2lim({0, inf});

    show();
    return 0;
}

example_yyaxis_3

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

int main() {
    using namespace matplot;

    vector_1d x = linspace(0, 10);
    auto yl1 = transform(x, [](double x) { return sin(x); });
    auto yl2 = transform(x, [](double x) { return sin(x / 2); });
    plot(x, yl1)->color(gca()->colororder()[0]);
    hold(on);
    plot(x, yl2)->color(gca()->colororder()[0]).line_style("--");

    auto yr1 = x;
    auto yr2 = transform(x, [](double x) { return pow(x, 2); });
    plot(x, yr1)->use_y2(true).color(gca()->colororder()[1]);
    plot(x, yr2)->use_y2(true).color(gca()->colororder()[1]).line_style("--");
    hold(off);

    show();
    return 0;
}

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

int main() {
    using namespace matplot;

    colororder({"b", "m"});

    vector_2d y = {{1, 3}, {2, 4}};
    auto p1 = plot(y);
    p1[0]->color("b");
    p1[1]->color("b").line_style("--");
    gca()->y_axis().color("b");

    hold(on);
    vector_2d z = {{4, 2}, {3, 1}};
    auto ps = plot(z);
    ps[0]->use_y2(true).color("m");
    ps[1]->use_y2(true).color("m").line_style("--");
    gca()->y2_axis().color("m");

    ::matplot::legend();

    show();
    return 0;
}

example_yyaxis_5

 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 <matplot/matplot.h>

int main() {
    using namespace matplot;

    vector_2d magic = {{8, 3, 4}, {1, 5, 9}, {6, 7, 2}};
    bar(magic);
    auto ax = gca();
    ax->y_axis().color(ax->colororder()[0]);

    hold(on);
    scatter(std::vector{1., 2., 3.}, std::vector{2., 5., 2.})
        ->marker_face(true)
        .use_y2(true);
    scatter(std::vector{1., 2., 3.}, std::vector{3., 4., 1.})
        ->marker_face(true)
        .use_y2(true);
    scatter(std::vector{1., 2., 3.}, std::vector{4., 2., 4.})
        ->marker_face(true)
        .use_y2(true);
    ax->y2_axis().color(ax->colororder()[1]);
    hold(off);

    ::matplot::legend();

    show();
    return 0;
}

example_yyaxis_6

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

int main() {
    using namespace matplot;

    vector_1d x = linspace(1, 10);

    tiledlayout(2, 1);

    auto ax1 = nexttile();
    plot(ax1, x, transform(x, [](double x) { return sin(x); }));
    hold(on);
    plot(ax1, x, transform(x, [](double x) { return exp(x); }))->use_y2(true);

    auto ax2 = nexttile();
    plot(ax2, iota(1, 10));

    show();
    return 0;
}