@@ -92,6 +92,7 @@ struct _interpreter {
9292 PyObject *s_python_function_suptitle;
9393 PyObject *s_python_function_bar;
9494 PyObject *s_python_function_subplots_adjust;
95+ PyObject *s_python_function_imshow;
9596
9697 /* For now, _interpreter is implemented as a singleton since its currently not
9798 possible to have multiple independent embedded python interpreters without
@@ -226,6 +227,7 @@ struct _interpreter {
226227 s_python_function_bar = PyObject_GetAttrString (pymod, " bar" );
227228 s_python_function_subplots_adjust =
228229 PyObject_GetAttrString (pymod, " subplots_adjust" );
230+ s_python_function_imshow = PyObject_GetAttrString (pymod, " imshow" );
229231
230232 if (!s_python_function_show || !s_python_function_close ||
231233 !s_python_function_draw || !s_python_function_pause ||
@@ -248,7 +250,7 @@ struct _interpreter {
248250 !s_python_function_stem || !s_python_function_xkcd ||
249251 !s_python_function_text || !s_python_function_suptitle ||
250252 !s_python_function_bar || !s_python_function_subplots_adjust ||
251- !s_python_function_spy) {
253+ !s_python_function_spy || !s_python_function_imshow ) {
252254 throw std::runtime_error (" Couldn't find required function!" );
253255 }
254256
@@ -292,7 +294,9 @@ struct _interpreter {
292294 !PyFunction_Check (s_python_function_text) ||
293295 !PyFunction_Check (s_python_function_suptitle) ||
294296 !PyFunction_Check (s_python_function_bar) ||
295- !PyFunction_Check (s_python_function_subplots_adjust)) {
297+ !PyFunction_Check (s_python_function_subplots_adjust) ||
298+ !PyFunction_Check (s_python_function_imshow)
299+ ) {
296300 throw std::runtime_error (
297301 " Python object is unexpectedly not a PyFunction." );
298302 }
@@ -660,6 +664,29 @@ bool semilogy(const VectorY &y,
660664 return semilogy (x, y, " " , keywords);
661665}
662666
667+ template <typename Matrix>
668+ void imshow (const Matrix& X, const std::map<std::string, std::string> &keywords = {}) {
669+ PyObject *Xarray = get_2darray (X);
670+
671+ PyObject *kwargs = PyDict_New ();
672+ for (std::map<std::string, std::string>::const_iterator it = keywords.begin ();
673+ it != keywords.end (); ++it) {
674+ PyDict_SetItemString (kwargs, it->first .c_str (),
675+ PyUnicode_FromString (it->second .c_str ()));
676+ }
677+
678+ PyObject *plot_args = PyTuple_New (1 );
679+ PyTuple_SetItem (plot_args, 0 , Xarray);
680+
681+ PyObject *res = PyObject_Call (
682+ detail::_interpreter::get ().s_python_function_imshow , plot_args, kwargs);
683+
684+ Py_DECREF (plot_args);
685+ Py_DECREF (kwargs);
686+ if (res)
687+ Py_DECREF (res);
688+ }
689+
663690// @brief plot_surface for datapoints (x_ij, y_ij, z_ij) with i,j = 0..n
664691// @param x The x values of the datapoints in a matrix
665692// @param y The y values of the datapoints in a matrix
0 commit comments