8000 Added binding to matplotlib's `subplots(...)` call · lava/matplotlib-cpp@f4305af · GitHub
[go: up one dir, main page]

Skip to content

Commit f4305af

Browse files
committed
Added binding to matplotlib's subplots(...) call
1 parent aa2e1d4 commit f4305af

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

matplotlibcpp.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ struct _interpreter {
6767
PyObject *s_python_function_scatter;
6868
PyObject *s_python_function_boxplot;
6969
PyObject *s_python_function_subplot;
70+
PyObject *s_python_function_subplots;
7071
PyObject *s_python_function_subplot2grid;
7172
PyObject *s_python_function_legend;
7273
PyObject *s_python_function_xlim;
@@ -243,6 +244,7 @@ struct _interpreter {
243244
s_python_function_scatter = safe_import(pymod,"scatter");
244245
s_python_function_boxplot = safe_import(pymod,"boxplot");
245246
s_python_function_subplot = safe_import(pymod, "subplot");
247+
s_python_function_subplots = safe_import(pymod, "subplots");
246248
s_python_function_subplot2grid = safe_import(pymod, "subplot2grid");
247249
s_python_function_legend = safe_import(pymod, "legend");
248250
s_python_function_xlim = safe_import(pymod, "xlim");
@@ -2266,6 +2268,40 @@ inline void subplot(long nrows, long ncols, long plot_number)
22662268
Py_DECREF(res);
22672269
}
22682270

2271+
/**
2272+
* @brief This utility wrapper makes it convenient to create common layouts of subplots, including the enclosing figure object, in a single call.
2273+
*
2274+
* @param nrows Number of rows of the subplot grid.
2275+
* @param ncols Number of columns of the subplot grid.
2276+
* @param keywords sharex {'none', 'all', 'row', 'col'}, sharey {'none', 'all', 'row', 'col'}
2277+
*/
2278+
inline void subplots(long nrows, long ncols, const std::map<std::string, std::string> &keywords = {})
2279+
{
2280+
detail::_interpreter::get();
2281+
2282+
// construct positional args
2283+
PyObject* args = PyTuple_New(2);
2284+
PyTuple_SetItem(args, 0, PyLong_FromLong(nrows));
2285+
PyTuple_SetItem(args, 1, PyLong_FromLong(ncols));
2286+
2287+
// construct keyword arguments
2288+
PyObject* kwargs = PyDict_New();
2289+
for (auto it = keywords.begin(); it != keywords.end(); ++it) {
2290+
PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
2291+
}
2292+
2293+
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_subplots, args, kwargs);
2294+
if(!res){
2295+
PyErr_Print(); // TODO: print this error on every exception!
2296+
throw std::runtime_error("Call to subplots() failed.");
2297+
}
2298+
2299+
// free memory
2300+
Py_DECREF(args);
2301+
Py_DECREF(kwargs);
2302+
Py_DECREF(res);
2303+
}
2304+
22692305
inline void subplot2grid(long nrows, long ncols, long rowid=0, long colid=0, long rowspan=1, long colspan=1)
22702306
{
22712307
detail::_interpreter::get();

0 commit comments

Comments
 (0)
0