8000 Fixes #30. Creates a function called import_numpy defined as an int i… · konanrobot/matplotlib-cpp@6aa4bc6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6aa4bc6

Browse files
raldridge11kartikmohta
authored andcommitted
Fixes lava#30. Creates a function called import_numpy defined as an int if the Python version 3 and void if the Python version is 2.
1 parent 1d79940 commit 6aa4bc6

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

matplotlibcpp.h

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,17 @@ namespace matplotlibcpp {
7171
}
7272

7373
private:
74+
#if PY_MAJOR_VERSION >= 3
75+
int import_numpy() {
76+
import_array(); // initialize C-API
77+
}
78+
#else
79+
void import_numpy() {
80+
import_array(); // initialize C-API
81+
}
82+
#endif
7483
_interpreter() {
75-
84+
7685
// optional but recommended
7786
#if PY_MAJOR_VERSION >= 3
7887
wchar_t name[] = L"plotting";
@@ -83,7 +92,7 @@ namespace matplotlibcpp {
8392
Py_Initialize();
8493

8594
#ifndef WITHOUT_NUMPY
86-
import_array(); // initialize numpy C-API
95+
import_numpy(); // initialize numpy C-API
8796
#endif
8897

8998
PyObject* matplotlibname = PyString_FromString("matplotlib");
@@ -211,7 +220,7 @@ namespace matplotlibcpp {
211220
{
212221
detail::s_backend = name;
213222
}
214-
223+
215224
bool annotate(std::string annotation, double x, double y)
216225
{
217226
PyObject * xy = PyTuple_New(2);
@@ -227,7 +236,7 @@ namespace matplotlibcpp {
227236
PyTuple_SetItem(args, 0, str);
228237

229238
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_annotate, args, kwargs);
230-
239+
231240
Py_DECREF(args);
232241
Py_DECREF(kwargs);
233242

@@ -255,7 +264,7 @@ namespace matplotlibcpp {
255264
PyObject* get_array(const std::vector<Numeric>& v)
256265
{
257266
detail::_interpreter::get(); //interpreter needs to be initialized for the numpy commands to work
258-
NPY_TYPES type = select_npy_type<Numeric>::type;
267+
NPY_TYPES type = select_npy_type<Numeric>::type;
259268
if (type == NPY_NOTYPE)
260269
{
261270
std::vector<double> vd(v.size());
@@ -390,7 +399,7 @@ namespace matplotlibcpp {
390399
PyDict_SetItemString(kwargs, "bins", PyLong_FromLong(bins));
391400
PyDict_SetItemString(kwargs, "color", PyString_FromString(color.c_str()));
392401
PyDict_SetItemString(kwargs, "alpha", PyFloat_FromDouble(alpha));
393-
402+
394403

395404
PyObject* plot_args = PyTuple_New(1);
396405

@@ -430,7 +439,7 @@ namespace matplotlibcpp {
430439

431440
return res;
432441
}
433-
442+
434443
template<typename NumericX, typename NumericY>
435444
bool plot(const std::vector<NumericX>& x, const std::vector<NumericY>& y, const std::string& s = "")
436445
{
@@ -769,8 +778,8 @@ namespace matplotlibcpp {
769778
Py_DECREF(args);
770779
Py_DECREF(res);
771780
}
772-
773-
781+
782+
774783
inline double* xlim()
775784
{
776785
PyObject* args = PyTuple_New(0);
@@ -781,14 +790,14 @@ namespace matplotlibcpp {
781790
double* arr = new double[2];
782791
arr[0] = PyFloat_AsDouble(left);
783792
arr[1] = PyFloat_AsDouble(right);
784-
793+
785794
if(!res) throw std::runtime_error("Call to xlim() failed.");
786795

787796
Py_DECREF(res);
788797
return arr;
789798
}
790-
791-
799+
800+
792801
inline double* ylim()
793802
{
794803
PyObject* args = PyTuple_New(0);
@@ -799,8 +808,8 @@ namespace matplotlibcpp {
799808
double* arr = new double[2];
800809
arr[0] = PyFloat_AsDouble(left);
801810
arr[1] = PyFloat_AsDouble(right);
802-
803-
if(!res) throw std::runtime_error("Call to ylim() failed.");
811+
812+
if(!res) throw std::runtime_error("Call to ylim() failed.");
804813

805814
Py_DECREF(res);
806815
return arr;

0 commit comments

Comments
 (0)
0