@@ -67,6 +67,7 @@ struct _interpreter {
67
67
PyObject *s_python_function_scatter;
68
68
PyObject *s_python_function_boxplot;
69
69
PyObject *s_python_function_subplot;
70
+ PyObject *s_python_function_subplots;
70
71
PyObject *s_python_function_subplot2grid;
71
72
PyObject *s_python_function_legend;
72
73
PyObject *s_python_function_xlim;
@@ -243,6 +244,7 @@ struct _interpreter {
243
244
s_python_function_scatter = safe_import (pymod," scatter" );
244
245
s_python_function_boxplot = safe_import (pymod," boxplot" );
245
246
s_python_function_subplot = safe_import (pymod, " subplot" );
247
+ s_python_function_subplots = safe_import (pymod, " subplots" );
246
248
s_python_function_subplot2grid = safe_import (pymod, " subplot2grid" );
247
249
s_python_function_legend = safe_import (pymod, " legend" );
248
250
s_python_function_xlim = safe_import (pymod, " xlim" );
@@ -2266,6 +2268,40 @@ inline void subplot(long nrows, long ncols, long plot_number)
2266
2268
Py_DECREF (res);
2267
2269
}
2268
2270
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
+
2269
2305
inline void subplot2grid (long nrows, long ncols, long rowid=0 , long colid=0 , long rowspan=1 , long colspan=1 )
2270
2306
{
2271
2307
detail::_interpreter::get ();
0 commit comments