Skip to content

Commit b203af7

Browse files
committed
examples: fix segfault on exit with python3
1 parent ef0383f commit b203af7

19 files changed

+193
-71
lines changed

examples/animation.cpp

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//
2+
// g++ -g -Wall -o animation $(python-config --includes) 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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//
2+
// g++ -g -Wall -o bar $(python-config --includes) 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

Lines changed: 10 additions & 2 deletions
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) 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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//
2+
// g++ -g -Wall -o colorbar $(python-config --includes) 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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//
2+
// g++ -g -Wall -o contour $(python-config --includes) 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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//
2+
// g++ -g -Wall -o fill $(python-config --includes) 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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//
2+
// g++ -g -Wall -o fill_inbetween $(python-config --includes) 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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//
2+
// g++ -g -Wall -o imshow $(python-config --includes) 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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//
2+
// g++ -g -Wall -o lines3d $(python-config --includes) 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
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy