@@ -55,6 +55,8 @@ namespace matplotlibcpp {
55
55
PyObject *s_python_function_annotate;
56
56
PyObject *s_python_function_tight_layout;
57
57
PyObject *s_python_empty_tuple;
58
+ PyObject *s_python_function_stem;
59
+ PyObject *s_python_function_xkcd;
58
60
59
61
/* For now, _interpreter is implemented as a singleton since its currently not possible to have
60
62
multiple independent embedded python interpreters without patching the python source code
@@ -133,6 +135,8 @@ namespace matplotlibcpp {
133
135
s_python_function_clf = PyObject_GetAttrString (pymod, " clf" );
134
136
s_python_function_errorbar = PyObject_GetAttrString (pymod, " errorbar" );
135
137
s_python_function_tight_layout = PyObject_GetAttrString (pymod, " tight_layout" );
138
+ s_python_function_stem = PyObject_GetAttrString (pymod, " stem" );
139
+ s_python_function_xkcd = PyObject_GetAttrString (pymod, " xkcd" );
136
140
137
141
if ( !s_python_function_show
138
142
|| !s_python_function_draw
@@ -158,6 +162,8 @@ namespace matplotlibcpp {
158
162
|| !s_python_function_errorbar
159
163
|| !s_python_function_errorbar
160
164
|| !s_python_function_tight_layout
165
+ || !s_python_function_stem
166
+ || !s_python_function_xkcd
161
167
) { throw std::runtime_error (" Couldn't find required function!" ); }
162
168
163
169
if ( !PyFunction_Check (s_python_function_show)
@@ -183,6 +189,8 @@ namespace matplotlibcpp {
183
189
|| !PyFunction_Check (s_python_function_clf)
184
190
|| !PyFunction_Check (s_python_function_tight_layout)
185
191
|| !PyFunction_Check (s_python_function_errorbar)
192
+ || !PyFunction_Check (s_python_function_stem)
193
+ || !PyFunction_Check (s_python_function_xkcd)
186
194
) { throw std::runtime_error (" Python object is unexpectedly not a PyFunction." ); }
187
195
188
196
s_python_empty_tuple = PyTuple_New (0 );
@@ -302,6 +310,39 @@ namespace matplotlibcpp {
302
310
return res;
303
311
}
304
312
313
+ template <typename Numeric>
314
+ bool stem (const std::vector<Numeric> &x, const std::vector<Numeric> &y, const std::map<std::string, std::string>& keywords)
315
+ {
316
+ assert (x.size () == y.size ());
317
+
318
+ // using numpy arrays
319
+ PyObject* xarray = get_array (x);
320
+ PyObject* yarray = get_array (y);
321
+
322
+ // construct positional args
323
+ PyObject* args = PyTuple_New (2 );
324
+ PyTuple_SetItem (args, 0 , xarray);
325
+ PyTuple_SetItem (args, 1 , yarray);
326
+
327
+ // construct keyword args
328
+ PyObject* kwargs = PyDict_New ();
329
+ for (std::map<std::string, std::string>::const_iterator it =
330
+ keywords.begin (); it != keywords.end (); ++it) {
331
+ PyDict_SetItemString (kwargs, it->first .c_str (),
332
+ PyString_FromString (it->second .c_str ()));
333
+ }
334
+
335
+ PyObject* res = PyObject_Call (
336
+ detail::_interpreter::get ().s_python_function_stem , args, kwargs);
337
+
338
+ Py_DECREF (args);
339
+ Py_DECREF (kwargs);
340
+ if (res)
341
+ Py_DECREF (res);
342
+
343
+ return res;
344
+ }
345
+
305
346
template < typename Numeric >
306
347
bool fill_between (const std::vector<Numeric>& x, const std::vector<Numeric>& y1, const std::vector<Numeric>& y2, const std::map<std::string, std::string>& keywords)
307
348
{
@@ -409,6 +450,31 @@ namespace matplotlibcpp {
409
450
return res;
410
451
}
411
452
453
+ template <typename NumericX, typename NumericY>
454
+ bool stem (const std::vector<NumericX>& x, const std::vector<NumericY>& y, const std::string& s = " " )
455
+ {
456
+ assert (x.size () == y.size ());
457
+
458
+ PyObject* xarray = get_array (x);
459
+ PyObject* yarray = get_array (y);
460
+
461
+ PyObject* pystring = PyString_FromString (s.c_str ());
462
+
463
+ PyObject* plot_args = PyTuple_New (3 );
464
+ PyTuple_SetItem (plot_args, 0 , xarray);
465
+ PyTuple_SetItem (plot_args, 1 , yarray);
466
+ PyTuple_SetItem (plot_args, 2 , pystring);
467
+
468
+ PyObject* res = PyObject_CallObject (
469
+ detail::_interpreter::get ().s_python_function_stem , plot_args);
470
+
471
+ Py_DECREF (plot_args);
472
+ if (res)
473
+ Py_DECREF (res);
474
+
475
+ return res;
476
+ }
477
+
412
478
template <typename NumericX, typename NumericY>
413
479
bool semilogx (const std::vector<NumericX>& x, const std::vector<NumericY>& y, const std::string& s = " " )
414
480
{
@@ -642,6 +708,14 @@ namespace matplotlibcpp {
642
708
return plot (x,y,format);
643
709
}
644
710
711
+ template <typename Numeric>
712
+ bool stem (const std::vector<Numeric>& y, const std::string& format = " " )
713
+ {
714
+ std::vector<Numeric> x (y.size ());
715
+ for (size_t i = 0 ; i < x.size (); ++i) x.at (i) = i;
716
+ return stem (x, y, format);
717
+ }
718
+
645
719
inline void figure ()
646
720
{
647
721
PyObject* res = PyObject_CallObject (detail::_interpreter::get ().s_python_function_figure , detail::_interpreter::get ().s_python_empty_tuple );
@@ -826,6 +900,19 @@ namespace matplotlibcpp {
826
900
Py_DECREF (res);
827
901
}
828
902
903
+ inline void xkcd () {
904
+ PyObject* res;
905
+ PyObject *kwargs = PyDict_New ();
906
+
907
+ res = PyObject_Call (detail::_interpreter::get ().s_python_function_xkcd ,
908
+ detail::_interpreter::get ().s_python_empty_tuple , kwargs);
909
+
910
+ if (!res)
911
+ throw std::runtime_error (" Call to show() failed." );
912
+
913
+ Py_DECREF (res);
914
+ }
915
+
829
916
inline void draw ()
830
917
{
831
918
PyObject* res = PyObject_CallObject (
@@ -1015,6 +1102,21 @@ namespace matplotlibcpp {
1015
1102
return plot<double >(x,y,keywords);
1016
1103
}
1017
1104
1105
+ bool stem (const std::vector<double >& x, const std::vector<double >& y, const std::string& format = " " )
1106
+ {
1107
+ return stem<double , double >(x, y, format);
1108
+ }
1109
+
1110
+ bool stem (const std::vector<double >& y, const std::string& format = " " )
1111
+ {
1112
+ return stem<double >(y, format);
1113
+ }
1114
+
1115
+ bool stem (const std::vector<double >& x, const std::vector<double >& y, const std::map<std::string, std::string>& keywords)
1116
+ {
1117
+ return stem<double >(x, y, keywords);
1118
+ }
1119
+
1018
1120
bool named_plot (const std::string& name, const std::vector<double >& x, const std::vector<double >& y, const std::string& format = " " ) {
1019
1121
return named_plot<double >(name,x,y,format);
1020
1122
}
0 commit comments