Skip to content

Line Plot

Tip

Use these examples to understand how to quickly use the library for data visualization. If you are interested in understanding how the library works, you can later read the details in the complete article.

1
plot(x,y);

Where x and y are are any value ranges.

example_plot_1

 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;
    std::vector<double> x = linspace(0, 2 * pi);
    std::vector<double> y = transform(x, [](auto x) { return sin(x); });

    plot(x, y, "-o");
    hold(on);
    plot(x, transform(y, [](auto y) { return -y; }), "--xr");
    plot(x, transform(x, [](auto x) { return x / pi - 1.; }), "-:gs");
    plot({1.0, 0.7, 0.4, 0.0, -0.4, -0.7, -1}, "k");

    show();
    return 0;
}

Tip

Setters return a reference to *this to allow method chaining:

1
plot(x,y)->line_width(2).color("red");

Tip

These examples use free-standing functions to create plots. You can also use a object-oriented style for plots. We discuss these coding styles in the Section Coding Styles.

More examples

example_plot_2

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

int main() {
    using namespace matplot;

    std::set<std::vector<double>> Y = {
        {16, 5, 9, 4}, {2, 11, 7, 14}, {3, 10, 6, 15}, {13, 8, 12, 1}};
    plot(Y);

    show();
    return 0;
}

example_plot_3

 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;

    std::vector<double> x = linspace(0, 2 * pi);
    std::vector<double> y1 = transform(x, [](auto x) { return sin(x); });
    std::vector<double> y2 = transform(x, [](auto x) { return sin(x - 0.25); });
    std::vector<double> y3 = transform(x, [](auto x) { return sin(x - 0.5); });
    plot(x, y1, x, y2, "--", x, y3, ":");

    show();
    return 0;
}

example_plot_4

 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;

    std::vector<double> x = linspace(0, 2 * pi);
    std::vector<double> y1 = transform(x, [](auto x) { return sin(x); });
    std::vector<double> y2 = transform(x, [](auto x) { return sin(x - 0.25); });
    std::vector<double> y3 = transform(x, [](auto x) { return sin(x - 0.5); });
    plot(x, y1, "g", x, y2, "b--o", x, y3, "c*");

    show();
    return 0;
}

example_plot_5

 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 = linspace(0, 10);
    std::vector<double> y = transform(x, [](auto x) { return sin(x); });
    plot(x, y, "-o")->marker_indices({0,  5,  10, 15, 20, 25, 30, 35, 40, 45,
                                      50, 55, 60, 65, 70, 75, 80, 85, 90, 95});

    show();
    return 0;
}

example_plot_6

 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>
#include <set>

int main() {
    using namespace matplot;

    std::vector<double> x = linspace(-pi, +pi, 20);
    // test with set, just because...
    std::set<double> x2(x.begin(), x.end());

    std::vector<double> y =
        transform(x, [](auto x) { return tan(sin(x)) - sin(tan(x)); });
    plot(x2, y, "--gs")
        ->line_width(2)
        .marker_size(10)
        .marker_color("b")
        .marker_face_color({.5, .5, .5});

    show();
    return 0;
}

example_plot_7

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

int main() {
    using namespace matplot;

    std::vector<double> x = linspace(0, 10, 150);
    std::vector<double> y = transform(x, [](auto x) { return cos(5 * x); });
    plot(x, y)->color({0.f, 0.7f, 0.9f});
    title("2-D Line Plot");
    xlabel("x");
    ylabel("cos(5x)");

    show();
    return 0;
}

example_plot_8

 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(0, 180, 7);
    std::vector<double> y = {0.8, 0.9, 0.1, 0.9, 0.6, 0.1, 0.3};
    plot(x, y);
    title("Time Plot");
    xlabel("Time");
    yrange({0, 1});
    xticks({0, 30, 60, 90, 120, 150, 180});
    xticklabels(
        {"00:00s", "30:00", "01:00", "01:30", "02:00", "02:30", "03:00"});

    show();
    return 0;
}

example_plot_9

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

int main() {
    using namespace matplot;

    std::vector<double> x = linspace(0, 3);
    std::vector<double> y1 = transform(x, [](auto x) { return sin(5 * x); });
    std::vector<double> y2 = transform(x, [](auto x) { return sin(15 * x); });

    tiledlayout(2, 1);
    auto ax1 = nexttile();
    plot(ax1, x, y1);
    title(ax1, "Top Plot");
    ylabel(ax1, "sin(5x)");

    auto ax2 = nexttile();
    plot(ax2, x, y2);
    title(ax2, "Bottom Plot");
    ylabel(ax2, "sin(15x)");

    show();
    return 0;
}

example_plot_10

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

int main() {
    using namespace matplot;

    std::vector<double> x = linspace(-2 * pi, 3);
    std::vector<double> y1 = transform(x, [](auto x) { return sin(x); });
    std::vector<double> y2 = transform(x, [](auto x) { return cos(x); });
    auto p = plot(x, y1, x, y2);
    p[0]->line_width(2);
    p[1]->marker(line_spec::marker_style::asterisk);

    show();
    return 0;
}

example_plot_11

 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;

    double r = 2;
    double xc = 4;
    double yc = 3;
    std::vector<double> theta = linspace(0, 2 * pi);
    std::vector<double> x =
        transform(theta, [=](auto theta) { return r * cos(theta) + xc; });
    std::vector<double> y =
        transform(theta, [=](auto theta) { return r * sin(theta) + yc; });
    plot(x, y);
    axis(equal);

    show();
    return 0;
}

example_plot_12

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

int main() {
    using namespace matplot;

    std::vector<int> y = {2, 4, 7, 7, 6, 3, 9, 7, 3, 5};
    plot(y);

    show();
    return 0;
}