Skip to content

Error Bars

1
errorbar(x,y,err);

example_errorbar_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, 10, 100);
    std::vector<double> y = {20, 30, 45, 40, 60, 65, 80, 75, 95, 90};
    std::vector<double> err(y.size(), 10.);
    errorbar(x, y, err);
    axis({0, 100, 0, 110});
    show();
    return 0;
}

More examples

example_errorbar_2

 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 = iota(0, 10, 100);
    std::vector<double> y = {20, 30, 45, 40, 60, 65, 80, 75, 95, 90};
    std::vector<double> err(y.size(), 10.);
    errorbar(x, y, err)->filled_curve(true);

    axis({0, 100, 0, 110});

    show();
    return 0;
}

example_errorbar_3

 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, 10, 100);
    std::vector<double> y = {20, 30, 45, 40, 60, 65, 80, 75, 95, 90};
    std::vector<double> err = {5, 8, 2, 9, 3, 3, 8, 3, 9, 3};
    errorbar(x, y, err);
    axis({0, 100, 0, 110});
    show();
    return 0;
}

example_errorbar_4

 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, 10, 100);
    std::vector<double> y = {20, 30, 45, 40, 60, 65, 80, 75, 95, 90};
    std::vector<double> err = {1, 3, 5, 3, 5, 3, 6, 4, 3, 3};
    errorbar(x, y, err, error_bar::type::horizontal);
    axis({0, 100, 0, 110});
    show();
    return 0;
}

example_errorbar_5

 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 = iota(0, 10, 100);
    std::vector<double> y = {20, 30, 45, 40, 60, 65, 80, 75, 95, 90};
    std::vector<double> err = {4, 3, 5, 3, 5, 3, 6, 4, 3, 3};
    errorbar(x, y, err, error_bar::type::both);
    axis({0, 100, 0, 110});
    axis(equal);
    show();
    return 0;
}

example_errorbar_6

 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 = iota(0, 10, 100);
    std::vector<double> y = {20, 30, 45, 40, 60, 65, 80, 75, 95, 90};
    std::vector<double> err = {4, 3, 5, 3, 5, 3, 6, 4, 3, 3};
    errorbar(x, y, err, error_bar::type::both, "o");
    axis({0, 100, 0, 100});
    axis(equal);
    show();
    return 0;
}

example_errorbar_7

 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 = iota(0, 10, 100);
    std::vector<double> y = {20, 30, 45, 40, 60, 65, 80, 75, 95, 90};
    std::vector<double> yneg = {1, 3, 5, 3, 5, 3, 6, 4, 3, 3};
    std::vector<double> ypos = {2, 5, 3, 5, 2, 5, 2, 2, 5, 5};
    std::vector<double> xneg = {1, 3, 5, 3, 5, 3, 6, 4, 3, 3};
    std::vector<double> xpos = {2, 5, 3, 5, 2, 5, 2, 2, 5, 5};

    errorbar(x, y, yneg, ypos, xneg, xpos, "o");
    axis({0, 100, 0, 100});

    show();
    return 0;
}

example_errorbar_8

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

int main() {
    using namespace matplot;

    std::vector<double> x = linspace(0, 10, 15);
    std::vector<double> y = transform(x, [](double x) { return sin(x / 2); });
    std::vector<double> err(y.size(), 0.3);

    auto e = errorbar(x, y, err, "-s")
                 ->marker_size(10)
                 .marker_color("red")
                 .marker_face_color("red");

    show();
    return 0;
}

example_errorbar_9

 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, 15);
    std::vector<double> y = transform(x, [](double x) { return exp(x); });
    std::vector<double> err(y.size(), 0.3);

    auto e = errorbar(x, y, err)->cap_size(18);

    show();
    return 0;
}

example_errorbar_10

 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, 10, 10);
    std::vector<double> y = transform(x, [](double x) { return sin(x / 2.); });
    std::vector<double> err(y.size(), 0.3);

    auto e =
        errorbar(x, y, err)->cap_size(15).marker("*").marker_size(10).color(
            "red");

    show();
    return 0;
}

The error bar object includes extra lines to represent error around data points. Log plots are utility functions that adjust the x or y axes to a logarithmic scale.