Skip to content

Commit 5b6cc66

Browse files
authored
Merge pull request #1 from amadeus84/segfault_fix
examples: fix segfault on exit with python3
2 parents ef0383f + 57b8784 commit 5b6cc66

19 files changed

+193
-71
lines changed

examples/animation.cpp

+33-26
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//
2+
// g++ -g -Wall -o animation $(python-config --includes --cflags) animation.cpp $(python-config --ldflags --embed)
3+
//
4+
15
#define _USE_MATH_DEFINES
26
#include <cmath>
37
#include "../matplotlibcpp.h"
@@ -6,31 +10,34 @@ namespace plt = matplotlibcpp;
610

711
int main()
812
{
9-
int n = 1000;
10-
std::vector<double> x, y, z;
11-
12-
for(int i=0; i<n; i++) {
13-
x.push_back(i*i);
14-
y.push_back(sin(2*M_PI*i/360.0));
15-
z.push_back(log(i));
16-
17-
if (i % 10 == 0) {
18-
// Clear previous plot
19-
plt::clf();
20-
// Plot line from given x and y data. Color is selected automatically.
21-
plt::plot(x, y);
22-
// Plot a line whose name will show up as "log(x)" in the legend.
23-
plt::named_plot("log(x)", x, z);
24-
25-
// Set x-axis to interval [0,1000000]
26-
plt::xlim(0, n*n);
27-
28-
// Add graph title
29-
plt::title("Sample figure");
30-
// Enable legend.
31-
plt::legend();
32-
// Display plot continuously
33-
plt::pause(0.01);
34-
}
13+
int n = 1000;
14+
std::vector<double> x, y, z;
15+
16+
for(int i=0; i<n; i++) {
17+
x.push_back(i*i);
18+
y.push_back(sin(2*M_PI*i/360.0));
19+
z.push_back(log(i));
20+
21+
if (i % 10 == 0) {
22+
// Clear previous plot
23+
plt::clf();
24+
// Plot line from given x and y data. Color is selected automatically.
25+
plt::plot(x, y);
26+
// Plot a line whose name will show up as "log(x)" in the legend.
27+
plt::named_plot("log(x)", x, z);
28+
29+
// Set x-axis to interval [0,1000000]
30+
plt::xlim(0, n*n);
31+
32+
// Add graph title
33+
plt::title("Sample figure");
34+
// Enable legend.
35+
plt::legend();
36+
// Display plot continuously
37+
plt::pause(0.01);
3538
}
39+
}
40+
41+
plt::detail::_interpreter::kill();
42+
return 0;
3643
}

examples/bar.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//
2+
// g++ -g -Wall -o bar $(python-config --includes --cflags) bar.cpp $(python-config --ldflags --embed)
3+
//
4+
15
#define _USE_MATH_DEFINES
26

37
#include <iostream>
@@ -14,5 +18,6 @@ int main(int argc, char **argv) {
1418
plt::bar(test_data);
1519
plt::show();
1620

21+
plt::detail::_interpreter::kill();
1722
return (0);
1823
}

examples/basic.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
//
2+
// g++ -g -Wall -o basic -I/usr/include/python3.9 basic.cpp -lpython3.9
3+
// g++ -g -Wall -o basic $(python-config --includes --cflags) basic.cpp $(python-config --ldflags --embed)
4+
//
5+
16
#define _USE_MATH_DEFINES
27
#include <iostream>
38
#include <cmath>
49
#include "../matplotlibcpp.h"
510

611
namespace plt = matplotlibcpp;
712

8-
int main()
13+
int main()
914
{
1015
// Prepare data.
1116
int n = 5000;
@@ -15,7 +20,7 @@ int main()
1520
y.at(i) = sin(2*M_PI*i/360.0);
1621
z.at(i) = log(i);
1722
}
18-
23+
1924
// Set the size of output image = 1200x780 pixels
2025
plt::figure_size(1200, 780);
2126

@@ -41,4 +46,7 @@ int main()
4146
const char* filename = "./basic.png";
4247
std::cout << "Saving result to " << filename << std::endl;;
4348
plt::save(filename);
49+
50+
plt::detail::_interpreter::kill();
51+
return 0;
4452
}

examples/basic.png

68 Bytes
Loading

examples/colorbar.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//
2+
// g++ -g -Wall -o colorbar $(python-config --includes --cflags) colorbar.cpp $(python-config --ldflags --embed)
3+
//
4+
15
#define _USE_MATH_DEFINES
26
#include <cmath>
37
#include <iostream>
@@ -29,4 +33,7 @@ int main()
2933
plt::show();
3034
plt::close();
3135
Py_DECREF(mat);
36+
37+
plt::detail::_interpreter::kill();
38+
return 0;
3239
}

examples/contour.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//
2+
// g++ -g -Wall -o contour $(python-config --includes --cflags) contour.cpp $(python-config --ldflags --embed)
3+
//
4+
15
#include "../matplotlibcpp.h"
26

37
#include <cmath>
@@ -21,4 +25,7 @@ int main()
2125

2226
plt::contour(x, y, z);
2327
plt::show();
28+
29+
plt::detail::_interpreter::kill();
30+
return (0);
2431
}

examples/fill.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//
2+
// g++ -g -Wall -o fill $(python-config --includes --cflags) fill.cpp $(python-config --ldflags --embed)
3+
//
4+
15
#define _USE_MATH_DEFINES
26
#include "../matplotlibcpp.h"
37
#include <cmath>
@@ -32,4 +36,7 @@ int main() {
3236
plt::fill(x1, y1, {});
3337
}
3438
plt::show();
39+
40+
plt::detail::_interpreter::kill();
41+
return (0);
3542
}

examples/fill_inbetween.cpp

+23-16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//
2+
// g++ -g -Wall -o fill_inbetween $(python-config --includes --cflags) fill_inbetween.cpp $(python-config --ldflags --embed)
3+
//
4+
15
#define _USE_MATH_DEFINES
26
#include "../matplotlibcpp.h"
37
#include <cmath>
@@ -7,22 +11,25 @@ using namespace std;
711
namespace plt = matplotlibcpp;
812

913
int main() {
10-
// Prepare data.
11-
int n = 5000;
12-
std::vector<double> x(n), y(n), z(n), w(n, 2);
13-
for (int i = 0; i < n; ++i) {
14-
x.at(i) = i * i;
15-
y.at(i) = sin(2 * M_PI * i / 360.0);
16-
z.at(i) = log(i);
17-
}
14+
// Prepare data.
15+
int n = 5000;
16+
std::vector<double> x(n), y(n), z(n), w(n, 2);
17+
for (int i = 0; i < n; ++i) {
18+
x.at(i) = i * i;
19+
y.at(i) = sin(2 * M_PI * i / 360.0);
20+
z.at(i) = log(i);
21+
}
22+
23+
// Prepare keywords to pass to PolyCollection. See
24+
// https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.fill_between.html
25+
std::map<string, string> keywords;
26+
keywords["alpha"] = "0.4";
27+
keywords["color"] = "grey";
28+
keywords["hatch"] = "-";
1829

19-
// Prepare keywords to pass to PolyCollection. See
20-
// https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.fill_between.html
21-
std::map<string, string> keywords;
22-
keywords["alpha"] = "0.4";
23-
keywords["color"] = "grey";
24-
keywords["hatch"] = "-";
30+
plt::fill_between(x, y, z, keywords);
31+
plt::show();
2532

26-
plt::fill_between(x, y, z, keywords);
27-
plt::show();
33+
plt::detail::_interpreter::kill();
34+
return (0);
2835
}

examples/imshow.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//
2+
// g++ -g -Wall -o imshow $(python-config --includes --cflags) imshow.cpp $(python-config --ldflags --embed)
3+
//
4+
15
#define _USE_MATH_DEFINES
26
#include <cmath>
37
#include <iostream>
@@ -26,4 +30,7 @@ int main()
2630
// Show plots
2731
plt::save("imshow.png");
2832
std::cout << "Result saved to 'imshow.png'.\n";
33+
34+
plt::detail::_interpreter::kill();
35+
return 0;
2936
}

examples/lines3d.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//
2+
// g++ -g -Wall -o lines3d $(python-config --includes --cflags) lines3d.cpp $(python-config --ldflags --embed)
3+
//
4+
15
#define _USE_MATH_DEFINES
26
#include "../matplotlibcpp.h"
37
#include <cmath>
@@ -9,7 +13,7 @@ int main()
913
std::vector<double> x, y, z;
1014
double theta, r;
1115
double z_inc = 4.0/99.0; double theta_inc = (8.0 * M_PI)/99.0;
12-
16+
1317
for (double i = 0; i < 100; i += 1) {
1418
theta = -4.0 * M_PI + theta_inc*i;
1519
z.push_back(-2.0 + z_inc*i);
@@ -27,4 +31,7 @@ int main()
2731
plt::set_zlabel("z label"); // set_zlabel rather than just zlabel, in accordance with the Axes3D method
2832
plt::legend();
2933
plt::show();
34+
35+
plt::detail::_interpreter::kill();
36+
return 0;
3037
}

examples/minimal.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
//
2+
// g++ -g -Wall -o minimal $(python-config --includes --cflags) minimal.cpp $(python-config --ldflags --embed)
3+
//
14
#include "../matplotlibcpp.h"
25

36
namespace plt = matplotlibcpp;
47

58
int main() {
69
plt::plot({1,3,2,4});
710
plt::show();
11+
12+
plt::detail::_interpreter::kill();
13+
return 0;
814
}

examples/modern.cpp

+31-24
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
1+
//
2+
// g++ -g -Wall -o modern $(python-config --includes --cflags) modern.cpp $(python-config --ldflags --embed)
3+
//
4+
15
#define _USE_MATH_DEFINES
26
#include <cmath>
37
#include "../matplotlibcpp.h"
48

59
using namespace std;
610
namespace plt = matplotlibcpp;
711

8-
int main()
12+
int main()
913
{
10-
// plot(y) - the x-coordinates are implicitly set to [0,1,...,n)
11-
//plt::plot({1,2,3,4});
12-
13-
// Prepare data for parametric plot.
14-
int n = 5000; // number of data points
15-
vector<double> x(n),y(n);
16-
for(int i=0; i<n; ++i) {
17-
double t = 2*M_PI*i/n;
18-
x.at(i) = 16*sin(t)*sin(t)*sin(t);
19-
y.at(i) = 13*cos(t) - 5*cos(2*t) - 2*cos(3*t) - cos(4*t);
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, y, "r-", x, [](double d) { return 12.5+abs(sin(d)); }, "k-");
26-
27-
//plt::set_aspect(0.5);
28-
plt::set_aspect_equal();
29-
30-
31-
// show plots
32-
plt::show();
14+
// plot(y) - the x-coordinates are implicitly set to [0,1,...,n)
15+
//plt::plot({1,2,3,4});
16+
17+
// Prepare data for parametric plot.
18+
int n = 5000; // number of data points
19+
vector<double> x(n),y(n);
20+
for(int i=0; i<n; ++i) {
21+
double t = 2*M_PI*i/n;
22+
x.at(i) = 16*sin(t)*sin(t)*sin(t);
23+
y.at(i) = 13*cos(t) - 5*cos(2*t) - 2*cos(3*t) - cos(4*t);
24+
}
25+
26+
// plot() takes an arbitrary number of (x,y,format)-triples.
27+
// x must be iterable (that is, anything providing begin(x) and end(x)),
28+
// y must either be callable (providing operator() const) or iterable.
29+
plt::plot(x, y, "r-", x, [](double d) { return 12.5+abs(sin(d)); }, "k-");
30+
31+
//plt::set_aspect(0.5);
32+
plt::set_aspect_equal();
33+
34+
35+
// show plots
36+
plt::show();
37+
38+
plt::detail::_interpreter::kill();
39+
return 0;
3340
}

examples/nonblock.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//
2+
// g++ -g -Wall -o nonblock $(python-config --includes --cflags) nonblock.cpp $(python-config --ldflags --embed)
3+
//
4+
15
#define _USE_MATH_DEFINES
26
#include <cmath>
37
#include "../matplotlibcpp.h"
@@ -43,4 +47,7 @@ int main()
4347

4448
cout << "matplotlibcpp::show() is working in an non-blocking mode" << endl;
4549
getchar();
50+
51+
plt::detail::_interpreter::kill();
52+
return 0;
4653
}

examples/quiver.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//
2+
// g++ -g -Wall -o quiver $(python-config --includes --cflags) quiver.cpp $(python-config --ldflags --embed)
3+
//
4+
15
#include "../matplotlibcpp.h"
26

37
namespace plt = matplotlibcpp;
@@ -17,4 +21,7 @@ int main()
1721

1822
plt::quiver(x, y, u, v);
1923
plt::show();
20-
}
24+
25+
plt::detail::_interpreter::kill();
26+
return 0;
27+
}

examples/spy.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//
2+
// g++ -g -Wall -o spy $(python-config --includes --cflags) spy.cpp $(python-config --ldflags --embed)
3+
//
4+
15
#include "../matplotlibcpp.h"
26

37
#include <iostream>
@@ -26,5 +30,6 @@ int main()
2630
plt::spy(matrix, 5, {{"marker", "o"}});
2731
plt::show();
2832

33+
plt::detail::_interpreter::kill();
2934
return 0;
3035
}

0 commit comments

Comments
 (0)