8000 add imshow · intercore/matplotlib-cpp@ed2c5c3 · GitHub
[go: up one dir, main page]

Skip to content

Commit ed2c5c3

Browse files
committed
add imshow
1 parent b568e87 commit ed2c5c3

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ add_example(eigen)
2121
add_example(errorbar)
2222
add_example(fill_inbetween)
2323
add_example(fill)
24+
add_example(imshow)
2425
add_example(legend)
2526
add_example(loglog)
2627
add_example(minimal)

examples/imshow.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#define __USE_MATH_DEFINES
2+
#include <cmath>
3+
#include <Eigen/Dense>
4+
#include "../matplotlibcpp.h"
5+
namespace plt = matplotlibcpp;
6+
7+
void waves(const unsigned n) {
8+
Eigen::MatrixXd X(n, n);
9+
for (unsigned i = 0; i < n; ++i) {
10+
for (unsigned j = 0; j < n; ++j) {
11+
X(i, j) = sin(3.0 * M_PI * i / n) * cos(20.0 * M_PI * j / n);
12+
}
13+
}
14+
plt::figure();
15+
plt::imshow(X);
16+
plt::show();
17+
}
18+
19+
int main() {
20+
waves(200);
21+
return 0;
22+
}

matplotlibcpp.h

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ struct _interpreter {
9292
PyObject *s_python_function_suptitle;
9393
PyObject *s_python_function_bar;
9494
PyObject *s_python_function_subplots_adjust;
95+
PyObject *s_python_function_imshow;
9596

9697
/* For now, _interpreter is implemented as a singleton since its currently not
9798
possible to have multiple independent embedded python interpreters without
@@ -226,6 +227,7 @@ struct _interpreter {
226227
s_python_function_bar = PyObject_GetAttrString(pymod, "bar");
227228
s_python_function_subplots_adjust =
228229
PyObject_GetAttrString(pymod, "subplots_adjust");
230+
s_python_function_imshow = PyObject_GetAttrString(pymod, "imshow");
229231

230232
if (!s_python_function_show || !s_python_function_close ||
231233
!s_python_function_draw || !s_python_function_pause ||
@@ -248,7 +250,7 @@ struct _interpreter {
248250
!s_python_function_stem || !s_python_function_xkcd ||
249251
!s_python_function_text || !s_python_function_suptitle ||
250252
!s_python_function_bar || !s_python_function_subplots_adjust ||
251-
!s_python_function_spy) {
253+
!s_python_function_spy || !s_python_function_imshow) {
252254
throw std::runtime_error("Couldn't find required function!");
253255
}
254256

@@ -292,7 +294,9 @@ struct _interpreter {
292294
!PyFunction_Check(s_python_function_text) ||
293295
!PyFunction_Check(s_python_function_suptitle) ||
294296
!PyFunction_Check(s_python_function_bar) ||
295-
!PyFunction_Check(s_python_function_subplots_adjust)) {
297+
!PyFunction_Check(s_python_function_subplots_adjust) ||
298+
!PyFunction_Check(s_python_function_imshow)
299+
) {
296300
throw std::runtime_error(
297301
"Python object is unexpectedly not a PyFunction.");
298302
}
@@ -660,6 +664,29 @@ bool semilogy(const VectorY &y,
660664
return semilogy(x, y, "", keywords);
661665
}
662666

667+
template <typename Matrix>
668+
void imshow(const Matrix& X, const std::map<std::string, std::string> &keywords = {}) {
669+
PyObject *Xarray = get_2darray(X);
670+
671+
PyObject *kwargs = PyDict_New();
672+
for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
673+
it != keywords.end(); ++it) {
674+
PyDict_SetItemString(kwargs, it->first.c_str(),
675+
PyUnicode_FromString(it->second.c_str()));
676+
}
677+
678+
PyObject *plot_args = PyTuple_New(1);
679+
PyTuple_SetItem(plot_args, 0, Xarray);
680+
681+
PyObject *res = PyObject_Call(
682+
detail::_interpreter::get().s_python_function_imshow, plot_args, kwargs);
683+
684+
Py_DECREF(plot_args);
685+
Py_DECREF(kwargs);
686+
if (res)
687+
Py_DECREF(res);
688+
}
689+
663690
// @brief plot_surface for datapoints (x_ij, y_ij, z_ij) with i,j = 0..n
664691
// @param x The x values of the datapoints in a matrix
665692
// @param y The y values of the datapoints in a matrix

0 commit comments

Comments
 (0)
0