Skip to content

Commit 18eab4c

Browse files
committed
Enable matplotlib contourf function
1 parent ef0383f commit 18eab4c

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ if(Python3_NumPy_FOUND)
9696
add_executable(contour examples/contour.cpp)
9797
target_link_libraries(contour PRIVATE matplotlib_cpp)
9898
set_target_properties(contour PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
99+
add_executable(contourf examples/contourf.cpp)
100+
target_link_libraries(contourf PRIVATE matplotlib_cpp)
101+
set_target_properties(contourf PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
99102

100103
add_executable(spy examples/spy.cpp)
101104
target_link_libraries(spy PRIVATE matplotlib_cpp)

examples/contourf.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::contourf(x, y, z);
23+
plt::show();
24+
}

matplotlibcpp.h

Lines changed: 44 additions & 0 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");
@@ -625,6 +627,48 @@ void contour(const std::vector<::std::vector<Numeric>> &x,
625627
if (res) Py_DECREF(res);
626628
}
627629

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy