@@ -67,6 +67,9 @@ struct _interpreter {
67
67
PyObject *s_python_function_xkcd;
68
68
PyObject *s_python_function_text;
69
69
PyObject *s_python_function_suptitle;
70
+ PyObject *s_python_function_bar;
71
+ PyObject *s_python_function_subplots_adjust;
72
+
70
73
71
74
/* For now, _interpreter is implemented as a singleton since its currently not possible to have
72
75
multiple independent embedded python interpreters without patching the python source code
@@ -175,6 +178,8 @@ struct _interpreter {
175
178
s_python_function_xkcd = PyObject_GetAttrString (pymod, " xkcd" );
176
179
s_python_function_text = PyObject_GetAttrString (pymod, " text" );
177
180
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" );
178
183
179
184
if ( !s_python_function_show
180
185
|| !s_python_function_close
@@ -209,6 +214,8 @@ struct _interpreter {
209
214
|| !s_python_function_xkcd
210
215
|| !s_python_function_text
211
216
|| !s_python_function_suptitle
217
+ || !s_python_function_bar
218
+ || !s_python_function_subplots_adjust
212
219
) { throw std::runtime_error (" Couldn't find required function!" ); }
213
220
214
221
if ( !PyFunction_Check (s_python_function_show)
@@ -243,6 +250,8 @@ struct _interpreter {
243
250
|| !PyFunction_Check (s_python_function_xkcd)
244
251
|| !PyFunction_Check (s_python_function_text)
245
252
|| !PyFunction_Check (s_python_function_suptitle)
253
+ || !PyFunction_Check (s_python_function_bar)
254
+ || !PyFunction_Check (s_python_function_subplots_adjust)
246
255
) { throw std::runtime_error (" Python object is unexpectedly not a PyFunction." ); }
247
256
248
257
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
440
449
PyDict_SetItemString (kwargs, " color" , PyString_FromString (color.c_str ()));
441
450
PyDict_SetItemString (kwargs, " alpha" , PyFloat_FromDouble (alpha));
442
451
443
-
444
452
PyObject* plot_args = PyTuple_New (1 );
445
453
446
454
PyTuple_SetItem (plot_args, 0 , yarray);
@@ -482,6 +490,59 @@ bool scatter(const std::vector<NumericX>& x,
482
490
return res;
483
491
}
484
492
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
+
485
546
template < typename Numeric>
486
547
bool named_hist (std::string label,const std::vector<Numeric>& y, long bins=10 , std::string color=" b" , double alpha=1.0 )
487
548
{
0 commit comments