8000 add contour plot · Park-MinJe/matplotlib-cpp@bbfb240 · GitHub
[go: up one dir, main page]

Skip to content
< 8000 header class="HeaderMktg header-logged-out js-details-container js-header Details f4 py-3" role="banner" data-is-top="true" data-color-mode=light data-light-theme=light data-dark-theme=dark>

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bbfb240

Browse files
CryorisBenno Evers
authored and
Benno Evers
committed
add contour plot
1 parent ec37453 commit bbfb240

File tree

Expand file tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

examples/contour.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "../matplotlibcpp.h"
2+
3+
#include <cmath>
4+
5+
namespace plt = matplotlibcpp;
6+
7+
int main()
8+
{
9+
std::vector<std::vector<double>> x, y, z;
10+
for (double i = -5; i <= 5; i += 0.25) {
11+
std::vector<double> x_row, y_row, z_row;
12+
for (double j = -5; j <= 5; j += 0.25) {
13+
x_row.push_back(i);
14+
y_row.push_back(j);
15+
z_row.push_back(::std::sin(::std::hypot(i, j)));
16+
}
17+
x.push_back(x_row);
18+
y.push_back(y_row);
19+
z.push_back(z_row);
20+
}
21+
22+
plt::contour(x, y, z);
23+
plt::show();
24+
}

matplotlibcpp.h

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ struct _interpreter {
103103
PyObject *s_python_function_subplots_adjust;
104104
PyObject *s_python_function_rcparams;
105105

106-
107106
/* For now, _interpreter is implemented as a singleton since its currently not possible to have
108107
multiple independent embedded python interpreters without patching the python source code
109108
or starting a separate process for each. [1]
@@ -245,6 +244,7 @@ struct _interpreter {
245244
s_python_function_subplot = safe_import(pymod, "subplot");
246245
s_python_function_subplot2grid = safe_import(pymod, "subplot2grid");
247246
s_python_function_legend = safe_import(pymod, "legend");
247+
s_python_function_xlim = safe_import(pymod, "xlim");
248248
s_python_function_ylim = safe_import(pymod, "ylim");
249249
s_python_function_title = safe_import(pymod, "title");
250250
s_python_function_axis = safe_import(pymod, "axis");
@@ -259,7 +259,6 @@ struct _interpreter {
259259
s_python_function_margins = safe_import(pymod, "margins");
260260
s_python_function_tick_params = safe_import(pymod, "tick_params");
261261
s_python_function_grid = safe_import(pymod, "grid");
262-
s_python_function_xlim = safe_import(pymod, "xlim");
263262
s_python_function_ion = safe_import(pymod, "ion");
264263
s_python_function_ginput = safe_import(pymod, "ginput");
265264
s_python_function_save = safe_import(pylabmod, "savefig");
@@ -349,10 +348,10 @@ template <> struct select_npy_type<uint64_t> { const static NPY_TYPES type = NPY
349348

350349
// Sanity checks; comment them out or change the numpy type below if you're compiling on
351350
// a platform where they don't apply
352-
static_assert(sizeof(long long) == 8);
353-
template <> struct select_npy_type<long long> { const static NPY_TYPES type = NPY_INT64; };
354-
static_assert(sizeof(unsigned long long) == 8);
355-
template <> struct select_npy_type<unsigned long long> { const static NPY_TYPES type = NPY_UINT64; };
351+
// static_assert(sizeof(long long) == 8);
352+
// template <> struct select_npy_type<long long> { const static NPY_TYPES type = NPY_INT64; };
353+
// static_assert(sizeof(unsigned long long) == 8);
354+
// template <> struct select_npy_type<unsigned long long> { const static NPY_TYPES type = NPY_UINT64; };
356355
// TODO: add int, long, etc.
357356

358357
template<typename Numeric>
@@ -582,6 +581,49 @@ void plot_surface(const std::vector<::std::vector<Numeric>> &x,
582581
Py_DECREF(kwargs);
583582
if (res) Py_DECREF(res);
584583
}
584+
585+
template <typename Numeric>
586+
void contour(const std::vector<::std::vector<Numeric>> &x,
587+
const std::vector<::std::vector<Numeric>> &y,
588+
const std::vector<::std::vector<Numeric>> &z,
589+
const std::map<std::string, std::string> &keywords = {})
590+
{
591+
detail::_interpreter::get();
592+
593+
// using numpy arrays
594+
PyObject *xarray = detail::get_2darray(x);
595+
PyObject *yarray = detail::get_2darray(y);
596+
PyObject *zarray = detail::get_2darray(z);
597+
598+
// construct positional args
599+
PyObject *args = PyTuple_New(3);
600+
PyTuple_SetItem(args, 0, xarray);
601+
PyTuple_SetItem(args, 1, yarray);
602+
PyTuple_SetItem(args, 2, zarray);
603+
604+
// Build up the kw args.
605+
PyObject *kwargs = PyDict_New();
606+
607+
PyObject *python_colormap_coolwarm = PyObject_GetAttrString(
608+
detail::_interpreter::get().s_python_colormap, "coolwarm");
609+
610+
PyDict_SetItemString(kwargs, "cmap", python_colormap_coolwarm);
611+
612+
for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
613+
it != keywords.end(); ++it) {
614+
PyDict_SetItemString(kwargs, it->first.c_str(),
615+
PyString_FromString(it->second.c_str()));
616+
}
617+
618+
PyObject *res = PyObject_Call(detail::_interpreter::get().s_python_function_contour, args, kwargs);
619+
if (!res)
620+
throw std::runtime_error("failed contour");
621+
622+
Py_DECREF(args);
623+
Py_DECREF(kwargs);
624+
if (res)
625+
Py_DECREF(res);
626+
}
585627
#endif // WITHOUT_NUMPY
586628

587629
template <typename Numeric>

0 commit comments

Comments
 (0)
0