@@ -9,85 +9,87 @@ It is built to resemble the plotting API used by Matlab and matplotlib.
9
9
Usage
10
10
-----
11
11
Complete minimal example:
12
+ ``` cpp
13
+ #include " matplotlibcpp.h"
14
+ namespace plt = matplotlibcpp;
15
+ int main() {
16
+ plt::plot({1,2,3,4});
17
+ plt::show();
18
+ }
19
+ ```
20
+ g++ minimal.cpp -std=c++11 -I/usr/include/python2.7 -lpython2.7
12
21
13
- #include "matplotlibcpp.h"
14
- namespace plt = matplotlibcpp;
15
- int main() {
16
- plt::plot({1,2,3,4});
17
- plt::show();
18
- }
19
-
20
- // g++ minimal.cpp -std=c++11 -I/usr/include/python2.7 -lpython2.7
22
+ **Result:**
21
23
22
- Result: ![ Minimal example] ( ./examples/minimal.png )
24
+ 
23
25
24
26
A more comprehensive example:
25
-
26
- #include "matplotlibcpp.h"
27
- #include <cmath>
28
-
29
- namespace plt = matplotlibcpp;
30
-
31
- int main()
32
- {
33
- // Prepare data.
34
- int n = 5000;
35
- std::vector<double> x(n), y(n), z(n), w(n,2);
36
- for(int i=0; i<n; ++i) {
37
- x.at(i) = i*i;
38
- y.at(i) = sin(2*M_PI*i/360.0);
39
- z.at(i) = log(i);
40
- }
41
-
42
- // Plot line from given x and y data. Color is selected automatically.
43
- plt::plot(x, y);
44
- // Plot a red dashed line from given x and y data.
45
- plt::plot(x, w,"r--");
46
- // Plot a line whose name will show up as "log(x)" in the legend.
47
- plt::named_plot("log(x)", x, z);
48
-
49
- // Set x-axis to interval [0,1000000]
50
- plt::xlim(0, 1000*1000);
51
- // Enable legend.
52
- plt::legend();
53
- // Save the image (file format is determined by the extension)
54
- plt::save("./basic.png");
27
+ ```cpp
28
+ #include "matplotlibcpp.h"
29
+ #include <cmath>
30
+
31
+ namespace plt = matplotlibcpp;
32
+
33
+ int main()
34
+ {
35
+ // Prepare data.
36
+ int n = 5000;
37
+ std::vector<double> x(n), y(n), z(n), w(n,2);
38
+ for(int i=0; i<n; ++i) {
39
+ x.at(i) = i*i;
40
+ y.at(i) = sin(2*M_PI*i/360.0);
41
+ z.at(i) = log(i);
55
42
}
56
43
57
- // g++ basic.cpp -I/usr/include/python2.7 -lpython2.7
44
+ // Plot line from given x and y data. Color is selected automatically.
45
+ plt::plot(x, y);
46
+ // Plot a red dashed line from given x and y data.
47
+ plt::plot(x, w,"r--");
48
+ // Plot a line whose name will show up as "log(x)" in the legend.
49
+ plt::named_plot("log(x)", x, z);
50
+
51
+ // Set x-axis to interval [0,1000000]
52
+ plt::xlim(0, 1000*1000);
53
+ // Enable legend.
54
+ plt::legend();
55
+ // Save the image (file format is determined by the extension)
56
+ plt::save("./basic.png");
57
+ }
58
+ ```
59
+ g++ basic.cpp -I/usr/include/python2.7 -lpython2.7
58
60
59
61
Result: ![ Basic example] ( ./examples/basic.png )
60
62
61
63
matplotlib-cpp doesn't require C++11, but will enable some additional syntactic sugar when available:
64
+ ``` cpp
65
+ #include < cmath>
66
+ #include " matplotlibcpp.h"
67
+
68
+ using namespace std ;
69
+ namespace plt = matplotlibcpp;
70
+
71
+ int main()
72
+ {
73
+ // Prepare data.
74
+ int n = 5000; // number of data points
75
+ vector<double> x(n),y(n);
76
+ for(int i=0; i<n; ++i) {
77
+ double t = 2*M_PI*i/n;
78
+ x.at(i) = 16*sin(t)*sin(t)*sin(t);
79
+ y.at(i) = 13*cos(t) - 5*cos(2*t) - 2*cos(3*t) - cos(4*t);
80
+ }
62
81
63
- #include <cmath>
64
- #include "matplotlibcpp.h"
65
-
66
- using namespace std;
67
- namespace plt = matplotlibcpp;
68
-
69
- int main()
70
- {
71
- // Prepare data.
72
- int n = 5000; // number of data points
73
- vector<double> x(n),y(n);
74
- for(int i=0; i<n; ++i) {
75
- double t = 2*M_PI*i/n;
76
- x.at(i) = 16*sin(t)*sin(t)*sin(t);
77
- y.at(i) = 13*cos(t) - 5*cos(2*t) - 2*cos(3*t) - cos(4*t);
78
- }
79
-
80
- // plot() takes an arbitrary number of (x,y,format)-triples.
81
- // x must be iterable (that is, anything providing begin(x) and end(x)),
82
- // y must either be callable (providing operator() const) or iterable.
83
- plt::plot(x, y, "r-", x, [](double d) { return 12.5+abs(sin(d)); }, "k-");
82
+ // plot() takes an arbitrary number of (x,y,format)-triples.
83
+ // x must be iterable (that is, anything providing begin(x) and end(x)),
84
+ // y must either be callable (providing operator() const) or iterable.
85
+ plt::plot (x, y, "r-", x, [ ] (double d) { return 12.5+abs(sin(d)); }, "k-");
84
86
85
87
86
- // show plots
87
- plt::show();
88
- }
89
-
90
- // g++ modern.cpp -std=c++11 -I/usr/include/python2.7 -lpython
88
+ // show plots
89
+ plt::show();
90
+ }
91
+ ```
92
+ g++ modern.cpp -std=c++11 -I/usr/include/python2.7 -lpython
91
93
92
94
Result: 
93
95
@@ -113,11 +115,11 @@ matplotlib-cpp.
113
115
114
116
If you prefer to use CMake as build system, you will want to add something like this to your
115
117
CMakeLists.txt:
116
-
117
- find_package(PythonLibs 2.7)
118
- target_include_directories(myproject PRIVATE ${PYTHON_INCLUDE_DIRS})
119
- target_link_libraries(myproject ${PYTHON_LIBRARIES})
120
-
118
+ ```cmake
119
+ find_package(PythonLibs 2.7)
120
+ target_include_directories(myproject PRIVATE ${PYTHON_INCLUDE_DIRS})
121
+ target_link_libraries(myproject ${PYTHON_LIBRARIES})
122
+ ```
121
123
# Python 3
122
124
123
125
This library supports both python2 and python3 (although the python3 support is probably far less tested,
0 commit comments