8000 fix segfault on exit with python3 by amadeus84 · Pull Request #301 · lava/matplotlib-cpp · GitHub
[go: up one dir, main page]

Skip to content

fix segfault on exit with python3 #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
examples: fix segfault on exit with python3
  • Loading branch information
amadeus84 committed Mar 11, 2022
commit b203af70d796d00f3d5ed99f3f61281aad68c1cd
59 changes: 33 additions & 26 deletions examples/animation.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// g++ -g -Wall -o animation $(python-config --includes) animation.cpp $(python-config --ldflags --embed)
//

#define _USE_MATH_DEFINES
#include <cmath>
#include "../matplotlibcpp.h"
Expand All @@ -6,31 +10,34 @@ namespace plt = matplotlibcpp;

int main()
{
int n = 1000;
std::vector<double> x, y, z;

for(int i=0; i<n; i++) {
x.push_back(i*i);
y.push_back(sin(2*M_PI*i/360.0));
z.push_back(log(i));

if (i % 10 == 0) {
// Clear previous plot
plt::clf();
// Plot line from given x and y data. Color is selected automatically.
plt::plot(x, y);
// Plot a line whose name will show up as "log(x)" in the legend.
plt::named_plot("log(x)", x, z);

// Set x-axis to interval [0,1000000]
plt::xlim(0, n*n);

// Add graph title
plt::title("Sample figure");
// Enable legend.
plt::legend();
// Display plot continuously
plt::pause(0.01);
}
int n = 1000;
std::vector<double> x, y, z;

for(int i=0; i<n; i++) {
x.push_back(i*i);
y.push_back(sin(2*M_PI*i/360.0));
z.push_back(log(i));

if (i % 10 == 0) {
// Clear previous plot
plt::clf();
// Plot line from given x and y data. Color is selected automatically.
plt::plot(x, y);
// Plot a line whose name will show up as "log(x)" in the legend.
plt::named_plot("log(x)", x, z);

// Set x-axis to interval [0,1000000]
plt::xlim(0, n*n);

// Add graph title
plt::title("Sample figure");
// Enable legend.
plt::legend();
// Display plot continuously
plt::pause(0.01);
}
}

plt::detail::_interpreter::kill();
return 0;
}
5 changes: 5 additions & 0 deletions examples/bar.cpp
8000
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// g++ -g -Wall -o bar $(python-config --includes) bar.cpp $(python-config --ldflags --embed)
//

#define _USE_MATH_DEFINES

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

plt::detail::_interpreter::kill();
return (0);
}
12 changes: 10 additions & 2 deletions examples/basic.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
//
// g++ -g -Wall -o basic -I/usr/include/python3.9 basic.cpp -lpython3.9
// g++ -g -Wall -o basic $(python-config --includes) basic.cpp $(python-config --ldflags --embed)
//

#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>
#include "../matplotlibcpp.h"

namespace plt = matplotlibcpp;

int main()
int main()
{
// Prepare data.
int n = 5000;
Expand All @@ -15,7 +20,7 @@ int main()
y.at(i) = sin(2*M_PI*i/360.0);
z.at(i) = log(i);
}

// Set the size of output image = 1200x780 pixels
plt::figure_size(1200, 780);

Expand All @@ -41,4 +46,7 @@ int main()
const char* filename = "./basic.png";
std::cout << "Saving result to " << filename << std::endl;;
plt::save(filename);

plt::detail::_interpreter::kill();
return 0;
}
Binary file modified examples/basic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions examples/colorbar.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// g++ -g -Wall -o colorbar $(python-config --includes) colorbar.cpp $(python-config --ldflags --embed)
//

#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
Expand Down Expand Up @@ -29,4 +33,7 @@ int main()
plt::show();
plt::close();
Py_DECREF(mat);

plt::detail::_interpreter::kill();
return 0;
}
7 changes: 7 additions & 0 deletions examples/contour.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// g++ -g -Wall -o contour $(python-config --includes) contour.cpp $(python-config --ldflags --embed)
//

#include "../matplotlibcpp.h"

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

plt::contour(x, y, z);
plt::show();

plt::detail::_interpreter::kill();
return (0);
}
7 changes: 7 additions & 0 deletions examples/fill.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// g++ -g -Wall -o fill $(python-config --includes) fill.cpp $(python-config --ldflags --embed)
//

#define _USE_MATH_DEFINES
#include "../matplotlibcpp.h"
#include <cmath>
Expand Down Expand Up @@ -32,4 +36,7 @@ int main() {
plt::fill(x1, y1, {});
}
plt::show();

plt::detail::_interpreter::kill();
return (0);
}
39 changes: 23 additions & 16 deletions examples/fill_inbetween.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// g++ -g -Wall -o fill_inbetween $(python-config --includes) fill_inbetween.cpp $(python-config --ldflags --embed)
//

#define _USE_MATH_DEFINES
#include "../matplotlibcpp.h"
#include <cmath>
Expand All @@ -7,22 +11,25 @@ using namespace std;
namespace plt = matplotlibcpp;

int main() {
// Prepare data.
int n = 5000;
std::vector<double> x(n), y(n), z(n), w(n, 2);
for (int i = 0; i < n; ++i) {
x.at(i) = i * i;
y.at(i) = sin(2 * M_PI * i / 360.0);
z.at(i) = log(i);
}
// Prepare data.
int n = 5000;
std::vector<double> x(n), y(n), z(n), w(n, 2);
for (int i = 0; i < n; ++i) {
x.at(i) = i * i;
y.at(i) = sin(2 * M_PI * i / 360.0);
z.at(i) = log(i);
}

// Prepare keywords to pass to PolyCollection. See
// https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.fill_between.html
std::map<string, string> keywords;
keywords["alpha"] = "0.4";
keywords["color"] = "grey";
keywords["hatch"] = "-";

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

plt::fill_between(x, y, z, keywords);
plt::show();
plt::detail::_interpreter::kill();
return (0);
}
7 changes: 7 additions & 0 deletions examples/imshow.cpp
10000
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// g++ -g -Wall -o imshow $(python-config --includes) imshow.cpp $(python-config --ldflags --embed)
//

#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
Expand Down Expand Up @@ -26,4 +30,7 @@ int main()
// Show plots
plt::save("imshow.png");
std::cout << "Result saved to 'imshow.png'.\n";

plt::detail::_interpreter::kill();
return 0;
}
9 changes: 8 additions & 1 deletion examples/lines3d.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// g++ -g -Wall -o lines3d $(python-config --includes) lines3d.cpp $(python-config --ldflags --embed)
//

#define _USE_MATH_DEFINES
#include "../matplotlibcpp.h"
#include <cmath>
Expand All @@ -9,7 +13,7 @@ int main()
std::vector<double> x, y, z;
double theta, r;
double z_inc = 4.0/99.0; double theta_inc = (8.0 * M_PI)/99.0;

for (double i = 0; i < 100; i += 1) {
theta = -4.0 * M_PI + theta_inc*i;
z.push_back(-2.0 + z_inc*i);
Expand All @@ -27,4 +31,7 @@ int main()
plt::set_zlabel("z label"); // set_zlabel rather than just zlabel, in accordance with the Axes3D method
plt::legend();
plt::show();

plt::detail::_interpreter::kill();
return 0;
}
6 changes: 6 additions & 0 deletions examples/minimal.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
//
// g++ -g -Wall -o minimal $(python-config --includes) minimal.cpp $(python-config --ldflags --embed)
//
#include "../matplotlibcpp.h"

namespace plt = matplotlibcpp;

int main() {
plt::plot({1,3,2,4});
plt::show();

plt::detail::_interpreter::kill();
return 0;
}
55 changes: 31 additions & 24 deletions examples/modern.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
//
// g++ -g -Wall -o modern $(python-config --includes) modern.cpp $(python-config --ldflags --embed)
//

#define _USE_MATH_DEFINES
#include <cmath>
#include "../matplotlibcpp.h"

using namespace std;
namespace plt = matplotlibcpp;

int main()
int main()
{
// plot(y) - the x-coordinates are implicitly set to [0,1,...,n)
//plt::plot({1,2,3,4});

// Prepare data for parametric plot.
int n = 5000; // number of data points
vector<double> x(n),y(n);
for(int i=0; i<n; ++i) {
double t = 2*M_PI*i/n;
x.at(i) = 16*sin(t)*sin(t)*sin(t);
y.at(i) = 13*cos(t) - 5*cos(2*t) - 2*cos(3*t) - cos(4*t);
}

// plot() takes an arbitrary number of (x,y,format)-triples.
// x must be iterable (that is, anything providing begin(x) and end(x)),
// y must either be callable (providing operator() const) or iterable.
plt::plot(x, y, "r-", x, [](double d) { return 12.5+abs(sin(d)); }, "k-");

//plt::set_aspect(0.5);
plt::set_aspect_equal();


// show plots
plt::show();
// plot(y) - the x-coordinates are implicitly set to [0,1,...,n)
//plt::plot({1,2,3,4});

// Prepare data for parametric plot.
int n = 5000; // number of data points
vector<double> x(n),y(n);
for(int i=0; i<n; ++i) {
double t = 2*M_PI*i/n;
x.at(i) = 16*sin(t)*sin(t)*sin(t);
y.at(i) = 13*cos(t) - 5*cos(2*t) - 2*cos(3*t) - cos(4*t);
}

// plot() takes an arbitrary number of (x,y,format)-triples.
// x must be iterable (that is, anything providing begin(x) and end(x)),
// y must either be callable (providing operator() const) or iterable.
plt::plot(x, y, "r-", x, [](double d) { return 12.5+abs(sin(d)); }, "k-");

//plt::set_aspect(0.5);
plt::set_aspect_equal();


// show plots
plt::show();

plt::detail::_interpreter::kill();
return 0;
}
7 changes: 7 additions & 0 deletions examples/nonblock.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// g++ -g -Wall -o nonblock $(python-config --includes) nonblock.cpp $(python-config --ldflags --embed)
//

#define _USE_MATH_DEFINES
#include <cmath>
#include "../matplotlibcpp.h"
Expand Down Expand Up @@ -43,4 +47,7 @@ int main()

cout << "matplotlibcpp::show() is working in an non-blocking mode" << endl;< 1241 /span>
getchar();

plt::detail::_interpreter::kill();
return 0;
}
9 changes: 8 additions & 1 deletion examples/quiver.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// g++ -g -Wall -o quiver $(python-config --includes) quiver.cpp $(python-config --ldflags --embed)
//

#include "../matplotlibcpp.h"

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

plt::quiver(x, y, u, v);
plt::show();
}

plt::detail::_interpreter::kill();
return 0;
}
5 changes: 5 additions & 0 deletions examples/spy.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//
// g++ -g -Wall -o spy $(python-config --includes) spy.cpp $(python-config --ldflags --embed)
//

#include "../matplotlibcpp.h"

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

plt::detail::_interpreter::kill();
return 0;
}
Loading
0