Skip to content

Commit 0c327b1

Browse files
alexdewartkphd
authored andcommitted
Add basic support for imshow() function
Requires numpy
1 parent 94c0215 commit 0c327b1

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

matplotlibcpp.h

+51
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ struct _interpreter {
4545
PyObject *s_python_function_fill;
4646
PyObject *s_python_function_fill_between;
4747
PyObject *s_python_function_hist;
48+
PyObject *s_python_function_imshow;
4849
PyObject *s_python_function_scatter;
4950
PyObject *s_python_function_subplot;
5051
PyObject *s_python_function_legend;
@@ -165,6 +166,9 @@ struct _interpreter {
165166
s_python_function_fill = PyObject_GetAttrString(pymod, "fill");
166167
s_python_function_fill_between = PyObject_GetAttrString(pymod, "fill_between");
167168
s_python_function_hist = PyObject_GetAttrString(pymod,"hist");
169+
#ifndef WITHOUT_NUMPY
170+
s_python_function_imshow = PyObject_GetAttrString(pymod, "imshow");
171+
#endif
168172
s_python_function_scatter = PyObject_GetAttrString(pymod,"scatter");
169173
s_python_function_subplot = PyObject_GetAttrString(pymod, "subplot");
170174
s_python_function_legend = PyObject_GetAttrString(pymod, "legend");
@@ -211,6 +215,9 @@ struct _interpreter {
211215
|| !s_python_function_axis
212216
|| !s_python_function_xlabel
213217
|| !s_python_function_ylabel
218+
#ifndef WITHOUT_NUMPY
219+
|| !s_python_function_imshow
220+
#endif
214221
|| !s_python_function_grid
215222
|| !s_python_function_xlim
216223
|| !s_python_function_ion
@@ -246,6 +253,9 @@ struct _interpreter {
246253
|| !PyFunction_Check(s_python_function_legend)
247254
|| !PyFunction_Check(s_python_function_annotate)
248255
|| !PyFunction_Check(s_python_function_ylim)
256+
#ifndef WITHOUT_NUMPY
257+
|| !PyFunction_Check(s_python_function_imshow)
258+
#endif
249259
|| !PyFunction_Check(s_python_function_title)
250260
|| !PyFunction_Check(s_python_function_axis)
251261
|| !PyFunction_Check(s_python_function_xlabel)
@@ -623,6 +633,47 @@ bool hist(const std::vector<Numeric>& y, long bins=10,std::string color="b",
623633
return res;
624634
}
625635

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+
626677
template<typename NumericX, typename NumericY>
627678
bool scatter(const std::vector<NumericX>& x,
628679
const std::vector<NumericY>& y,

0 commit comments

Comments
 (0)