Skip to content

Quiver

1
quiver(x, y, u, v);

example_quiver_1

 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;
    auto [x, y] = meshgrid(iota(0, 0.2, 2), iota(0, 0.2, 2));
    vector_2d u =
        transform(x, y, [](double x, double y) { return cos(x) * y; });
    vector_2d v =
        transform(x, y, [](double x, double y) { return sin(x) * y; });

    quiver(x, y, u, v);

    show();
    return 0;
}

More examples

example_quiver_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;
    auto [x, y] = meshgrid(iota(0, 0.2, 2), iota(0, 0.2, 2));
    vector_2d u =
        transform(x, y, [](double x, double y) { return cos(x) * y; });
    vector_2d v =
        transform(x, y, [](double x, double y) { return sin(x) * y; });

    quiver(x, y, u, v, 2.);

    show();
    return 0;
}

example_quiver_3

 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;
    auto [x, y] = meshgrid(iota(0, 0.2, 2), iota(0, 0.2, 2));
    vector_2d u =
        transform(x, y, [](double x, double y) { return cos(x) * y; });
    vector_2d v =
        transform(x, y, [](double x, double y) { return sin(x) * y; });

    quiver(x, y, u, v, 0.);

    show();
    return 0;
}

example_quiver_4

 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;
    auto [x, y] = meshgrid(iota(-2, 0.2, 2));
    auto z = transform(x, y, [](double x, double y) {
        return x * exp(-pow(x, 2) - pow(y, 2));
    });
    auto [dx, dy] = gradient(z, .2, .2);

    contour(x, y, z);
    hold(on);

    quiver(x, y, dx, dy);
    hold(off);

    show();
    return 0;
}

example_quiver_6

 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;
    auto [x, y] = meshgrid(iota(-2.5, 0.25, 2.5), iota(-2.5, 0.25, 2.5));
    vector_2d u =
        transform(x, y, [](double x, double y) { return cos(x) * sin(x + y); });
    vector_2d v =
        transform(x, y, [](double x, double y) { return sin(y) * cos(x + y); });

    vector_2d m =
        transform(u, v, [](double u, double v) { return sqrt(u * u + v * v); });

    quiver(x, y, u, v, m, 0.2)->normalize(true).line_width(1.5);

    colormap(palette::jet());
    show();
    return 0;
}

All these subcategories depend on the vectors object type. In a two-dimensional plot, for each value of and with the position of a vector, it also requires the value of and indicating its direction and magnitude. In a three-dimensional plot, the direction and magnitude are defined by , , and .

A quiver plot (or velocity plot) shows a grid of vectors whose direction and magnitude are scaled to prevent the overlap between vectors in subsequent quads.