@@ -92,6 +92,7 @@ struct _interpreter {
92
92
PyObject *s_python_function_suptitle;
93
93
PyObject *s_python_function_bar;
94
94
PyObject *s_python_function_subplots_adjust;
95
+ PyObject *s_python_function_imshow;
95
96
96
97
/* For now, _interpreter is implemented as a singleton since its currently not
97
98
possible to have multiple independent embedded python interpreters without
@@ -226,6 +227,7 @@ struct _interpreter {
226
227
s_python_function_bar = PyObject_GetAttrString (pymod, " bar" );
227
228
s_python_function_subplots_adjust =
228
229
PyObject_GetAttrString (pymod, " subplots_adjust" );
230
+ s_python_function_imshow = PyObject_GetAttrString (pymod, " imshow" );
229
231
230
232
if (!s_python_function_show || !s_python_function_close ||
231
233
!s_python_function_draw || !s_python_function_pause ||
@@ -248,7 +250,7 @@ struct _interpreter {
248
250
!s_python_function_stem || !s_python_function_xkcd ||
249
251
!s_python_function_text || !s_python_function_suptitle ||
250
252
!s_python_function_bar || !s_python_function_subplots_adjust ||
251
- !s_python_function_spy) {
253
+ !s_python_function_spy || !s_python_function_imshow ) {
252
254
throw std::runtime_error (" Couldn't find required function!" );
253
255
}
254
256
@@ -292,7 +294,9 @@ struct _interpreter {
292
294
!PyFunction_Check (s_python_function_text) ||
293
295
!PyFunction_Check (s_python_function_suptitle) ||
294
296
!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
+ ) {
296
300
throw std::runtime_error (
297
301
" Python object is unexpectedly not a PyFunction." );
298
302
}
@@ -660,6 +664,29 @@ bool semilogy(const VectorY &y,
660
664
return semilogy (x, y, " " , keywords);
661
665
}
662
666
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
+
663
690
// @brief plot_surface for datapoints (x_ij, y_ij, z_ij) with i,j = 0..n
664
691
// @param x The x values of the datapoints in a matrix
665
692
// @param y The y values of the datapoints in a matrix
0 commit comments