10000 more eigen examples · Cryoris/matplotlib-cpp@f872d23 · GitHub
[go: up one dir, main page]

Skip to content

Commit f872d23

Browse files
committed
more eigen examples
1 parent 033a246 commit f872d23

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ eigen_include = -I /usr/local/include/eigen3
1818

1919
examples: minimal basic modern animation nonblock xkcd quiver bar surface subplot fill_inbetween fill update
2020

21-
eigen: eigen_basic eigen_modern
21+
eigen: eigen_basic eigen_modern eigen_loglog
2222

2323
minimal: examples/minimal.cpp matplotlibcpp.h
2424
cd examples && g++ -DWITHOUT_NUMPY minimal.cpp ${includes} ${linkings} -o minimal ${definitions}
@@ -66,5 +66,9 @@ eigen_basic: examples/eigen/basic.cpp matplotlibcpp.h
6666
eigen_modern: examples/eigen/modern.cpp matplotlibcpp.h
6767
cd examples/eigen && g++ modern.cpp ${includes} ${eigen_include} ${linkings} -o $@ ${definitions}
6868

69+
eigen_loglog: examples/eigen/loglog.cpp matplotlibcpp.h
70+
cd examples/eigen && g++ loglog.cpp ${includes} ${eigen_include} ${linkings} -o $@ ${definitions}
71+
6972
clean:
7073
rm -f examples/{minimal,basic,modern,animation,nonblock,xkcd,quiver,bar,surface,subplot,fill_inbetween,fill,update}
74+
rm -f examples/eigen/{eigen_basic,eigen_modern,eigen_loglog}

examples/eigen/loglog.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#define _USE_MATH_DEFINES
2+
#include "../../matplotlibcpp.h"
3+
#include <Eigen/Dense>
4+
#include <cmath>
5+
#include <iostream>
6+
7+
namespace plt = matplotlibcpp;
8+
9+
int main() {
10+
// Prepare data.
11+
int n = 5000;
12+
Eigen::VectorXd x(n), y(n), z(n), w = Eigen::VectorXd::Ones(n);
13+
for (int i = 0; i < n; ++i) {
14+
double value = (1.0 + i) / n;
15+
x(i) = value;
16+
y(i) = value * value;
17+
z(i) = value * value * value;
18+
}
19+
20+
// Plot line from given x and y data. Color is selected automatically.
21+
plt::plot(x, w);
22+
23+
// Plot a red dashed line from given x and y data.
24+
plt::plot(x, y, "r--");
25+
26+
// Plot a line whose name will show up as "log(x)" in the legend.
27+
plt::plot(x, z, "g:", {{"label", "$x^3$"}});
28+
29+
// Add graph title
30+
plt::title("Sample figure");
31+
32+
// Enable legend.
33+
plt::legend();
34+
35+
// show figure
36+
plt::show();
37+
}

examples/eigen/modern.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#define _USE_MATH_DEFINES
2+
#include "../../matplotlibcpp.h"
3+
#include <Eigen/Dense>
4+
#include <cmath>
5+
6+
namespace plt = matplotlibcpp;
7+
8+
int main() {
9+
// plot(y) - the x-coordinates are implicitly set to [0,1,...,n)
10+
// plt::plot({1,2,3,4});
11+
12+
// Prepare data for parametric plot.
13+
int n = 5000; // number of data points
14+
Eigen::VectorXd x(n), y(n), z(n);
15+
for (int i = 0; i < n; ++i) {
16+
double t = 2 * M_PI * i / n;
17+
x(i) = 16 * sin(t) * sin(t) * sin(t);
18+
y(i) = 13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t);
19+
z(i) = 12.5 + abs(sin(x(i)));
20+
}
21+
22+
// plot() takes an arbitrary number of (x,y,format)-triples.
23+
// x must be iterable (that is, anything providing begin(x) and end(x)),
24+
// y must either be callable (providing operator() const) or iterable.
25+
plt::plot(x, z, "k-", x, y, "r-");
26+
27+
// show plots
28+
plt::show();
29+
}

0 commit comments

Comments
 (0)
0