|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "_python.h" |
| 4 | +#include "interpreter.h" |
| 5 | +#include "_numpy.h" |
| 6 | +#include "helpers.h" |
| 7 | + |
| 8 | +namespace matplotlibcpp { |
| 9 | +template<typename Numeric> |
| 10 | +bool imshow(const std::vector<Numeric>& values, size_t width, size_t height, const std::string& cmap = "") |
| 11 | +{ |
| 12 | + PyObject* matrix = get_array(values, width, height); |
| 13 | + |
| 14 | + PyObject* colormap; |
| 15 | + if (cmap == "") |
| 16 | + { |
| 17 | + colormap = Py_None; |
| 18 | + } |
| 19 | + else |
| 20 | + { |
| 21 | + colormap = PyString_FromString(cmap.c_str()); |
| 22 | + } |
| 23 | + |
| 24 | + PyObject* plot_args = PyTuple_New(2); |
| 25 | + PyTuple_SetItem(plot_args, 0, matrix); |
| 26 | + PyTuple_SetItem(plot_args, 1, colormap); |
| 27 | + |
| 28 | + PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_imshow, plot_args); |
| 29 | + |
| 30 | + Py_DECREF(plot_args); |
| 31 | + if(res != nullptr) Py_DECREF(res); |
| 32 | + |
| 33 | + return res != nullptr; |
| 34 | +} |
| 35 | + |
| 36 | +template<typename Numeric> |
| 37 | +bool named_imshow(const std::string& name, const std::vector<Numeric>& values, size_t width, size_t height, const std::string& cmap = "") |
| 38 | +{ |
| 39 | + PyObject* kwargs = PyDict_New(); |
| 40 | + PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str())); |
| 41 | + |
| 42 | + PyObject* matrix = get_array(values, width, height); |
| 43 | + |
| 44 | + PyObject* colormap; |
| 45 | + if (cmap == "") |
| 46 | + { |
| 47 | + colormap = Py_None; |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + colormap = PyString_FromString(cmap.c_str()); |
| 52 | + } |
| 53 | + |
| 54 | + PyObject* plot_args = PyTuple_New(2); |
| 55 | + PyTuple_SetItem(plot_args, 0, matrix); |
| 56 | + PyTuple_SetItem(plot_args, 1, colormap); |
| 57 | + |
| 58 | + PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_imshow, plot_args, kwargs); |
| 59 | + |
| 60 | + Py_DECREF(plot_args); |
| 61 | + Py_DECREF(kwargs); |
| 62 | + if(res != nullptr) Py_DECREF(res); |
| 63 | + |
| 64 | + return res != nullptr; |
| 65 | +} |
| 66 | +} |
0 commit comments