10000 redo Makefile, Eigen examples no longer in separate folder · utting98/matplotlib-cpp@1b5b059 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1b5b059

Browse files
committed
redo Makefile, Eigen examples no longer in separate folder
1 parent 37b2113 commit 1b5b059

File tree

10 files changed

+81
-119
lines changed

10 files changed

+81
-119
lines changed

Makefile

Lines changed: 28 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,65 +16,42 @@ definitions = -std=c++11
1616
# Eigen include
1717
eigen_include = -I /usr/local/include/eigen3
1818

19-
examples: minimal basic modern animation nonblock xkcd quiver bar surface subplot fill_inbetween fill update
19+
# Executable names for examples (w/o Eigen)
20+
example_execs = minimal basic animation nonblock xkcd quiver bar surface subplot fill_inbetween fill update
2021

21-
eigen: eigen_basic eigen_modern eigen_loglog eigen_semilogx eigen_semilogy
22+
# Executable names for examples using Eigen
23+
eigen_execs = eigen loglog semilogx semilogy small
2224

23-
minimal: examples/minimal.cpp matplotlibcpp.h
24-
cd examples && g++ -DWITHOUT_NUMPY minimal.cpp ${includes} ${linkings} -o minimal ${definitions}
25-
#cd examples && g++ minimal.cpp ${includes} ${linkings} -o minimal ${definitions}
25+
# Example targets (default if just 'make' is called)
26+
examples: $(example_execs)
2627

27-
basic: examples/basic.cpp matplotlibcpp.h
28-
cd examples && g++ basic.cpp ${includes} ${linkings} -o basic ${definitions}
28+
# Eigen example targets
29+
eigen: $(eigen_execs)
2930

30-
modern: examples/modern.cpp matplotlibcpp.h
31-
cd examples && g++ modern.cpp ${includes} ${linkings} -o modern ${definitions}
31+
# All examples
32+
all: examples eigen
3233

33-
animation: examples/animation.cpp matplotlibcpp.h
34-
cd examples && g++ animation.cpp ${includes} ${linkings} -o animation ${definitions}
34+
# Run all examples
35+
run: run_examples run_eigen
3536

36-
nonblock: examples/nonblock.cpp matplotlibcpp.h
37-
cd examples && g++ nonblock.cpp ${includes} ${linkings} -o nonblock ${definitions}
37+
# Compiler instructions for examples
38+
$(example_execs): %: examples/%.cpp matplotlibcpp.h
39+
g++ $< $(includes) $(linkings) -o examples/$@ $(definitions)
3840

39-
quiver: examples/quiver.cpp matplotlibcpp.h
40-
cd examples && g++ quiver.cpp ${includes} ${linkings} -o quiver ${definitions}
41+
# Run examples
42+
run_examples:
43+
for exec in $(example_execs); do ./examples/$$exec; done
4144

42-
xkcd: examples/xkcd.cpp matplotlibcpp.h
43-
cd examples && g++ xkcd.cpp ${includes} ${linkings} -o xkcd ${definitions}
45+
# Compiler instructions for Eigen examples
46+
$(eigen_execs): %: examples/%.cpp matplotlibcpp.h
47+
g++ $< $(includes) $(eigen_include) $(linkings) -o examples/$@ $(definitions)
4448

45-
bar: examples/bar.cpp matplotlibcpp.h
46-
cd examples && g++ bar.cpp ${includes} ${linkings} -o bar ${definitions}
47-
48-
surface: examples/surface.cpp matplotlibcpp.h
49-
cd examples && g++ surface.cpp ${includes} ${linkings} -o surface ${definitions}
50-
51-
subplot: examples/subplot.cpp matplotlibcpp.h
52-
cd examples && g++ subplot.cpp ${includes} ${linkings} -o subplot ${definitions}
53-
54-
fill_inbetween: examples/fill_inbetween.cpp matplotlibcpp.h
55-
cd examples && g++ fill_inbetween.cpp ${includes} ${linkings} -o fill_inbetween ${definitions}
56-
57-
fill: examples/fill.cpp matplotlibcpp.h
58-
cd examples && g++ fill.cpp ${includes} ${linkings} -o fill ${definitions}
59-
60-
update: examples/update.cpp matplotlibcpp.h
61-
cd examples && g++ update.cpp ${includes} ${linkings} -o update ${definitions}
62-
63-
eigen_basic: examples/eigen/basic.cpp matplotlibcpp.h
64-
cd examples/eigen && g++ basic.cpp ${includes} ${eigen_include} ${linkings} -o $@ ${definitions}
65-
66-
eigen_modern: examples/eigen/modern.cpp matplotlibcpp.h
67-
cd examples/eigen && g++ modern.cpp ${includes} ${eigen_include} ${linkings} -o $@ ${definitions}
68-
69-
eigen_loglog: examples/eigen/loglog.cpp matplotlibcpp.h
70-
cd examples/eigen && g++ loglog.cpp ${includes} ${eigen_include} ${linkings} -o $@ ${definitions}
71-
72-
eigen_semilogx: examples/eigen/semilogx.cpp matplotlibcpp.h
73-
cd examples/eigen && g++ semilogx.cpp ${includes} ${eigen_include} ${linkings} -o $@ ${definitions}
74-
75-
eigen_semilogy: examples/eigen/semilogy.cpp matplotlibcpp.h
76-
cd examples/eigen && g++ semilogy.cpp ${includes} ${eigen_include} ${linkings} -o $@ ${definitions}
49+
# Run Eigen examples
50+
run_eigen:
51+
for exec in $(eigen_execs); do ./examples/$$exec; done
7752

53+
# Clean all
7854
clean:
79-
rm -f examples/{minimal,basic,modern,animation,nonblock,xkcd,quiver,bar,surface,subplot,fill_inbetween,fill,update}
80-
rm -f examples/eigen/{eigen_basic,eigen_modern,eigen_loglog}
55+
# -f to silent warnings if file does not exist
56+
for exec in $(example_execs); do rm -f examples/$$exec; done
57+
for exec in $(eigen_execs); do rm -f examples/$$exec; done

examples/basic.cpp

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,30 @@
1-
#define _USE_MATH_DEFINES
1+
#define _USE_MATH_DEFINES // for sin/log
22
#include "../matplotlibcpp.h"
33
#include <cmath>
44
#include <iostream>
55

66
namespace plt = matplotlibcpp;
77

88
int main() {
9-
// Prepare data.
10-
int n = 5000;
9+
int n = 5000; // 5000 data points
1110
std::vector<double> x(n), y(n), z(n), w(n, 2);
1211
for (int i = 0; i < n; ++i) {
1312
x.at(i) = i * i;
1413
y.at(i) = sin(2 * M_PI * i / 360.0);
1514
z.at(i) = log(i);
1615
}
1716

18-
// Set the size of output image = 1200x780 pixels
19-
plt::figure_size(1200, 780);
17+
plt::figure(); // declare a new figure (optional if only one is used)
2018

21-
// Plot line from given x and y data. Color is selected automatically.
22-
plt::plot(x, y);
19+
plt::plot(x, y); // automatic coloring: tab:blue
20+
plt::show(false);
21+
plt::plot(x, w, "r--"); // red dashed line
22+
plt::plot(x, z, {{"label", "log(x)"}}); // legend label "log(x)"
2323

24-
// Plot a red dashed line from given x and y data.
25-
plt::plot(x, w, "r--");
24+
plt::xlim(0, 1000 * 1000); // x-axis interval: [0, 1e6]
25+
plt::title("Standard usage"); // set a title
26+
plt::legend(); // enable the legend
2627

27-
// Plot a line whose name will show up as "log(x)" in the legend.
28-
plt::plot(x, z, {{"label", "log(x)"}});
29-
30-
// Set x-axis to interval [0,1000000]
31-
plt::xlim(0, 1000 * 1000);
32-
33-
// Add graph title
34-
plt::title("Sample figure");
35-
36-
// Enable legend.
37-
plt::legend();
38-
39-
// save figure
40-
const char *filename = "./basic.png";
41-
std::cout << "Saving result to " << filename << std::endl;
42-
;
43-
plt::save(filename);
28+
plt::savefig("standard.pdf"); // save the figure
29+
plt::show();
4430
}

examples/eigen/basic.cpp renamed to examples/eigen.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#define _USE_MATH_DEFINES
2-
#include "../../matplotlibcpp.h"
2+
#include "../matplotlibcpp.h"
33
#include <Eigen/Dense>
44
#include <cmath>
55
#include <iostream>
@@ -38,8 +38,8 @@ int main() {
3838
plt::legend();
3939

4040
// save figure
41-
const char *filename = "./basic.png";
41+
const char *filename = "./eigen_basic.png";
4242
std::cout << "Saving result to " << filename << std::endl;
4343

44-
plt::save(filename);
44+
plt::savefig(filename);
4545
}

examples/eigen/modern.cpp

Lines changed: 0 additions & 29 deletions
This file was deleted.

examples/eigen/loglog.cpp renamed to examples/loglog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#define _USE_MATH_DEFINES
2-
#include "../../matplotlibcpp.h"
2+
#include "../matplotlibcpp.h"
33
#include <Eigen/Dense>
44
#include <cmath>
55
#include <iostream>

examples/minimal.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include "../matplotlibcpp.h"
2+
#include <vector>
23

34
namespace plt = matplotlibcpp;
45

56
int main() {
6-
plt::plot({1, 3, 2, 4});
7-
plt::show();
7+
std::vector<double> y = {1, 3, 2, 4};
8+
plt::plot(y);
9+
plt::savefig("minimal.pdf");
810
}

examples/modern.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include "../matplotlibcpp.h"
33
#include <cmath>
44

5-
using namespace std;
65
namespace plt = matplotlibcpp;
76

87
int main() {
@@ -11,12 +10,12 @@ int main() {
1110

1211
// Prepare data for parametric plot.
1312
int n = 5000; // number of data points
14-
vector<double> x(n), y(n), z(n);
13+
Eigen::VectorXd x(n), y(n), z(n);
1514
for (int i = 0; i < n; ++i) {
1615
double t = 2 * M_PI * i / n;
17-
x.at(i) = 16 * sin(t) * sin(t) * sin(t);
18-
y.at(i) = 13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t);
19-
z.at(i) = 12.5 + abs(sin(x.at(i)));
16+
x(i) = 16 * sin(t) * sin(t) * sin(t);
17+
y(i) = 13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t);
18+
z(i) = 12.5 + abs(sin(x(i)));
2019
}
2120

2221
// plot() takes an arbitrary number of (x,y,format)-triples.

examples/eigen/semilogx.cpp renamed to examples/semilogx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#define _USE_MATH_DEFINES
2-
#include "../../matplotlibcpp.h"
2+
#include "../matplotlibcpp.h"
33
#include <Eigen/Dense>
44
#include <cmath>
55
#include <iostream>

examples/eigen/semilogy.cpp renamed to examples/semilogy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#define _USE_MATH_DEFINES
2-
#include "../../matplotlibcpp.h"
2+
#include "../matplotlibcpp.h"
33
#include <Eigen/Dense>
44
#include <cmath>
55
#include <iostream>

examples/small.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "../matplotlibcpp.h"
2+
#include <Eigen/Dense>
3+
#include <iostream>
4+
5+
namespace plt = matplotlibcpp;
6+
7+
int main() {
8+
9+
Eigen::VectorXd x = Eigen::VectorXd::LinSpaced(200, 0, 6);
10+
Eigen::VectorXd y, z;
11+
12+
// y = exp(sin(x)), z = exp(cos(z))
13+
y = x.array().sin().exp().matrix();
14+
z = x.array().cos().exp().matrix();
15+
16+
plt::figure();
17+
18+
plt::loglog(x, y, "tab:red");
19+
plt::loglog(x, z, "tab:blue", {{"linestyle", "--"}});
20+
21+
plt::xlabel("Time in lecture");
22+
plt::ylabel("Student confusion");
23+
24+
plt::grid();
25+
plt::savefig("eigen.pdf");
26+
// plt::show(); // show the figure instead of saving it
27+
}

0 commit comments

Comments
 (0)
0