@@ -31,6 +31,7 @@ namespace matplotlibcpp {
31
31
PyObject *s_python_function_ylabel;
32
32
PyObject *s_python_function_grid;
33
33
PyObject *s_python_function_clf;
34
+ PyObject *s_python_function_errorbar;
34
35
PyObject *s_python_empty_tuple;
35
36
PyObject *s_python_function_annotate;
36
37
@@ -79,6 +80,7 @@ namespace matplotlibcpp {
79
80
s_python_function_save = PyObject_GetAttrString (pylabmod, " savefig" );
80
81
s_python_function_annotate = PyObject_GetAttrString (pymod," annotate" );
81
82
s_python_function_clf = PyObject_GetAttrString (pymod, " clf" );
83
+ s_python_function_errorbar = PyObject_GetAttrString (pymod, " errorbar" );
82
84
83
85
if ( !s_python_function_show
84
86
|| !s_python_function_figure
@@ -95,6 +97,7 @@ namespace matplotlibcpp {
95
97
|| !s_python_function_save
96
98
|| !s_python_function_clf
97
99
|| !s_python_function_annotate
100
+ || !s_python_function_errorbar
98
101
) { throw std::runtime_error (" Couldn't find required function!" ); }
99
102
100
103
if ( !PyFunction_Check (s_python_function_show)
@@ -112,6 +115,7 @@ namespace matplotlibcpp {
112
115
|| !PyFunction_Check (s_python_function_xlim)
113
116
|| !PyFunction_Check (s_python_function_save)
114
117
|| !PyFunction_Check (s_python_function_clf)
118
+ || !PyFunction_Check (s_python_function_errorbar)
115
119
) { throw std::runtime_error (" Python object is unexpectedly not a PyFunction." ); }
116
120
117
121
s_python_empty_tuple = PyTuple_New (0 );
@@ -264,6 +268,44 @@ namespace matplotlibcpp {
264
268
return res;
265
269
}
266
270
271
+ template <typename NumericX, typename NumericY>
272
+ bool errorbar (const std::vector<NumericX> &x, const std::vector<NumericY> &y, const std::vector<NumericX> &yerr, const std::string &s = " " ) {
273
+ assert (x.size () == y.size ());
274
+
275
+ PyObject *kwargs = PyDict_New ();
276
+ PyObject *xlist = PyList_New (x.size ());
277
+ PyObject *ylist = PyList_New (y.size ());
278
+ PyObject *yerrlist = PyList_New (yerr.size ());
279
+
280
+ for (size_t i = 0 ; i < yerr.size (); ++i)
281
+ PyList_SetItem (yerrlist, i, PyFloat_FromDouble (yerr.at (i)));
282
+
283
+ PyDict_SetItemString (kwargs, " yerr" , yerrlist);
284
+
285
+ PyObject *pystring = PyString_FromString (s.c_str ());
286
+
287
+ for (size_t i = 0 ; i < x.size (); ++i) {
288
+ PyList_SetItem (xlist, i, PyFloat_FromDouble (x.at (i)));
289
+ PyList_SetItem (ylist, i, PyFloat_FromDouble (y.at (i)));
290
+ }
291
+
292
+ PyObject *plot_args = PyTuple_New (2 );
293
+ PyTuple_SetItem (plot_args, 0 , xlist);
294
+ PyTuple_SetItem (plot_args, 1 , ylist);
295
+
296
+ PyObject *res = PyObject_Call (detail::_interpreter::get ().s_python_function_errorbar , plot_args, kwargs);
297
+
298
+ Py_DECREF (kwargs);
299
+ Py_DECREF (plot_args);
300
+
301
+ if (res)
302
+ Py_DECREF (res);
303
+ else
304
+ throw std::runtime_error (" Call to errorbar() failed." );
305
+
306
+ return res;
307
+ }
308
+
267
309
template <typename Numeric>
268
310
bool named_plot (const std::string& name, const std::vector<Numeric>& y, const std::string& format = " " ) {
269
311
PyObject* kwargs = PyDict_New ();
0 commit comments