@@ -71,8 +71,17 @@ namespace matplotlibcpp {
71
71
}
72
72
73
73
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
74
83
_interpreter () {
75
-
84
+
76
85
// optional but recommended
77
86
#if PY_MAJOR_VERSION >= 3
78
87
wchar_t name[] = L" plotting" ;
@@ -83,7 +92,7 @@ namespace matplotlibcpp {
83
92
Py_Initialize ();
84
93
85
94
#ifndef WITHOUT_NUMPY
86
- import_array (); // initialize numpy C-API
95
+ import_numpy (); // initialize numpy C-API
87
96
#endif
88
97
89
98
PyObject* matplotlibname = PyString_FromString (" matplotlib" );
@@ -211,7 +220,7 @@ namespace matplotlibcpp {
211
220
{
212
221
detail::s_backend = name;
213
222
}
214
-
223
+
215
224
bool annotate (std::string annotation, double x, double y)
216
225
{
217
226
PyObject * xy = PyTuple_New (2 );
@@ -227,7 +236,7 @@ namespace matplotlibcpp {
227
236
PyTuple_SetItem (args, 0 , str);
228
237
229
238
PyObject* res = PyObject_Call (detail::_interpreter::get ().s_python_function_annotate , args, kwargs);
230
-
239
+
231
240
Py_DECREF (args);
232
241
Py_DECREF (kwargs);
233
242
@@ -255,7 +264,7 @@ namespace matplotlibcpp {
255
264
PyObject* get_array (const std::vector<Numeric>& v)
256
265
{
257
266
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;
259
268
if (type == NPY_NOTYPE)
260
269
{
261
270
std::vector<double > vd (v.size ());
@@ -390,7 +399,7 @@ namespace matplotlibcpp {
390
399
PyDict_SetItemString (kwargs, " bins" , PyLong_FromLong (bins));
391
400
PyDict_SetItemString (kwargs, " color" , PyString_FromString (color.c_str ()));
392
401
PyDict_SetItemString (kwargs, " alpha" , PyFloat_FromDouble (alpha));
393
-
402
+
394
403
395
404
PyObject* plot_args = PyTuple_New (1 );
396
405
@@ -430,7 +439,7 @@ namespace matplotlibcpp {
430
439
431
440
return res;
432
441
}
433
-
442
+
434
443
template <typename NumericX, typename NumericY>
435
444
bool plot (const std::vector<NumericX>& x, const std::vector<NumericY>& y, const std::string& s = " " )
436
445
{
@@ -769,8 +778,8 @@ namespace matplotlibcpp {
769
778
Py_DECREF (args);
770
779
Py_DECREF (res);
771
780
}
772
-
773
-
781
+
782
+
774
783
inline double * xlim ()
775
784
{
776
785
PyObject* args = PyTuple_New (0 );
@@ -781,14 +790,14 @@ namespace matplotlibcpp {
781
790
double * arr = new double [2 ];
782
791
arr[0 ] = PyFloat_AsDouble (left);
783
792
arr[1 ] = PyFloat_AsDouble (right);
784
-
793
+
785
794
if (!res) throw std::runtime_error (" Call to xlim() failed." );
786
795
787
796
Py_DECREF (res);
788
797
return arr;
789
798
}
790
-
791
-
799
+
800
+
792
801
inline double * ylim ()
793
802
{
794
803
PyObject* args = PyTuple_New (0 );
@@ -799,8 +808,8 @@ namespace matplotlibcpp {
799
808
double * arr = new double [2 ];
800
809
arr[0 ] = PyFloat_AsDouble (left);
801
810
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." );
804
813
805
814
Py_DECREF (res);
806
815
return arr;
0 commit comments