@@ -45,8 +45,9 @@ namespace matplotlibcpp {
45
45
PyObject *s_python_function_grid;
46
46
PyObject *s_python_function_clf;
47
47
PyObject *s_python_function_errorbar;
48
- PyObject *s_python_empty_tuple;
49
48
PyObject *s_python_function_annotate;
49
+ PyObject *s_python_function_tight_layout;
50
+ PyObject *s_python_empty_tuple;
50
51
51
52
/* For now, _interpreter is implemented as a singleton since its currently not possible to have
52
53
multiple independent embedded python interpreters without patching the python source code
@@ -101,6 +102,7 @@ namespace matplotlibcpp {
101
102
s_python_function_annotate = PyObject_GetAttrString (pymod," annotate" );
102
103
s_python_function_clf = PyObject_GetAttrString (pymod, " clf" );
103
104
s_python_function_errorbar = PyObject_GetAttrString (pymod, " errorbar" );
105
+ s_python_function_tight_layout = PyObject_GetAttrString (pymod, " tight_layout" );
104
106
105
107
if ( !s_python_function_show
106
108
|| !s_python_function_figure
@@ -119,6 +121,8 @@ namespace matplotlibcpp {
119
121
|| !s_python_function_clf
120
122
|| !s_python_function_annotate
121
123
|| !s_python_function_errorbar
124
+ || !s_python_function_errorbar
125
+ || !s_python_function_tight_layout
122
126
) { throw std::runtime_error (" Couldn't find required function!" ); }
123
127
124
128
if ( !PyFunction_Check (s_python_function_show)
@@ -137,6 +141,7 @@ namespace matplotlibcpp {
137
141
|| !PyFunction_Check (s_python_function_xlim)
138
142
|| !PyFunction_Check (s_python_function_save)
139
143
|| !PyFunction_Check (s_python_function_clf)
144
+ || !PyFunction_Check (s_python_function_tight_layout)
140
145
|| !PyFunction_Check (s_python_function_errorbar)
141
146
) { throw std::runtime_error (" Python object is unexpectedly not a PyFunction." ); }
142
147
@@ -629,6 +634,17 @@ namespace matplotlibcpp {
629
634
Py_DECREF (res);
630
635
}
631
636
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
+
632
648
#if __cplusplus > 199711L || _MSC_VER > 1800
633
649
// C++11-exclusive content starts here (variadic plot() and initializer list support)
634
650
0 commit comments