@@ -45,8 +45,9 @@ namespace matplotlibcpp {
4545 PyObject *s_python_function_grid;
4646 PyObject *s_python_function_clf;
4747 PyObject *s_python_function_errorbar;
48- PyObject *s_python_empty_tuple;
4948 PyObject *s_python_function_annotate;
49+ PyObject *s_python_function_tight_layout;
50+ PyObject *s_python_empty_tuple;
5051
5152 /* For now, _interpreter is implemented as a singleton since its currently not possible to have
5253 multiple independent embedded python interpreters without patching the python source code
@@ -101,6 +102,7 @@ namespace matplotlibcpp {
101102 s_python_function_annotate = PyObject_GetAttrString (pymod," annotate" );
102103 s_python_function_clf = PyObject_GetAttrString (pymod, " clf" );
103104 s_python_function_errorbar = PyObject_GetAttrString (pymod, " errorbar" );
105+ s_python_function_tight_layout = PyObject_GetAttrString (pymod, " tight_layout" );
104106
105107 if ( !s_python_function_show
106108 || !s_python_function_figure
@@ -119,6 +121,8 @@ namespace matplotlibcpp {
119121 || !s_python_function_clf
120122 || !s_python_function_annotate
121123 || !s_python_function_errorbar
124+ || !s_python_function_errorbar
125+ || !s_python_function_tight_layout
122126 ) { throw std::runtime_error (" Couldn't find required function!" ); }
123127
124128 if ( !PyFunction_Check (s_python_function_show)
@@ -137,6 +141,7 @@ namespace matplotlibcpp {
137141 || !PyFunction_Check (s_python_function_xlim)
138142 || !PyFunction_Check (s_python_function_save)
139143 || !PyFunction_Check (s_python_function_clf)
144+ || !PyFunction_Check (s_python_function_tight_layout)
140145 || !PyFunction_Check (s_python_function_errorbar)
141146 ) { throw std::runtime_error (" Python object is unexpectedly not a PyFunction." ); }
142147
@@ -629,6 +634,17 @@ namespace matplotlibcpp {
629634 Py_DECREF (res);
630635 }
631636
637+ // Actually, is there any reason not to call this automatically for every plot?
638+ inline void tight_layout () {
639+ PyObject *res = PyObject_CallObject (
640+ detail::_interpreter::get ().s_python_function_tight_layout ,
641+ detail::_interpreter::get ().s_python_empty_tuple );
642+
643+ if (!res) throw std::runtime_error (" Call to tight_layout() failed." );
644+
645+ Py_DECREF (res);
646+ }
647+
632648#if __cplusplus > 199711L || _MSC_VER > 1800
633649 // C++11-exclusive content starts here (variadic plot() and initializer list support)
634650
0 commit comments