@@ -67,6 +67,9 @@ struct _interpreter {
6767 PyObject *s_python_function_xkcd;
6868 PyObject *s_python_function_text;
6969 PyObject *s_python_function_suptitle;
70+ PyObject *s_python_function_bar;
71+ PyObject *s_python_function_subplots_adjust;
72+
7073
7174 /* For now, _interpreter is implemented as a singleton since its currently not possible to have
7275 multiple independent embedded python interpreters without patching the python source code
@@ -175,6 +178,8 @@ struct _interpreter {
175178 s_python_function_xkcd = PyObject_GetAttrString (pymod, " xkcd" );
176179 s_python_function_text = PyObject_GetAttrString (pymod, " text" );
177180 s_python_function_suptitle = PyObject_GetAttrString (pymod, " suptitle" );
181+ s_python_function_bar = PyObject_GetAttrString (pymod," bar" );
182+ s_python_function_subplots_adjust = PyObject_GetAttrString (pymod," subplots_adjust" );
178183
179184 if ( !s_python_function_show
180185 || !s_python_function_close
@@ -209,6 +214,8 @@ struct _interpreter {
209214 || !s_python_function_xkcd
210215 || !s_python_function_text
211216 || !s_python_function_suptitle
217+ || !s_python_function_bar
218+ || !s_python_function_subplots_adjust
212219 ) { throw std::runtime_error (" Couldn't find required function!" ); }
213220
214221 if ( !PyFunction_Check (s_python_function_show)
@@ -243,6 +250,8 @@ struct _interpreter {
243250 || !PyFunction_Check (s_python_function_xkcd)
244251 || !PyFunction_Check (s_python_function_text)
245252 || !PyFunction_Check (s_python_function_suptitle)
253+ || !PyFunction_Check (s_python_function_bar)
254+ || !PyFunction_Check (s_python_function_subplots_adjust)
246255 ) { throw std::runtime_error (" Python object is unexpectedly not a PyFunction." ); }
247256
248257 s_python_empty_tuple = PyTuple_New (0 );
@@ -440,7 +449,6 @@ bool hist(const std::vector<Numeric>& y, long bins=10,std::string color="b", dou
440449 PyDict_SetItemString (kwargs, " color" , PyString_FromString (color.c_str ()));
441450 PyDict_SetItemString (kwargs, " alpha" , PyFloat_FromDouble (alpha));
442451
443-
444452 PyObject* plot_args = PyTuple_New (1 );
445453
446454 PyTuple_SetItem (plot_args, 0 , yarray);
@@ -482,6 +490,59 @@ bool scatter(const std::vector<NumericX>& x,
482490 return res;
483491}
484492
493+ template < typename Numeric>
494+ bool bar (const std::vector<Numeric>& y, std::string ec = " black" , std::string ls = " -" , double lw = 1.0 ,
495+ const std::map<std::string, std::string>& keywords = {})
496+ {
497+ PyObject* yarray = get_array (y);
498+
499+ std::vector<int > x;
500+ for (int i = 0 ; i < y.size (); i++)
501+ x.push_back (i);
502+
503+ PyObject* xarray = get_array (x);
504+
505+ PyObject* kwargs = PyDict_New ();
506+
507+ PyDict_SetItemString (kwargs, " ec" , PyString_FromString (ec.c_str ()));
508+ PyDict_SetItemString (kwargs, " ls" , PyString_FromString (ls.c_str ()));
509+ PyDict_SetItemString (kwargs, " lw" , PyFloat_FromDouble (lw));
510+
511+ PyObject* plot_args = PyTuple_New (2 );
512+ PyTuple_SetItem (plot_args, 0 , xarray);
513+ PyTuple_SetItem (plot_args, 1 , yarray);
514+
515+ PyObject* res = PyObject_Call (detail::_interpreter::get ().s_python_function_bar , plot_args, kwargs);
516+
517+ Py_DECREF (plot_args);
518+ Py_DECREF (kwargs);
519+ if (res) Py_DECREF (res);
520+
521+ return res;
522+ }
523+
524+ inline bool subplots_adjust (const std::map<std::string, double >& keywords = {})
525+ {
526+
527+ PyObject* kwargs = PyDict_New ();
528+ for (std::map<std::string, double >::const_iterator it =
529+ keywords.begin (); it != keywords.end (); ++it) {
530+ PyDict_SetItemString (kwargs, it->first .c_str (),
531+ PyFloat_FromDouble (it->second ));
532+ }
533+
534+
535+ PyObject* plot_args = PyTuple_New (0 );
536+
537+ PyObject* res = PyObject_Call (detail::_interpreter::get ().s_python_function_subplots_adjust , plot_args, kwargs);
538+
539+ Py_DECREF (plot_args);
540+ Py_DECREF (kwargs);
541+ if (res) Py_DECREF (res);
542+
543+ return res;
544+ }
545+
485546template < typename Numeric>
486547bool named_hist (std::string label,const std::vector<Numeric>& y, long bins=10 , std::string color=" b" , double alpha=1.0 )
487548{
0 commit comments