@@ -87,6 +87,7 @@ struct _interpreter {
87
87
PyObject *s_python_function_text;
88
88
PyObject *s_python_function_suptitle;
89
89
PyObject *s_python_function_bar;
90
+ PyObject *s_python_function_colorbar;
90
91
PyObject *s_python_function_subplots_adjust;
91
92
92
93
@@ -205,7 +206,7 @@ struct _interpreter {
205
206
s_python_function_ylabel = safe_import (pymod, " ylabel" );
206
207
s_python_function_xticks = safe_import (pymod, " xticks" );
207
208
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" );
209
210
s_python_function_grid = safe_import (pymod, " grid" );
210
211
s_python_function_xlim = safe_import (pymod, " xlim" );
211
212
s_python_function_ion = safe_import (pymod, " ion" );
@@ -220,11 +221,11 @@ struct _interpreter {
220
221
s_python_function_text = safe_import (pymod, " text" );
221
222
s_python_function_suptitle = safe_import (pymod, " suptitle" );
222
223
s_python_function_bar = safe_import (pymod," bar" );
224
+ s_python_function_colorbar = PyObject_GetAttrString (pymod, " colorbar" );
223
225
s_python_function_subplots_adjust = safe_import (pymod," subplots_adjust" );
224
226
#ifndef WITHOUT_NUMPY
225
227
s_python_function_imshow = safe_import (pymod, " imshow" );
226
228
#endif
227
-
228
229
s_python_empty_tuple = PyTuple_New (0 );
229
230
}
230
231
@@ -589,7 +590,7 @@ bool hist(const std::vector<Numeric>& y, long bins=10,std::string color="b",
589
590
590
591
#ifndef WITHOUT_NUMPY
591
592
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 )
593
594
{
594
595
assert (type == NPY_UINT8 || type == NPY_FLOAT);
595
596
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",
613
614
Py_DECREF (kwargs);
614
615
if (!res)
615
616
throw std::runtime_error (" Call to imshow() failed" );
616
- Py_DECREF (res);
617
+ if (out)
618
+ *out = res;
619
+ else
620
+ Py_DECREF (res);
617
621
}
618
622
}
619
623
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 )
621
625
{
622
- internal::imshow ((void *) ptr, NPY_UINT8, rows, columns, colors, keywords);
626
+ internal::imshow ((void *) ptr, NPY_UINT8, rows, columns, colors, keywords, out );
623
627
}
624
628
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 )
626
630
{
627
- internal::imshow ((void *) ptr, NPY_FLOAT, rows, columns, colors, keywords);
631
+ internal::imshow ((void *) ptr, NPY_FLOAT, rows, columns, colors, keywords, out );
628
632
}
629
633
630
634
#ifdef WITH_OPENCV
@@ -1136,6 +1140,27 @@ void text(Numeric x, Numeric y, const std::string& s = "")
1136
1140
Py_DECREF (res);
1137
1141
}
1138
1142
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
+
1139
1164
1140
1165
inline long figure (long number = -1 )
1141
1166
{
0 commit comments