10000 add contourf function · wztzjhn/matplotlib-cpp@5506266 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5506266

Browse files
committed
add contourf function
1 parent ef0383f commit 5506266

File tree

1 file changed

+80
-7
lines changed

1 file changed

+80
-7
lines changed

matplotlibcpp.h

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ struct _interpreter {
5757
PyObject *s_python_function_plot;
5858
PyObject *s_python_function_quiver;
5959
PyObject* s_python_function_contour;
60+
PyObject* s_python_function_contourf;
6061
PyObject *s_python_function_semilogx;
6162
PyObject *s_python_function_semilogy;
6263
PyObject *s_python_function_loglog;
@@ -234,6 +235,7 @@ struct _interpreter {
234235
s_python_function_plot = safe_import(pymod, "plot");
235236
s_python_function_quiver = safe_import(pymod, "quiver");
236237
s_python_function_contour = safe_import(pymod, "contour");
238+
s_python_function_contourf = safe_import(pymod, "contourf");
237239
s_python_function_semilogx = safe_import(pymod, "semilogx");
238240
s_python_function_semilogy = safe_import(pymod, "semilogy");
239241
s_python_function_loglog = safe_import(pymod, "loglog");
@@ -610,10 +612,9 @@ void contour(const std::vector<::std::vector<Numeric>> &x,
610612

611613
PyDict_SetItemString(kwargs, "cmap", python_colormap_coolwarm);
612614

613-
for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
614-
it != keywords.end(); ++it) {
615-
PyDict_SetItemString(kwargs, it->first.c_str(),
616-
PyString_FromString(it->second.c_str()));
615+
for (const auto & keyword : keywords) {
616+
PyDict_SetItemString(kwargs, keyword.first.c_str(),
617+
PyString_FromString(keyword.second.c_str()));
617618
}
618619

619620
PyObject *res = PyObject_Call(detail::_interpreter::get().s_python_function_contour, args, kwargs);
@@ -625,6 +626,47 @@ void contour(const std::vector<::std::vector<Numeric>> &x,
625626
if (res) Py_DECREF(res);
626627
}
627628

629+
template <typename Numeric>
630+
void contourf(const std::vector<::std::vector<Numeric>> &x,
631+
const std::vector<::std::vector<Numeric>> &y,
632+
const std::vector<::std::vector<Numeric>> &z,
633+
const std::map<std::string, std::string> &keywords = {})
634+
{
635+
detail::_interpreter::get();
636+
637+
// using numpy arrays
638+
PyObject *xarray = detail::get_2darray(x);
639+
PyObject *yarray = detail::get_2darray(y);
640+
PyObject *zarray = detail::get_2darray(z);
641+
642+
// construct positional args
643+
PyObject *args = PyTuple_New(3);
644+
PyTuple_SetItem(args, 0, xarray);
645+
PyTuple_SetItem(args, 1, yarray);
646+
PyTuple_SetItem(args, 2, zarray);
647+
648+
// Build up the kw args.
649+
PyObject *kwargs = PyDict_New();
650+
651+
PyObject *python_colormap_coolwarm = PyObject_GetAttrString(
652+
detail::_interpreter::get().s_python_colormap, "coolwarm");
653+
654+
PyDict_SetItemString(kwargs, "cmap", python_colormap_coolwarm);
655+
656+
for (const auto & keyword : keywords) {
657+
PyDict_SetItemString(kwargs, keyword.first.c_str(),
658+
PyString_FromString(keyword.second.c_str()));
659+
}
660+
661+
PyObject *res = PyObject_Call(detail::_interpreter::get().s_python_function_contourf, args, kwargs);
662+
if (!res)
663+
throw std::runtime_error("failed contourf");
664+
665+
Py_DECREF(args);
666+
Py_DECREF(kwargs);
667+
if (res) Py_DECREF(res);
668+
}
669+
628670
template <typename Numeric>
629671
void spy(const std::vector<::std::vector<Numeric>> &x,
630672
const double markersize = -1, // -1 for default matplotlib size
@@ -1396,9 +1438,8 @@ bool contour(const std::vector<NumericX>& x, const std::vector<NumericY>& y,
13961438

13971439
// construct keyword args
13981440
PyObject* kwargs = PyDict_New();
1399-
for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
1400-
it != keywords.end(); ++it) {
1401-
PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
1441+
for (const auto & keyword : keywords) {
1442+
PyDict_SetItemString(kwargs, keyword.first.c_str(), PyUnicode_FromString(keyword.second.c_str()));
14021443
}
14031444

14041445
PyObject* res =
@@ -1412,6 +1453,38 @@ bool contour(const std::vector<NumericX>& x, const std::vector<NumericY>& y,
14121453
return res;
14131454
}
14141455

1456+
template <typename NumericX, typename NumericY, typename NumericZ>
1457+
bool contourf(const std::vector<NumericX>& x, const std::vector<NumericY>& y,
1458+
const std::vector<NumericZ>& z,
1459+
const std::map<std::string, std::string>& keywords = {}) {
1460+
assert(x.size() == y.size() && x.size() == z.size());
1461+
1462+
PyObject* xarray = detail::get_array(x);
1463+
PyObject* yarray = detail::get_array(y);
1464+
PyObject* zarray = detail::get_array(z);
1465+
1466+
PyObject* plot_args = PyTuple_New(3);
1467+
PyTuple_SetItem(plot_args, 0, xarray);
1468+
PyTuple_SetItem(plot_args, 1, yarray);
1469+
PyTuple_SetItem(plot_args, 2, zarray);
1470+
1471+
// construct keyword args
1472+
PyObject* kwargs = PyDict_New();
1473+
for (const auto & keyword : keywords) {
1474+
PyDict_SetItemString(kwargs, keyword.first.c_str(), PyUnicode_FromString(keyword.second.c_str()));
1475+
}
1476+
1477+
PyObject* res =
1478+
PyObject_Call(detail::_interpreter::get().s_python_function_contourf, plot_args, kwargs);
1479+
1480+
Py_DECREF(kwargs);
1481+
Py_DECREF(plot_args);
1482+
if (res)
1483+
Py_DECREF(res);
1484+
1485+
return res;
1486+
}
1487+
14151488
template<typename NumericX, typename NumericY, typename NumericU, typename NumericW>
14161489
bool quiver(const std::vector<NumericX>& x, const std::vector<NumericY>& y, const std::vector<NumericU>& u, const std::vector<NumericW>& w, const std::map<std::string, std::string>& keywords = {})
14171490
{

0 commit comments

Comments
 (0)
0