@@ -45,6 +45,7 @@ struct _interpreter {
45
45
PyObject *s_python_function_fill;
46
46
PyObject *s_python_function_fill_between;
47
47
PyObject *s_python_function_hist;
48
+ PyObject *s_python_function_imshow;
48
49
PyObject *s_python_function_scatter;
49
50
PyObject *s_python_function_subplot;
50
51
PyObject *s_python_function_legend;
@@ -165,6 +166,9 @@ struct _interpreter {
165
166
s_python_function_fill = PyObject_GetAttrString (pymod, " fill" );
166
167
s_python_function_fill_between = PyObject_GetAttrString (pymod, " fill_between" );
167
168
s_python_function_hist = PyObject_GetAttrString (pymod," hist" );
169
+ #ifndef WITHOUT_NUMPY
170
+ s_python_function_imshow = PyObject_GetAttrString (pymod, " imshow" );
171
+ #endif
168
172
s_python_function_scatter = PyObject_GetAttrString (pymod," scatter" );
169
173
s_python_function_subplot = PyObject_GetAttrString (pymod, " subplot" );
170
174
s_python_function_legend = PyObject_GetAttrString (pymod, " legend" );
@@ -211,6 +215,9 @@ struct _interpreter {
211
215
|| !s_python_function_axis
212
216
|| !s_python_function_xlabel
213
217
|| !s_python_function_ylabel
218
+ #ifndef WITHOUT_NUMPY
219
+ || !s_python_function_imshow
220
+ #endif
214
221
|| !s_python_function_grid
215
222
|| !s_python_function_xlim
216
223
|| !s_python_function_ion
@@ -246,6 +253,9 @@ struct _interpreter {
246
253
|| !PyFunction_Check (s_python_function_legend)
247
254
|| !PyFunction_Check (s_python_function_annotate)
248
255
|| !PyFunction_Check (s_python_function_ylim)
256
+ #ifndef WITHOUT_NUMPY
257
+ || !PyFunction_Check (s_python_function_imshow)
258
+ #endif
249
259
|| !PyFunction_Check (s_python_function_title)
250
260
|| !PyFunction_Check (s_python_function_axis)
251
261
|| !PyFunction_Check (s_python_function_xlabel)
@@ -623,6 +633,47 @@ bool hist(const std::vector<Numeric>& y, long bins=10,std::string color="b",
623
633
return res;
624
634
}
625
635
636
+ #ifndef WITHOUT_NUMPY
637
+ namespace internal {
638
+ void imshow (void *ptr, const NPY_TYPES type, const int rows, const int columns, const int colors, const std::map<std::string, std::string> &keywords)
639
+ {
640
+ assert (type == NPY_UINT8 || type == NPY_FLOAT);
641
+ assert (colors == 1 || colors == 3 || colors == 4 );
642
+
643
+ detail::_interpreter::get (); // interpreter needs to be initialized for the numpy commands to work
644
+
645
+ // construct args
646
+ npy_intp dims[3 ] = { rows, columns, colors };
647
+ PyObject *args = PyTuple_New (1 );
648
+ PyTuple_SetItem (args, 0 , PyArray_SimpleNewFromData (3 , dims, type, ptr));
649
+
650
+ // construct keyword args
651
+ PyObject* kwargs = PyDict_New ();
652
+ for (std::map<std::string, std::string>::const_iterator it = keywords.begin (); it != keywords.end (); ++it)
653
+ {
654
+ PyDict_SetItemString (kwargs, it->first .c_str (), PyUnicode_FromString (it->second .c_str ()));
655
+ }
656
+
657
+ PyObject *res = PyObject_Call (detail::_interpreter::get ().s_python_function_imshow , args, kwargs);
658
+ Py_DECREF (args);
659
+ Py_DECREF (kwargs);
660
+ if (!res)
661
+ throw std::runtime_error (" Call to imshow() failed" );
662
+ Py_DECREF (res);
663
+ }
664
+ }
665
+
666
+ void imshow (const unsigned char *ptr, const int rows, const int columns, const int colors, const std::map<std::string, std::string> &keywords = {})
667
+ {
668
+ internal::imshow ((void *) ptr, NPY_UINT8, rows, columns, colors, keywords);
669
+ }
670
+
671
+ void imshow (const float *ptr, const int rows, const int columns, const int colors, const std::map<std::string, std::string> &keywords = {})
672
+ {
673
+ internal::imshow ((void *) ptr, NPY_FLOAT, rows, columns, colors, keywords);
674
+ }
675
+ #endif
676
+
626
677
template <typename NumericX, typename NumericY>
627
678
bool scatter (const std::vector<NumericX>& x,
628
679
const std::vector<NumericY>& y,
0 commit comments