Skip to content

Commit f983e9e

Browse files
committed
Add imshow
1 parent f5511d1 commit f983e9e

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

matplotlibcpp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@
2424
#include "matplotlibcpp/log.h"
2525
#include "matplotlibcpp/errorbar.h"
2626
#include "matplotlibcpp/utilities.h"
27+
#include "matplotlibcpp/imshow.h"

matplotlibcpp/imshow.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

Comments
 (0)