forked from lava/matplotlib-cpp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimshow.h
More file actions
66 lines (52 loc) · 1.64 KB
/
Copy pathimshow.h
File metadata and controls
66 lines (52 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma once
#include "_python.h"
#include "interpreter.h"
#include "_numpy.h"
#include "helpers.h"
namespace matplotlibcpp {
template<typename Numeric>
bool imshow(const std::vector<Numeric>& values, size_t width, size_t height, const std::string& cmap = "")
{
PyObject* matrix = get_array(values, width, height);
PyObject* colormap;
if (cmap == "")
{
colormap = Py_None;
}
else
{
colormap = PyString_FromString(cmap.c_str());
}
PyObject* plot_args = PyTuple_New(2);
PyTuple_SetItem(plot_args, 0, matrix);
PyTuple_SetItem(plot_args, 1, colormap);
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_imshow, plot_args);
Py_DECREF(plot_args);
if(res != nullptr) Py_DECREF(res);
return res != nullptr;
}
template<typename Numeric>
bool named_imshow(const std::string& name, const std::vector<Numeric>& values, size_t width, size_t height, const std::string& cmap = "")
{
PyObject* kwargs = PyDict_New();
PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
PyObject* matrix = get_array(values, width, height);
PyObject* colormap;
if (cmap == "")
{
colormap = Py_None;
}
else
{
colormap = PyString_FromString(cmap.c_str());
}
PyObject* plot_args = PyTuple_New(2);
PyTuple_SetItem(plot_args, 0, matrix);
PyTuple_SetItem(plot_args, 1, colormap);
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_imshow, plot_args, kwargs);
Py_DECREF(plot_args);
Py_DECREF(kwargs);
if(res != nullptr) Py_DECREF(res);
return res != nullptr;
}
}