@@ -87,6 +87,7 @@ struct _interpreter {
8787 PyObject *s_python_function_text;
8888 PyObject *s_python_function_suptitle;
8989 PyObject *s_python_function_bar;
90+ PyObject *s_python_function_colorbar;
9091 PyObject *s_python_function_subplots_adjust;
9192
9293
@@ -205,7 +206,7 @@ struct _interpreter {
205206 s_python_function_ylabel = safe_import (pymod, " ylabel" );
206207 s_python_function_xticks = safe_import (pymod, " xticks" );
207208 s_python_function_yticks = safe_import (pymod, " yticks" );
208- s_python_function_tick_params = safe_import (pymod, " tick_params" );
209+ s_python_function_tick_params = safe_import (pymod, " tick_params" );
209210 s_python_function_grid = safe_import (pymod, " grid" );
210211 s_python_function_xlim = safe_import (pymod, " xlim" );
211212 s_python_function_ion = safe_import (pymod, " ion" );
@@ -220,11 +221,11 @@ struct _interpreter {
220221 s_python_function_text = safe_import (pymod, " text" );
221222 s_python_function_suptitle = safe_import (pymod, " suptitle" );
222223 s_python_function_bar = safe_import (pymod," bar" );
224+ s_python_function_colorbar = PyObject_GetAttrString (pymod, " colorbar" );
223225 s_python_function_subplots_adjust = safe_import (pymod," subplots_adjust" );
224226#ifndef WITHOUT_NUMPY
225227 s_python_function_imshow = safe_import (pymod, " imshow" );
226228#endif
227-
228229 s_python_empty_tuple = PyTuple_New (0 );
229230 }
230231
@@ -589,7 +590,7 @@ bool hist(const std::vector<Numeric>& y, long bins=10,std::string color="b",
589590
590591#ifndef WITHOUT_NUMPY
591592 namespace internal {
592- inline 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)
593+ inline 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, PyObject** out )
593594 {
594595 assert (type == NPY_UINT8 || type == NPY_FLOAT);
595596 assert (colors == 1 || colors == 3 || colors == 4 );
@@ -613,18 +614,21 @@ bool hist(const std::vector<Numeric>& y, long bins=10,std::string color="b",
613614 Py_DECREF (kwargs);
614615 if (!res)
615616 throw std::runtime_error (" Call to imshow() failed" );
616- Py_DECREF (res);
617+ if (out)
618+ *out = res;
619+ else
620+ Py_DECREF (res);
617621 }
618622 }
619623
620- inline void imshow (const unsigned char *ptr, const int rows, const int columns, const int colors, const std::map<std::string, std::string> &keywords = {})
624+ inline void imshow (const unsigned char *ptr, const int rows, const int columns, const int colors, const std::map<std::string, std::string> &keywords = {}, PyObject** out = nullptr )
621625 {
622- internal::imshow ((void *) ptr, NPY_UINT8, rows, columns, colors, keywords);
626+ internal::imshow ((void *) ptr, NPY_UINT8, rows, columns, colors, keywords, out );
623627 }
624628
625- inline void imshow (const float *ptr, const int rows, const int columns, const int colors, const std::map<std::string, std::string> &keywords = {})
629+ inline void imshow (const float *ptr, const int rows, const int columns, const int colors, const std::map<std::string, std::string> &keywords = {}, PyObject** out = nullptr )
626630 {
627- internal::imshow ((void *) ptr, NPY_FLOAT, rows, columns, colors, keywords);
631+ internal::imshow ((void *) ptr, NPY_FLOAT, rows, columns, colors, keywords, out );
628632 }
629633
630634#ifdef WITH_OPENCV
@@ -1136,6 +1140,27 @@ void text(Numeric x, Numeric y, const std::string& s = "")
11361140 Py_DECREF (res);
11371141}
11381142
1143+ void colorbar (PyObject* mappable = NULL , const std::map<std::string, float >& keywords = {})
1144+ {
1145+ if (mappable == NULL )
1146+ throw std::runtime_error (" Must call colorbar with PyObject* returned from an image, contour, surface, etc." );
1147+ PyObject* args = PyTuple_New (1 );
1148+ PyTuple_SetItem (args, 0 , mappable);
1149+
1150+ PyObject* kwargs = PyDict_New ();
1151+ for (std::map<std::string, float >::const_iterator it = keywords.begin (); it != keywords.end (); ++it)
1152+ {
1153+ PyDict_SetItemString (kwargs, it->first .c_str (), PyFloat_FromDouble (it->second ));
1154+ }
1155+
1156+ PyObject* res = PyObject_Call (detail::_interpreter::get ().s_python_function_colorbar , args, kwargs);
1157+ if (!res) throw std::runtime_error (" Call to colorbar() failed." );
1158+
1159+ Py_DECREF (args);
1160+ Py_DECREF (kwargs);
1161+ Py_DECREF (res);
1162+ }
1163+
11391164
11401165inline long figure (long number = -1 )
11411166{
0 commit comments