Skip to content

Polar Line Plot

1
polarplot(theta, rho);

example_polarplot_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> theta = iota(0, 0.01, 2 * pi);
    std::vector<double> rho =
        transform(theta, [](auto t) { return sin(2 * t) * cos(2 * t); });
    polarplot(theta, rho);

    show();
    return 0;
}

More examples

example_polarplot_2

 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> theta_degrees = linspace(0, 360, 50);
    std::vector<double> rho =
        transform(theta_degrees, [](auto t) { return 0.005 * t / 10.; });
    std::vector<double> theta_radians = deg2rad(theta_degrees);
    polarplot(theta_radians, rho);

    show();
    return 0;
}

example_polarplot_3

 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> theta = linspace(0, 6 * pi);
    std::vector<double> rho1 = transform(theta, [](auto t) { return t / 10.; });
    polarplot(theta, rho1);

    std::vector<double> rho2 = transform(theta, [](auto t) { return t / 12.; });
    hold(on);
    polarplot(theta, rho2, "--");
    hold(off);

    show();
    return 0;
}

example_polarplot_4

 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<double> rho = iota(10, 5, 70);
    polarplot(rho, "-o");

    show();
    return 0;
}

example_polarplot_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> theta = linspace(0, 2 * pi);
    std::vector<double> rho = transform(theta, [](double t) { return sin(t); });
    polarplot(theta, rho);
    gca()->r_axis().limits({-1, 1});

    show();
    return 0;
}

example_polarplot_6

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

int main() {
    using namespace matplot;

    std::vector<double> theta = linspace(0, 2 * pi, 25);
    std::vector<double> rho = transform(theta, [](double t) { return 2 * t; });
    polarplot(theta, rho, "r-o");

    show();
    return 0;
}

example_polarplot_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> theta = linspace(0, 2 * pi, 25);
    std::vector<double> rho = transform(theta, [](double t) { return 2 * t; });
    auto p = polarplot(theta, rho);
    p->color("magenta");
    p->marker("square");
    p->marker_size(8.);

    show();
    return 0;
}

example_polarplot_8

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

int main() {
    using namespace matplot;

    std::vector<std::complex<double>> z = {{2, 3},  {2, 0},   {-1, +4}, {3, -4},
                                           {5, +2}, {-4, -2}, {-2, +3}, {-2, 0},
                                           {0, -3}, {0, 1}};

    polarplot(z);

    show();
    return 0;
}

By emplacing a polar plot in the axes, the axes move to a polar mode, where we use the and axis instead of the and axis.

From the backend point of view, these axes are an abstraction to the user. The data points in the and axis are drawn by converting the positions from the polar coordinates and to the Cartesian coordinates and with the relationships and .

Aside from this conversion, these plot subcategories are analogous to line plots, scatter plots, histograms, quiver plots, and line functions.