Skip to content

Commit 52931d5

Browse files
authored
Merge pull request lava#1 from tkphd/imshow
rebase imshow to origin/master
2 parents ee244f9 + 8382571 commit 52931d5

File tree

11 files changed

+635
-29
lines changed

11 files changed

+635
-29
lines changed

Makefile

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
examples: minimal basic modern animation nonblock xkcd quiver
1+
examples: minimal basic modern animation nonblock xkcd quiver bar surface fill_inbetween fill update
22

33
minimal: examples/minimal.cpp matplotlibcpp.h
44
cd examples && g++ -DWITHOUT_NUMPY minimal.cpp -I/usr/include/python2.7 -lpython2.7 -o minimal -std=c++11
55

66
basic: examples/basic.cpp matplotlibcpp.h
7-
cd examples && g++ basic.cpp -I/usr/include/python2.7 -lpython2.7 -o basic
7+
cd examples && g++ basic.cpp -I/usr/include/python2.7 -lpython2.7 -o basic -std=c++11
88

99
modern: examples/modern.cpp matplotlibcpp.h
1010
cd examples && g++ modern.cpp -I/usr/include/python2.7 -lpython2.7 -o modern -std=c++11
@@ -21,5 +21,20 @@ quiver: examples/quiver.cpp matplotlibcpp.h
2121
xkcd: examples/xkcd.cpp matplotlibcpp.h
2222
cd examples && g++ xkcd.cpp -I/usr/include/python2.7 -lpython2.7 -o xkcd -std=c++11
2323

24+
bar: examples/bar.cpp matplotlibcpp.h
25+
cd examples && g++ bar.cpp -I/usr/include/python2.7 -lpython2.7 -o bar -std=c++11
26+
27+
surface: examples/surface.cpp matplotlibcpp.h
28+
cd examples && g++ surface.cpp -I/usr/include/python2.7 -lpython2.7 -o surface -std=c++11
29+
30+
fill_inbetween: examples/fill_inbetween.cpp matplotlibcpp.h
31+
cd examples && g++ fill_inbetween.cpp -I/usr/include/python2.7 -lpython2.7 -o fill_inbetween -std=c++11
32+
33+
fill: examples/fill.cpp matplotlibcpp.h
34+
cd examples && g++ fill.cpp -I/usr/include/python2.7 -lpython2.7 -o fill -std=c++11
35+
36+
update: examples/update.cpp matplotlibcpp.h
37+
cd examples && g++ update.cpp -I/usr/include/python2.7 -lpython2.7 -o update -std=c++11
38+
2439
clean:
25-
rm -f examples/{minimal,basic,modern,animation,nonblock,xkcd,quiver}
40+
rm -f examples/{minimal,basic,modern,animation,nonblock,xkcd,quiver,bar,surface,fill_inbetween,fill,update}

README.md

+41-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ int main()
6565

6666
![Basic example](./examples/basic.png)
6767

68-
matplotlib-cpp doesn't require C++11, but will enable some additional syntactic sugar when available:
68+
Alternatively, matplotlib-cpp also supports some C++11-powered syntactic sugar:
6969
```cpp
7070
#include <cmath>
7171
#include "matplotlibcpp.h"
@@ -159,6 +159,36 @@ int main()
159159
160160
![quiver example](./examples/quiver.png)
161161
162+
When working with 3d functions, you might be interested in 3d plots:
163+
```cpp
164+
#include "../matplotlibcpp.h"
165+
166+
namespace plt = matplotlibcpp;
167+
168+
int main()
169+
{
170+
std::vector<std::vector<double>> x, y, z;
171+
for (double i = -5; i <= 5; i += 0.25) {
172+
std::vector<double> x_row, y_row, z_row;
173+
for (double j = -5; j <= 5; j += 0.25) {
174+
x_row.push_back(i);
175+
y_row.push_back(j);
176+
z_row.push_back(::std::sin(::std::hypot(i, j)));
177+
}
178+
x.push_back(x_row);
179+
y.push_back(y_row);
180+
z.push_back(z_row);
181+
}
182+
183+
plt::plot_surface(x, y, z);
184+
plt::show();
185+
}
186+
```
187+
188+
**Result:**
189+
190+
![surface example](./examples/surface.png)
191+
162192
Installation
163193
------------
164194

@@ -186,6 +216,16 @@ find_package(PythonLibs 2.7)
186216
target_include_directories(myproject PRIVATE ${PYTHON_INCLUDE_DIRS})
187217
target_link_libraries(myproject ${PYTHON_LIBRARIES})
188218
```
219+
220+
# C++11
221+
222+
Currently, c++11 is required to build matplotlib-cpp. The last working commit that did
223+
not have this requirement was `717e98e752260245407c5329846f5d62605eff08`.
224+
225+
Note that support for c++98 was dropped more or less accidentally, so if you have to work
226+
with an ancient compiler and still want to enjoy the latest additional features, I'd
227+
probably merge a PR that restores support.
228+
189229
# Python 3
190230

191231
This library supports both python2 and python3 (although the python3 support is probably far less tested,

contrib/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ add_executable(modern ${CMAKE_CURRENT_SOURCE_DIR}/../examples/modern.cpp)
2323
add_executable(animation ${CMAKE_CURRENT_SOURCE_DIR}/../examples/animation.cpp)
2424
add_executable(nonblock ${CMAKE_CURRENT_SOURCE_DIR}/../examples/nonblock.cpp)
2525
add_executable(xkcd ${CMAKE_CURRENT_SOURCE_DIR}/../examples/xkcd.cpp)
26+
add_executable(bar ${CMAKE_CURRENT_SOURCE_DIR}/../examples/bar.cpp)

examples/bar.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#define _USE_MATH_DEFINES
2+
3+
#include <iostream>
4+
#include <string>
5+
#include "../matplotlibcpp.h"
6+
namespace plt = matplotlibcpp;
7+
8+
int main(int argc, char **argv) {
9+
std::vector<int> test_data;
10+
for (int i = 0; i < 20; i++) {
11+
test_data.push_back(i);
12+
}
13+
14+
plt::bar(test_data);
15+
plt::show();
16+
17+
return (0);
18+
}

examples/bar.png

11.7 KB
Loading

examples/fill.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#define _USE_MATH_DEFINES
2+
#include "../matplotlibcpp.h"
3+
#include <cmath>
4+
5+
using namespace std;
6+
namespace plt = matplotlibcpp;
7+
8+
// Example fill plot taken from:
9+
// https://matplotlib.org/gallery/misc/fill_spiral.html
10+
int main() {
11+
// Prepare data.
12+
vector<double> theta;
13+
for (double d = 0; d < 8 * M_PI; d += 0.1)
14+
theta.push_back(d);
15+
16+
const int a = 1;
17+
const double b = 0.2;
18+
19+
for (double dt = 0; dt < 2 * M_PI; dt += M_PI/2.0) {
20+
vector<double> x1, y1, x2, y2;
21+
for (double th : theta) {
22+
x1.push_back( a*cos(th + dt) * exp(b*th) );
23+
y1.push_back( a*sin(th + dt) * exp(b*th) );
24+
25+
x2.push_back( a*cos(th + dt + M_PI/4.0) * exp(b*th) );
26+
y2.push_back( a*sin(th + dt + M_PI/4.0) * exp(b*th) );
27+
}
28+
29+
x1.insert(x1.end(), x2.rbegin(), x2.rend());
30+
y1.insert(y1.end(), y2.rbegin(), y2.rend());
31+
32+
plt::fill(x1, y1, {});
33+
}
34+
plt::show();
35+
}

examples/fill.png

61.5 KB
Loading

examples/surface.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "../matplotlibcpp.h"
2+
3+
#include <cmath>
4+
5+
namespace plt = matplotlibcpp;
6+
7+
int main()
8+
{
9+
std::vector<std::vector<double>> x, y, z;
10+
for (double i = -5; i <= 5; i += 0.25) {
11+
std::vector<double> x_row, y_row, z_row;
12+
for (double j = -5; j <= 5; j += 0.25) {
13+
x_row.push_back(i);
14+
y_row.push_back(j);
15+
z_row.push_back(::std::sin(::std::hypot(i, j)));
16+
}
17+
x.push_back(x_row);
18+
y.push_back(y_row);
19+
z.push_back(z_row);
20+
}
21+
22+
plt::plot_surface(x, y, z);
23+
plt::show();
24+
}

examples/surface.png

104 KB
Loading

examples/update.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#define _USE_MATH_DEFINES
2+
#include <cmath>
3+
#include "../matplotlibcpp.h"
4+
#include <chrono>
5+
6+
namespace plt = matplotlibcpp;
7+
8+
void update_window(const double x, const double y, const double t,
9+
std::vector<double> &xt, std::vector<double> &yt)
10+
{
11+
const double target_length = 300;
12+
const double half_win = (target_length/(2.*sqrt(1.+t*t)));
13+
14+
xt[0] = x - half_win;
15+
xt[1] = x + half_win;
16+
yt[0] = y - half_win*t;
17+
yt[1] = y + half_win*t;
18+
}
19+
20+
21+
int main()
22+
{
23+
size_t n = 1000;
24+
std::vector<double> x, y;
25+
26+
const double w = 0.05;
27+
const double a = n/2;
28+
29+
for (size_t i=0; i<n; i++) {
30+
x.push_back(i);
31+
y.push_back(a*sin(w*i));
32+
}
33+
34+
std::vector<double> xt(2), yt(2);
35+
36+
plt::title("Tangent of a sine curve");
37+
plt::xlim(x.front(), x.back());
38+
plt::ylim(-a, a);
39+
plt::axis("equal");
40+
41+
// Plot sin once and for all.
42+
plt::named_plot("sin", x, y);
43+
44+
// Prepare plotting the tangent.
45+
plt::Plot plot("tangent");
46+
47+
plt::legend();
48+
49+
for (size_t i=0; i<n; i++) {
50+
if (i % 10 == 0) {
51+
update_window(x[i], y[i], a*w*cos(w*x[i]), xt, yt);
52+
53+
// Just update data for this plot.
54+
plot.update(xt, yt);
55+
56+
// Small pause so the viewer has a chance to enjoy the animation.
57+
plt::pause(0.1);
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)