@@ -30,8 +30,9 @@ namespace matplotlibcpp {
30
30
PyObject *s_python_function_xlabel;
31
31
PyObject *s_python_function_ylabel;
32
32
PyObject *s_python_function_grid;
33
+ PyObject *s_python_function_clf;
33
34
PyObject *s_python_empty_tuple;
34
- PyObject *s_python_function_annotate;
35
+ PyObject *s_python_function_annotate;
35
36
36
37
/* For now, _interpreter is implemented as a singleton since its currently not possible to have
37
38
multiple independent embedded python interpreters without patching the python source code
@@ -77,30 +78,31 @@ namespace matplotlibcpp {
77
78
s_python_function_xlim = PyObject_GetAttrString (pymod, " xlim" );
78
79
s_python_function_save = PyObject_GetAttrString (pylabmod, " savefig" );
79
80
s_python_function_annotate = PyObject_GetAttrString (pymod," annotate" );
81
+ s_python_function_clf = PyObject_GetAttrString (pymod, " clf" );
80
82
81
83
if ( !s_python_function_show
82
- || !s_python_function_figure
83
- || !s_python_function_plot
84
- || !s_python_function_subplot
84
+ || !s_python_function_figure
85
+ || !s_python_function_plot
86
+ || !s_python_function_subplot
85
87
|| !s_python_function_legend
86
- || !s_python_function_ylim
87
- || !s_python_function_title
88
- || !s_python_function_axis
89
- || !s_python_function_xlabel
90
- || !s_python_function_ylabel
91
- || !s_python_function_grid
92
- || !s_python_function_xlim
93
- || !s_python_function_save
94
- || !s_python_function_annotate
95
- )
96
- { throw std::runtime_error (" Couldn't find required function!" ); }
97
-
98
- if ( !PyFunction_Check (s_python_function_show)
88
+ || !s_python_function_ylim
89
+ || !s_python_function_title
90
+ || !s_python_function_axis
91
+ || !s_python_function_xlabel
92
+ || !s_python_function_ylabel
93
+ || !s_python_function_grid
94
+ || !s_python_function_xlim
95
+ || !s_python_function_save
96
+ || !s_python_function_clf
97
+ || !s_python_function_annotate
98
+ ) { throw std::runtime_error (" Couldn't find required function!" ); }
99
+
100
+ if ( !PyFunction_Check (s_python_function_show)
99
101
|| !PyFunction_Check (s_python_function_figure)
100
102
|| !PyFunction_Check (s_python_function_plot)
101
103
|| !PyFunction_Check (s_python_function_subplot)
102
- || !PyFunction_Check (s_python_function_legend)
103
- || !PyFunction_Check (s_python_function_annotate)
104
+ || !PyFunction_Check (s_python_function_legend)
105
+ || !PyFunction_Check (s_python_function_annotate)
104
106
|| !PyFunction_Check (s_python_function_ylim)
105
107
|| !PyFunction_Check (s_python_function_title)
106
108
|| !PyFunction_Check (s_python_function_axis)
@@ -109,8 +111,8 @@ namespace matplotlibcpp {
109
111
|| !PyFunction_Check (s_python_function_grid)
110
112
|| !PyFunction_Check (s_python_function_xlim)
111
113
|| !PyFunction_Check (s_python_function_save)
112
- )
113
- { throw std::runtime_error (" Python object is unexpectedly not a PyFunction." ); }
114
+ || ! PyFunction_Check (s_python_function_clf )
115
+ ) { throw std::runtime_error (" Python object is unexpectedly not a PyFunction." ); }
114
116
115
117
s_python_empty_tuple = PyTuple_New (0 );
116
118
}
@@ -164,9 +166,6 @@ namespace matplotlibcpp {
164
166
PyTuple_SetItem (args, 0 , xlist);
165
167
PyTuple_SetItem (args, 1 , ylist);
166
168
167
- Py_DECREF (xlist);
168
- Py_DECREF (ylist);
169
-
170
169
// construct keyword args
171
170
PyObject* kwargs = PyDict_New ();
172
171
for (std::map<std::string, std::string>::const_iterator it = keywords.begin (); it != keywords.end (); ++it)
@@ -217,7 +216,7 @@ namespace matplotlibcpp {
217
216
PyObject* ylist = PyList_New (y.size ());
218
217
PyObject* kwargs = PyDict_New ();
219
218
PyDict_SetItemString (kwargs, " label" , PyString_FromString (label.c_str ()));
220
- PyDict_SetItemString (kwargs, " bins" , PyLong_FromLong (bins));
219
+ PyDict_SetItemString (kwargs, " bins" , PyLong_FromLong (bins));
221
220
PyDict_SetItemString (kwargs, " color" , PyString_FromString (color.c_str ()));
222
221
PyDict_SetItemString (kwargs, " alpha" , PyFloat_FromDouble (alpha));
223
222
@@ -264,7 +263,8 @@ namespace matplotlibcpp {
264
263
265
264
return res;
266
265
}
267
- template <typename Numeric>
266
+
267
+ template <typename Numeric>
268
268
bool named_plot (const std::string& name, const std::vector<Numeric>& y, const std::string& format = " " ) {
269
269
PyObject* kwargs = PyDict_New ();
270
270
PyDict_SetItemString (kwargs, " label" , PyString_FromString (name.c_str ()));
@@ -499,11 +499,18 @@ namespace matplotlibcpp {
499
499
PyObject* res = PyObject_CallObject (detail::_interpreter::get ().s_python_function_save , args);
500
500
if (!res) throw std::runtime_error (" Call to save() failed." );
501
501
502
- Py_DECREF (pyfilename);
503
502
Py_DECREF (args);
504
503
Py_DECREF (res);
505
504
}
506
505
506
+ inline void clf () {
507
+ PyObject *res = PyObject_CallObject (detail::_interpreter::get ().s_python_function_clf ,
508
+ detail::_interpreter::get ().s_python_empty_tuple );
509
+ if (!res) throw std::runtime_error (" Call to clf() failed." );
510
+
511
+ Py_DECREF (res);
512
+ }
513
+
507
514
#if __cplusplus > 199711L
508
515
// C++11-exclusive content starts here (variadic plot() and initializer list support)
509
516
@@ -582,8 +589,6 @@ namespace matplotlibcpp {
582
589
583
590
PyObject* res = PyObject_CallObject (detail::_interpreter::get ().s_python_function_plot , plot_args);
584
591
585
- Py_DECREF (xlist);
586
- Py_DECREF (ylist);
587
592
Py_DECREF (plot_args);
588
593
if (res) Py_DECREF (res);
589
594
0 commit comments