63 namespace Matplotlib {
66 static std::string s_backend;
69 PyObject * s_python_function_show;
70 PyObject * s_python_function_close;
71 PyObject * s_python_function_draw;
72 PyObject * s_python_function_pause;
73 PyObject * s_python_function_save;
74 PyObject * s_python_function_figure;
75 PyObject * s_python_function_fignum_exists;
76 PyObject * s_python_function_plot;
77 PyObject * s_python_function_quiver;
78 PyObject * s_python_function_semilogx;
79 PyObject * s_python_function_semilogy;
80 PyObject * s_python_function_loglog;
81 PyObject * s_python_function_fill;
82 PyObject * s_python_function_fill_between;
83 PyObject * s_python_function_hist;
84 PyObject * s_python_function_imshow;
85 PyObject * s_python_function_scatter;
86 PyObject * s_python_function_subplot;
87 PyObject * s_python_function_subplot2grid;
88 PyObject * s_python_function_legend;
89 PyObject * s_python_function_xlim;
90 PyObject * s_python_function_ion;
91 PyObject * s_python_function_ginput;
92 PyObject * s_python_function_ylim;
93 PyObject * s_python_function_title;
94 PyObject * s_python_function_axis;
95 PyObject * s_python_function_xscale;
96 PyObject * s_python_function_yscale;
97 PyObject * s_python_function_xlabel;
98 PyObject * s_python_function_ylabel;
99 PyObject * s_python_function_xticks;
100 PyObject * s_python_function_yticks;
101 PyObject * s_python_function_tick_params;
102 PyObject * s_python_function_grid;
103 PyObject * s_python_function_clf;
104 PyObject * s_python_function_errorbar;
105 PyObject * s_python_function_annotate;
106 PyObject * s_python_function_tight_layout;
107 PyObject * s_python_colormap;
108 PyObject * s_python_empty_tuple;
109 PyObject * s_python_function_stem;
110 PyObject * s_python_function_xkcd;
111 PyObject * s_python_function_text;
112 PyObject * s_python_function_suptitle;
113 PyObject * s_python_function_bar;
114 PyObject * s_python_function_subplots_adjust;
122 static _interpreter &
get() {
123 static _interpreter ctx;
127 PyObject * safe_import(PyObject * module, std::string fname) {
128 PyObject * fn = PyObject_GetAttrString(module, fname.c_str());
131 throw std::runtime_error(std::string(
"Couldn't find required function: ") + fname);
133 if (!PyFunction_Check(fn))
134 throw std::runtime_error(fname + std::string(
" is unexpectedly not a PyFunction."));
141#if PY_MAJOR_VERSION >= 3
143 void * import_numpy() {
150 void import_numpy() {
159#if PY_MAJOR_VERSION >= 3
160#if PY_VERSION_HEX < 0x030B0000
161 wchar_t name[] = L
"plotting";
162 Py_SetProgramName(
name);
165 char name[] =
"plotting";
166 Py_SetProgramName(
name);
174 PyObject * matplotlibname = PyString_FromString(
"matplotlib");
175 PyObject * pyplotname = PyString_FromString(
"matplotlib.pyplot");
176 PyObject * cmname = PyString_FromString(
"matplotlib.cm");
177 PyObject * pylabname = PyString_FromString(
"pylab");
178 if (!pyplotname || !pylabname || !matplotlibname || !cmname) {
179 throw std::runtime_error(
"couldnt create string");
182 PyObject * matplotlib = PyImport_Import(matplotlibname);
183 Py_DECREF(matplotlibname);
186 throw std::runtime_error(
"Error loading module matplotlib!");
191 if (!s_backend.empty()) {
192 PyObject_CallMethod(matplotlib,
193 const_cast<char *
>(
"use"),
194 const_cast<char *
>(
"s"),
198 PyObject * pymod = PyImport_Import(pyplotname);
199 Py_DECREF(pyplotname);
201 throw std::runtime_error(
"Error loading module matplotlib.pyplot!");
204 s_python_colormap = PyImport_Import(cmname);
206 if (!s_python_colormap) {
207 throw std::runtime_error(
"Error loading module matplotlib.cm!");
210 PyObject * pylabmod = PyImport_Import(pylabname);
211 Py_DECREF(pylabname);
213 throw std::runtime_error(
"Error loading module pylab!");
216 s_python_function_show = safe_import(pymod,
"show");
217 s_python_function_close = safe_import(pymod,
"close");
218 s_python_function_draw = safe_import(pymod,
"draw");
219 s_python_function_pause = safe_import(pymod,
"pause");
220 s_python_function_figure = safe_import(pymod,
"figure");
221 s_python_function_fignum_exists = safe_import(pymod,
"fignum_exists");
222 s_python_function_plot = safe_import(pymod,
"plot");
223 s_python_function_quiver = safe_import(pymod,
"quiver");
224 s_python_function_semilogx = safe_import(pymod,
"semilogx");
225 s_python_function_semilogy = safe_import(pymod,
"semilogy");
226 s_python_function_loglog = safe_import(pymod,
"loglog");
227 s_python_function_fill = safe_import(pymod,
"fill");
228 s_python_function_fill_between = safe_import(pymod,
"fill_between");
229 s_python_function_hist = safe_import(pymod,
"hist");
230 s_python_function_scatter = safe_import(pymod,
"scatter");
231 s_python_function_subplot = safe_import(pymod,
"subplot");
232 s_python_function_subplot2grid = safe_import(pymod,
"subplot2grid");
233 s_python_function_legend = safe_import(pymod,
"legend");
234 s_python_function_ylim = safe_import(pymod,
"ylim");
235 s_python_function_title = safe_import(pymod,
"title");
236 s_python_function_axis = safe_import(pymod,
"axis");
237 s_python_function_xscale = safe_import(pymod,
"xscale");
238 s_python_function_yscale = safe_import(pymod,
"yscale");
239 s_python_function_xlabel = safe_import(pymod,
"xlabel");
240 s_python_function_ylabel = safe_import(pymod,
"ylabel");
241 s_python_function_xticks = safe_import(pymod,
"xticks");
242 s_python_function_yticks = safe_import(pymod,
"yticks");
243 s_python_function_tick_params = safe_import(pymod,
"tick_params");
244 s_python_function_grid = safe_import(pymod,
"grid");
245 s_python_function_xlim = safe_import(pymod,
"xlim");
246 s_python_function_ion = safe_import(pymod,
"ion");
247 s_python_function_ginput = safe_import(pymod,
"ginput");
248 s_python_function_save = safe_import(pylabmod,
"savefig");
249 s_python_function_annotate = safe_import(pymod,
"annotate");
250 s_python_function_clf = safe_import(pymod,
"clf");
251 s_python_function_errorbar = safe_import(pymod,
"errorbar");
252 s_python_function_tight_layout = safe_import(pymod,
"tight_layout");
253 s_python_function_stem = safe_import(pymod,
"stem");
254 s_python_function_xkcd = safe_import(pymod,
"xkcd");
255 s_python_function_text = safe_import(pymod,
"text");
256 s_python_function_suptitle = safe_import(pymod,
"suptitle");
257 s_python_function_bar = safe_import(pymod,
"bar");
258 s_python_function_subplots_adjust = safe_import(pymod,
"subplots_adjust");
260 s_python_function_imshow = safe_import(pymod,
"imshow");
263 s_python_empty_tuple = PyTuple_New(0);
266 ~_interpreter() { Py_Finalize(); }
274 inline bool annotate(std::string annotation,
double x,
double y) {
275 detail::_interpreter::get();
276 PyObject * xy = PyTuple_New(2);
277 PyObject * str = PyString_FromString(annotation.c_str());
279 PyTuple_SetItem(xy, 0, PyFloat_FromDouble(x));
280 PyTuple_SetItem(xy, 1, PyFloat_FromDouble(
y));
282 PyObject * kwargs = PyDict_New();
283 PyDict_SetItemString(kwargs,
"xy", xy);
285 PyObject * args = PyTuple_New(1);
286 PyTuple_SetItem(args, 0, str);
288 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_annotate, args, kwargs);
301 template <
typename T>
302 struct select_npy_type {
303 const static NPY_TYPES type = NPY_NOTYPE;
306 struct select_npy_type<double> {
307 const static NPY_TYPES type = NPY_DOUBLE;
310 struct select_npy_type<float> {
311 const static NPY_TYPES type = NPY_FLOAT;
314 struct select_npy_type<bool> {
315 const static NPY_TYPES
type = NPY_BOOL;
318 struct select_npy_type<int8_t> {
319 const static NPY_TYPES
type = NPY_INT8;
322 struct select_npy_type<int16_t> {
323 const static NPY_TYPES
type = NPY_SHORT;
326 struct select_npy_type<int32_t> {
327 const static NPY_TYPES
type = NPY_INT;
330 struct select_npy_type<int64_t> {
331 const static NPY_TYPES
type = NPY_INT64;
334 struct select_npy_type<uint8_t> {
335 const static NPY_TYPES
type = NPY_UINT8;
338 struct select_npy_type<uint16_t> {
339 const static NPY_TYPES
type = NPY_USHORT;
342 struct select_npy_type<uint32_t> {
343 const static NPY_TYPES
type = NPY_ULONG;
346 struct select_npy_type<uint64_t> {
347 const static NPY_TYPES
type = NPY_UINT64;
350 template <
typename Numeric>
352 detail::_interpreter::get();
354 NPY_TYPES type = select_npy_type<Numeric>::type;
355 if (type == NPY_NOTYPE) {
356 std::vector<double> vd(v.size());
357 npy_intp vsize = v.size();
358 std::copy(v.begin(), v.end(), vd.begin());
359 PyObject * varray = PyArray_SimpleNewFromData(1, &vsize, NPY_DOUBLE, (
void *)(vd.data()));
363 npy_intp vsize = v.size();
364 PyObject * varray = PyArray_SimpleNewFromData(1, &vsize, type, (
void *)(v.data()));
368 template <
typename Numeric>
369 PyObject *
get_2darray(
const std::vector<::std::vector<Numeric>> & v) {
370 detail::_interpreter::get();
373 throw std::runtime_error(
"get_2d_array v too small");
375 npy_intp vsize[2] = {
static_cast<npy_intp
>(v.size()),
static_cast<npy_intp
>(v[0].size())};
377 PyArrayObject * varray = (PyArrayObject *)PyArray_SimpleNew(2, vsize, NPY_DOUBLE);
379 double * vd_begin =
static_cast<double *
>(PyArray_DATA(varray));
381 for (const ::std::vector<Numeric> & v_row : v) {
382 if (v_row.size() !=
static_cast<size_t>(vsize[1]))
383 throw std::runtime_error(
"Missmatched array size");
384 std::copy(v_row.begin(), v_row.end(), vd_begin);
385 vd_begin += vsize[1];
388 return reinterpret_cast<PyObject *
>(varray);
394 template <
typename Numeric>
395 PyObject *
get_array(
const std::vector<Numeric> & v) {
396 detail::_interpreter::get();
397 PyObject * list = PyList_New(v.size());
398 for (
size_t i = 0; i < v.size(); ++i) {
399 PyList_SetItem(list, i, PyFloat_FromDouble(v.at(i)));
406 template <
typename Numeric>
407 bool plot(
const std::vector<Numeric> & x,
408 const std::vector<Numeric> &
y,
409 const std::map<std::string, std::string> & keywords) {
410 assert(x.size() ==
y.size());
417 PyObject * args = PyTuple_New(2);
418 PyTuple_SetItem(args, 0, xarray);
419 PyTuple_SetItem(args, 1, yarray);
422 PyObject * kwargs = PyDict_New();
423 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
425 PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
428 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_plot, args, kwargs);
441 template <
typename Numeric>
444 const std::vector<::std::vector<Numeric>> &
y,
445 const std::vector<::std::vector<Numeric>> & z,
446 const std::map<std::string, std::string> & keywords = std::map<std::string, std::string>()) {
451 static PyObject *mpl_toolkitsmod =
nullptr, *axis3dmod =
nullptr;
452 if (!mpl_toolkitsmod) {
453 detail::_interpreter::get();
455 PyObject * mpl_toolkits = PyString_FromString(
"mpl_toolkits");
456 PyObject * axis3d = PyString_FromString(
"mpl_toolkits.mplot3d");
457 if (!mpl_toolkits || !axis3d) {
458 throw std::runtime_error(
"couldnt create string");
461 mpl_toolkitsmod = PyImport_Import(mpl_toolkits);
462 Py_DECREF(mpl_toolkits);
463 if (!mpl_toolkitsmod) {
464 throw std::runtime_error(
"Error loading module mpl_toolkits!");
467 axis3dmod = PyImport_Import(axis3d);
470 throw std::runtime_error(
"Error loading module mpl_toolkits.mplot3d!");
474 assert(x.size() ==
y.size());
475 assert(
y.size() == z.size());
483 PyObject * args = PyTuple_New(3);
484 PyTuple_SetItem(args, 0, xarray);
485 PyTuple_SetItem(args, 1, yarray);
486 PyTuple_SetItem(args, 2, zarray);
489 PyObject * kwargs = PyDict_New();
490 PyDict_SetItemString(kwargs,
"rstride", PyInt_FromLong(1));
491 PyDict_SetItemString(kwargs,
"cstride", PyInt_FromLong(1));
493 PyObject * python_colormap_coolwarm =
494 PyObject_GetAttrString(detail::_interpreter::get().s_python_colormap,
"coolwarm");
496 PyDict_SetItemString(kwargs,
"cmap", python_colormap_coolwarm);
498 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
500 PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
503 PyObject * fig = PyObject_CallObject(detail::_interpreter::get().s_python_function_figure,
504 detail::_interpreter::get().s_python_empty_tuple);
506 throw std::runtime_error(
"Call to figure() failed.");
508 PyObject * gca_kwargs = PyDict_New();
509 PyDict_SetItemString(gca_kwargs,
"projection", PyString_FromString(
"3d"));
511 PyObject * gca = PyObject_GetAttrString(fig,
"gca");
513 throw std::runtime_error(
"No gca");
515 PyObject *
axis = PyObject_Call(gca, detail::_interpreter::get().s_python_empty_tuple, gca_kwargs);
518 throw std::runtime_error(
"No axis");
522 Py_DECREF(gca_kwargs);
526 throw std::runtime_error(
"No surface");
528 PyObject * res = PyObject_Call(
plot_surface, args, kwargs);
530 throw std::runtime_error(
"failed surface");
541 template <
typename Numeric>
542 bool stem(
const std::vector<Numeric> & x,
543 const std::vector<Numeric> &
y,
544 const std::map<std::string, std::string> & keywords) {
545 assert(x.size() ==
y.size());
552 PyObject * args = PyTuple_New(2);
553 PyTuple_SetItem(args, 0, xarray);
554 PyTuple_SetItem(args, 1, yarray);
557 PyObject * kwargs = PyDict_New();
558 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
560 PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
563 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_stem, args, kwargs);
573 template <
typename Numeric>
574 bool fill(
const std::vector<Numeric> & x,
575 const std::vector<Numeric> &
y,
576 const std::map<std::string, std::string> & keywords) {
577 assert(x.size() ==
y.size());
584 PyObject * args = PyTuple_New(2);
585 PyTuple_SetItem(args, 0, xarray);
586 PyTuple_SetItem(args, 1, yarray);
589 PyObject * kwargs = PyDict_New();
590 for (
auto it = keywords.begin(); it != keywords.end(); ++it) {
591 PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
594 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_fill, args, kwargs);
605 template <
typename Numeric>
607 const std::vector<Numeric> & y1,
608 const std::vector<Numeric> & y2,
609 const std::map<std::string, std::string> & keywords) {
610 assert(x.size() == y1.size());
611 assert(x.size() == y2.size());
619 PyObject * args = PyTuple_New(3);
620 PyTuple_SetItem(args, 0, xarray);
621 PyTuple_SetItem(args, 1, y1array);
622 PyTuple_SetItem(args, 2, y2array);
625 PyObject * kwargs = PyDict_New();
626 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
628 PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
632 PyObject_Call(detail::_interpreter::get().s_python_function_fill_between, args, kwargs);
642 template <
typename Numeric>
643 bool hist(
const std::vector<Numeric> &
y,
645 std::string color =
"b",
647 bool cumulative =
false) {
650 PyObject * kwargs = PyDict_New();
651 PyDict_SetItemString(kwargs,
"bins", PyLong_FromLong(bins));
652 PyDict_SetItemString(kwargs,
"color", PyString_FromString(color.c_str()));
653 PyDict_SetItemString(kwargs,
"alpha", PyFloat_FromDouble(
alpha));
654 PyDict_SetItemString(kwargs,
"cumulative", cumulative ? Py_True : Py_False);
656 PyObject * plot_args = PyTuple_New(1);
658 PyTuple_SetItem(plot_args, 0, yarray);
660 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_hist, plot_args, kwargs);
662 Py_DECREF(plot_args);
672 inline void imshow(
void * ptr,
673 const NPY_TYPES type,
677 const std::map<std::string, std::string> & keywords) {
678 assert(type == NPY_UINT8 || type == NPY_FLOAT);
679 assert(colors == 1 || colors == 3 || colors == 4);
681 detail::_interpreter::get();
685 npy_intp dims[3] = {
rows, columns, colors};
686 PyObject * args = PyTuple_New(1);
687 PyTuple_SetItem(args, 0, PyArray_SimpleNewFromData(colors == 1 ? 2 : 3, dims, type, ptr));
690 PyObject * kwargs = PyDict_New();
691 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
693 PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
696 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_imshow, args, kwargs);
700 throw std::runtime_error(
"Call to imshow() failed");
705 inline void imshow(
const unsigned char * ptr,
709 const std::map<std::string, std::string> & keywords = {}) {
710 internal::imshow((
void *)ptr, NPY_UINT8,
rows, columns, colors, keywords);
717 const std::map<std::string, std::string> & keywords = {}) {
718 internal::imshow((
void *)ptr, NPY_FLOAT,
rows, columns, colors, keywords);
722 void imshow(
const cv::Mat & image,
const std::map<std::string, std::string> & keywords = {}) {
725 NPY_TYPES npy_type = NPY_UINT8;
726 switch (image.type() & CV_MAT_DEPTH_MASK) {
732 npy_type = NPY_FLOAT;
735 image.convertTo(image2, CV_MAKETYPE(CV_8U, image.channels()));
739 switch (image2.channels()) {
741 cv::cvtColor(image2, image2, CV_BGR2RGB);
744 cv::cvtColor(image2, image2, CV_BGRA2RGBA);
747 internal::imshow(image2.data, npy_type, image2.rows, image2.cols, image2.channels(), keywords);
752 template <
typename NumericX,
typename NumericY>
754 const std::vector<NumericY> &
y,
755 const double s = 1.0,
756 const std::unordered_map<std::string, std::string> & keywords = {}) {
757 assert(x.size() ==
y.size());
762 PyObject * kwargs = PyDict_New();
763 PyDict_SetItemString(kwargs,
"s", PyLong_FromLong(s));
764 for (
const auto & it : keywords) {
765 PyDict_SetItemString(kwargs, it.first.c_str(), PyString_FromString(it.second.c_str()));
768 PyObject * plot_args = PyTuple_New(2);
769 PyTuple_SetItem(plot_args, 0, xarray);
770 PyTuple_SetItem(plot_args, 1, yarray);
773 PyObject_Call(detail::_interpreter::get().s_python_function_scatter, plot_args, kwargs);
775 Py_DECREF(plot_args);
783 template <
typename Numeric>
784 bool bar(
const std::vector<Numeric> & x,
785 const std::vector<Numeric> &
y,
786 std::string ec =
"black",
787 std::string ls =
"-",
789 const std::map<std::string, std::string> & keywords = {}) {
793 PyObject * kwargs = PyDict_New();
795 PyDict_SetItemString(kwargs,
"ec", PyString_FromString(ec.c_str()));
796 PyDict_SetItemString(kwargs,
"ls", PyString_FromString(ls.c_str()));
797 PyDict_SetItemString(kwargs,
"lw", PyFloat_FromDouble(lw));
799 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
801 PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
804 PyObject * plot_args = PyTuple_New(2);
805 PyTuple_SetItem(plot_args, 0, xarray);
806 PyTuple_SetItem(plot_args, 1, yarray);
808 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_bar, plot_args, kwargs);
810 Py_DECREF(plot_args);
818 template <
typename Numeric>
819 bool bar(
const std::vector<Numeric> &
y,
820 std::string ec =
"black",
821 std::string ls =
"-",
823 const std::map<std::string, std::string> & keywords = {}) {
824 using T =
typename std::remove_reference<
decltype(
y)>::type::value_type;
827 for (std::size_t i = 0; i <
y.size(); i++) {
831 return bar(x,
y, ec, ls, lw, keywords);
835 detail::_interpreter::get();
836 PyObject * kwargs = PyDict_New();
837 for (std::map<std::string, double>::const_iterator it = keywords.begin(); it != keywords.end(); ++it) {
838 PyDict_SetItemString(kwargs, it->first.c_str(), PyFloat_FromDouble(it->second));
841 PyObject * plot_args = PyTuple_New(0);
844 PyObject_Call(detail::_interpreter::get().s_python_function_subplots_adjust, plot_args, kwargs);
846 Py_DECREF(plot_args);
854 template <
typename Numeric>
856 const std::vector<Numeric> &
y,
858 std::string color =
"b",
859 double alpha = 1.0) {
862 PyObject * kwargs = PyDict_New();
863 PyDict_SetItemString(kwargs,
"label", PyString_FromString(label.c_str()));
864 PyDict_SetItemString(kwargs,
"bins", PyLong_FromLong(bins));
865 PyDict_SetItemString(kwargs,
"color", PyString_FromString(color.c_str()));
866 PyDict_SetItemString(kwargs,
"alpha", PyFloat_FromDouble(
alpha));
868 PyObject * plot_args = PyTuple_New(1);
869 PyTuple_SetItem(plot_args, 0, yarray);
871 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_hist, plot_args, kwargs);
873 Py_DECREF(plot_args);
881 template <
typename NumericX,
typename NumericY>
882 bool plot(
const std::vector<NumericX> & x,
const std::vector<NumericY> &
y,
const std::string & s =
"") {
883 assert(x.size() ==
y.size());
888 PyObject * pystring = PyString_FromString(s.c_str());
890 PyObject * plot_args = PyTuple_New(3);
891 PyTuple_SetItem(plot_args, 0, xarray);
892 PyTuple_SetItem(plot_args, 1, yarray);
893 PyTuple_SetItem(plot_args, 2, pystring);
895 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_plot, plot_args);
897 Py_DECREF(plot_args);
904 template <
typename NumericX,
typename NumericY,
typename NumericU,
typename NumericW>
905 bool quiver(
const std::vector<NumericX> & x,
906 const std::vector<NumericY> &
y,
907 const std::vector<NumericU> &
u,
908 const std::vector<NumericW> & w,
909 const std::map<std::string, std::string> & keywords = {}) {
910 assert(x.size() ==
y.size() and x.size() ==
u.size() and
u.size() == w.size());
917 PyObject * plot_args = PyTuple_New(4);
918 PyTuple_SetItem(plot_args, 0, xarray);
919 PyTuple_SetItem(plot_args, 1, yarray);
920 PyTuple_SetItem(plot_args, 2, uarray);
921 PyTuple_SetItem(plot_args, 3, warray);
924 PyObject * kwargs = PyDict_New();
925 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
927 PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
930 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_quiver, plot_args, kwargs);
933 Py_DECREF(plot_args);
940 template <
typename NumericX,
typename NumericY>
941 bool stem(
const std::vector<NumericX> & x,
const std::vector<NumericY> &
y,
const std::string & s =
"") {
942 assert(x.size() ==
y.size());
947 PyObject * pystring = PyString_FromString(s.c_str());
949 PyObject * plot_args = PyTuple_New(3);
950 PyTuple_SetItem(plot_args, 0, xarray);
951 PyTuple_SetItem(plot_args, 1, yarray);
952 PyTuple_SetItem(plot_args, 2, pystring);
954 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_stem, plot_args);
956 Py_DECREF(plot_args);
963 template <
typename NumericX,
typename NumericY>
965 semilogx(
const std::vector<NumericX> & x,
const std::vector<NumericY> &
y,
const std::string & s =
"") {
966 assert(x.size() ==
y.size());
971 PyObject * pystring = PyString_FromString(s.c_str());
973 PyObject * plot_args = PyTuple_New(3);
974 PyTuple_SetItem(plot_args, 0, xarray);
975 PyTuple_SetItem(plot_args, 1, yarray);
976 PyTuple_SetItem(plot_args, 2, pystring);
978 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_semilogx, plot_args);
980 Py_DECREF(plot_args);
987 template <
typename NumericX,
typename NumericY>
989 semilogy(
const std::vector<NumericX> & x,
const std::vector<NumericY> &
y,
const std::string & s =
"") {
990 assert(x.size() ==
y.size());
995 PyObject * pystring = PyString_FromString(s.c_str());
997 PyObject * plot_args = PyTuple_New(3);
998 PyTuple_SetItem(plot_args, 0, xarray);
999 PyTuple_SetItem(plot_args, 1, yarray);
1000 PyTuple_SetItem(plot_args, 2, pystring);
1002 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_semilogy, plot_args);
1004 Py_DECREF(plot_args);
1011 template <
typename NumericX,
typename NumericY>
1012 bool loglog(
const std::vector<NumericX> & x,
const std::vector<NumericY> &
y,
const std::string & s =
"") {
1013 assert(x.size() ==
y.size());
1018 PyObject * pystring = PyString_FromString(s.c_str());
1020 PyObject * plot_args = PyTuple_New(3);
1021 PyTuple_SetItem(plot_args, 0, xarray);
1022 PyTuple_SetItem(plot_args, 1, yarray);
1023 PyTuple_SetItem(plot_args, 2, pystring);
1025 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_loglog, plot_args);
1027 Py_DECREF(plot_args);
1034 template <
typename NumericX,
typename NumericY>
1036 const std::vector<NumericY> &
y,
1037 const std::vector<NumericX> & yerr,
1038 const std::map<std::string, std::string> & keywords = {}) {
1039 assert(x.size() ==
y.size());
1046 PyObject * kwargs = PyDict_New();
1047 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
1049 PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
1052 PyDict_SetItemString(kwargs,
"yerr", yerrarray);
1054 PyObject * plot_args = PyTuple_New(2);
1055 PyTuple_SetItem(plot_args, 0, xarray);
1056 PyTuple_SetItem(plot_args, 1, yarray);
1059 PyObject_Call(detail::_interpreter::get().s_python_function_errorbar, plot_args, kwargs);
1062 Py_DECREF(plot_args);
1067 throw std::runtime_error(
"Call to errorbar() failed.");
1072 template <
typename Numeric>
1073 bool named_plot(
const std::string &
name,
const std::vector<Numeric> &
y,
const std::string & format =
"") {
1074 detail::_interpreter::get();
1075 PyObject * kwargs = PyDict_New();
1076 PyDict_SetItemString(kwargs,
"label", PyString_FromString(
name.c_str()));
1080 PyObject * pystring = PyString_FromString(format.c_str());
1082 PyObject * plot_args = PyTuple_New(2);
1084 PyTuple_SetItem(plot_args, 0, yarray);
1085 PyTuple_SetItem(plot_args, 1, pystring);
1087 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_plot, plot_args, kwargs);
1090 Py_DECREF(plot_args);
1097 template <
typename Numeric>
1099 const std::vector<Numeric> & x,
1100 const std::vector<Numeric> &
y,
1101 const std::string & format =
"") {
1102 detail::_interpreter::get();
1103 PyObject * kwargs = PyDict_New();
1104 PyDict_SetItemString(kwargs,
"label", PyString_FromString(
name.c_str()));
1109 PyObject * pystring = PyString_FromString(format.c_str());
1111 PyObject * plot_args = PyTuple_New(3);
1112 PyTuple_SetItem(plot_args, 0, xarray);
1113 PyTuple_SetItem(plot_args, 1, yarray);
1114 PyTuple_SetItem(plot_args, 2, pystring);
1116 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_plot, plot_args, kwargs);
1119 Py_DECREF(plot_args);
1126 template <
typename Numeric>
1128 const std::vector<Numeric> & x,
1129 const std::vector<Numeric> &
y,
1130 const std::string & format =
"") {
1131 detail::_interpreter::get();
1132 PyObject * kwargs = PyDict_New();
1133 PyDict_SetItemString(kwargs,
"label", PyString_FromString(
name.c_str()));
1138 PyObject * pystring = PyString_FromString(format.c_str());
1140 PyObject * plot_args = PyTuple_New(3);
1141 PyTuple_SetItem(plot_args, 0, xarray);
1142 PyTuple_SetItem(plot_args, 1, yarray);
1143 PyTuple_SetItem(plot_args, 2, pystring);
1146 PyObject_Call(detail::_interpreter::get().s_python_function_semilogx, plot_args, kwargs);
1149 Py_DECREF(plot_args);
1156 template <
typename Numeric>
1158 const std::vector<Numeric> & x,
1159 const std::vector<Numeric> &
y,
1160 const std::string & format =
"") {
1161 detail::_interpreter::get();
1162 PyObject * kwargs = PyDict_New();
1163 PyDict_SetItemString(kwargs,
"label", PyString_FromString(
name.c_str()));
1168 PyObject * pystring = PyString_FromString(format.c_str());
1170 PyObject * plot_args = PyTuple_New(3);
1171 PyTuple_SetItem(plot_args, 0, xarray);
1172 PyTuple_SetItem(plot_args, 1, yarray);
1173 PyTuple_SetItem(plot_args, 2, pystring);
1176 PyObject_Call(detail::_interpreter::get().s_python_function_semilogy, plot_args, kwargs);
1179 Py_DECREF(plot_args);
1186 template <
typename Numeric>
1188 const std::vector<Numeric> & x,
1189 const std::vector<Numeric> &
y,
1190 const std::string & format =
"") {
1191 detail::_interpreter::get();
1192 PyObject * kwargs = PyDict_New();
1193 PyDict_SetItemString(kwargs,
"label", PyString_FromString(
name.c_str()));
1198 PyObject * pystring = PyString_FromString(format.c_str());
1200 PyObject * plot_args = PyTuple_New(3);
1201 PyTuple_SetItem(plot_args, 0, xarray);
1202 PyTuple_SetItem(plot_args, 1, yarray);
1203 PyTuple_SetItem(plot_args, 2, pystring);
1204 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_loglog, plot_args, kwargs);
1207 Py_DECREF(plot_args);
1214 template <
typename Numeric>
1215 bool plot(
const std::vector<Numeric> &
y,
const std::string & format =
"") {
1216 std::vector<Numeric> x(
y.size());
1217 for (
size_t i = 0; i < x.size(); ++i)
1219 return plot(x,
y, format);
1222 template <
typename Numeric>
1223 bool plot(
const std::vector<Numeric> &
y,
const std::map<std::string, std::string> & keywords) {
1224 std::vector<Numeric> x(
y.size());
1225 for (
size_t i = 0; i < x.size(); ++i)
1227 return plot(x,
y, keywords);
1230 template <
typename Numeric>
1231 bool stem(
const std::vector<Numeric> &
y,
const std::string & format =
"") {
1232 std::vector<Numeric> x(
y.size());
1233 for (
size_t i = 0; i < x.size(); ++i)
1235 return stem(x,
y, format);
1238 template <
typename Numeric>
1239 void text(Numeric x, Numeric
y,
const std::string & s =
"") {
1240 detail::_interpreter::get();
1241 PyObject * args = PyTuple_New(3);
1242 PyTuple_SetItem(args, 0, PyFloat_FromDouble(x));
1243 PyTuple_SetItem(args, 1, PyFloat_FromDouble(
y));
1244 PyTuple_SetItem(args, 2, PyString_FromString(s.c_str()));
1246 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_text, args);
1248 throw std::runtime_error(
"Call to text() failed.");
1257 res = PyObject_CallObject(detail::_interpreter::get().s_python_function_figure,
1258 detail::_interpreter::get().s_python_empty_tuple);
1263 detail::_interpreter::get();
1265 PyObject * args = PyTuple_New(1);
1266 PyTuple_SetItem(args, 0, PyLong_FromLong(number));
1267 res = PyObject_CallObject(detail::_interpreter::get().s_python_function_figure, args);
1272 throw std::runtime_error(
"Call to figure() failed.");
1274 PyObject *
num = PyObject_GetAttrString(res,
"number");
1276 throw std::runtime_error(
"Could not get number attribute of figure object");
1277 const long figureNumber = PyLong_AsLong(
num);
1282 return figureNumber;
1287 detail::_interpreter::get();
1289 PyObject * args = PyTuple_New(1);
1290 PyTuple_SetItem(args, 0, PyLong_FromLong(number));
1291 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_fignum_exists, args);
1293 throw std::runtime_error(
"Call to fignum_exists() failed.");
1295 bool ret = PyObject_IsTrue(res);
1304 detail::_interpreter::get();
1306 const size_t dpi = 100;
1307 PyObject * size = PyTuple_New(2);
1308 PyTuple_SetItem(size, 0, PyFloat_FromDouble((
double)w / dpi));
1309 PyTuple_SetItem(size, 1, PyFloat_FromDouble((
double)
h / dpi));
1311 PyObject * kwargs = PyDict_New();
1312 PyDict_SetItemString(kwargs,
"figsize", size);
1313 PyDict_SetItemString(kwargs,
"dpi", PyLong_FromSize_t(dpi));
1315 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_figure,
1316 detail::_interpreter::get().s_python_empty_tuple,
1322 throw std::runtime_error(
"Call to figure_size() failed.");
1327 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_legend,
1328 detail::_interpreter::get().s_python_empty_tuple);
1330 throw std::runtime_error(
"Call to legend() failed.");
1335 template <
typename Numeric>
1336 void ylim(Numeric left, Numeric right) {
1337 detail::_interpreter::get();
1338 PyObject * list = PyList_New(2);
1339 PyList_SetItem(list, 0, PyFloat_FromDouble(left));
1340 PyList_SetItem(list, 1, PyFloat_FromDouble(right));
1342 PyObject * args = PyTuple_New(1);
1343 PyTuple_SetItem(args, 0, list);
1345 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_ylim, args);
1347 throw std::runtime_error(
"Call to ylim() failed.");
1353 template <
typename Numeric>
1354 void xlim(Numeric left, Numeric right) {
1355 detail::_interpreter::get();
1356 PyObject * list = PyList_New(2);
1357 PyList_SetItem(list, 0, PyFloat_FromDouble(left));
1358 PyList_SetItem(list, 1, PyFloat_FromDouble(right));
1360 PyObject * args = PyTuple_New(1);
1361 PyTuple_SetItem(args, 0, list);
1363 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_xlim, args);
1365 throw std::runtime_error(
"Call to xlim() failed.");
1372 detail::_interpreter::get();
1373 PyObject * args = PyTuple_New(0);
1374 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_xlim, args);
1375 PyObject * left = PyTuple_GetItem(res, 0);
1376 PyObject * right = PyTuple_GetItem(res, 1);
1378 double * arr =
new double[2];
1379 arr[0] = PyFloat_AsDouble(left);
1380 arr[1] = PyFloat_AsDouble(right);
1383 throw std::runtime_error(
"Call to xlim() failed.");
1390 detail::_interpreter::get();
1391 PyObject * args = PyTuple_New(0);
1392 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_ylim, args);
1393 PyObject * left = PyTuple_GetItem(res, 0);
1394 PyObject * right = PyTuple_GetItem(res, 1);
1396 double * arr =
new double[2];
1397 arr[0] = PyFloat_AsDouble(left);
1398 arr[1] = PyFloat_AsDouble(right);
1401 throw std::runtime_error(
"Call to ylim() failed.");
1407 template <
typename Numeric>
1408 inline void xticks(
const std::vector<Numeric> & ticks,
1409 const std::vector<std::string> & labels = {},
1410 const std::map<std::string, std::string> & keywords = {}) {
1411 assert(labels.size() == 0 || ticks.size() == labels.size());
1414 PyObject * ticksarray =
get_array(ticks);
1417 if (labels.size() == 0) {
1419 args = PyTuple_New(1);
1420 PyTuple_SetItem(args, 0, ticksarray);
1423 PyObject * labelstuple = PyTuple_New(labels.size());
1424 for (
size_t i = 0; i < labels.size(); i++)
1425 PyTuple_SetItem(labelstuple, i, PyUnicode_FromString(labels[i].c_str()));
1428 args = PyTuple_New(2);
1429 PyTuple_SetItem(args, 0, ticksarray);
1430 PyTuple_SetItem(args, 1, labelstuple);
1434 PyObject * kwargs = PyDict_New();
1435 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
1437 PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
1440 PyObject *
res = PyObject_Call(detail::_interpreter::get().s_python_function_xticks, args, kwargs);
1445 throw std::runtime_error(
"Call to xticks() failed");
1450 template <
typename Numeric>
1451 inline void xticks(
const std::vector<Numeric> & ticks,
1452 const std::map<std::string, std::string> & keywords) {
1453 xticks(ticks, {}, keywords);
1456 template <
typename Numeric>
1457 inline void yticks(
const std::vector<Numeric> & ticks,
1458 const std::vector<std::string> & labels = {},
1459 const std::map<std::string, std::string> & keywords = {}) {
1460 assert(labels.size() == 0 || ticks.size() == labels.size());
1463 PyObject * ticksarray =
get_array(ticks);
1466 if (labels.size() == 0) {
1468 args = PyTuple_New(1);
1469 PyTuple_SetItem(args, 0, ticksarray);
1472 PyObject * labelstuple = PyTuple_New(labels.size());
1473 for (
size_t i = 0; i < labels.size(); i++)
1474 PyTuple_SetItem(labelstuple, i, PyUnicode_FromString(labels[i].c_str()));
1477 args = PyTuple_New(2);
1478 PyTuple_SetItem(args, 0, ticksarray);
1479 PyTuple_SetItem(args, 1, labelstuple);
1483 PyObject * kwargs = PyDict_New();
1484 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
1486 PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
1489 PyObject *
res = PyObject_Call(detail::_interpreter::get().s_python_function_yticks, args, kwargs);
1494 throw std::runtime_error(
"Call to yticks() failed");
1499 template <
typename Numeric>
1500 inline void yticks(
const std::vector<Numeric> & ticks,
1501 const std::map<std::string, std::string> & keywords) {
1502 yticks(ticks, {}, keywords);
1505 inline void tick_params(
const std::map<std::string, std::string> & keywords,
1506 const std::string
axis =
"both") {
1507 detail::_interpreter::get();
1510 args = PyTuple_New(1);
1511 PyTuple_SetItem(args, 0, PyString_FromString(
axis.c_str()));
1514 PyObject * kwargs = PyDict_New();
1515 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
1517 PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
1520 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_tick_params, args, kwargs);
1525 throw std::runtime_error(
"Call to tick_params() failed");
1530 inline void subplot(
long nrows,
long ncols,
long plot_number) {
1531 detail::_interpreter::get();
1533 PyObject * args = PyTuple_New(3);
1534 PyTuple_SetItem(args, 0, PyFloat_FromDouble(nrows));
1535 PyTuple_SetItem(args, 1, PyFloat_FromDouble(ncols));
1536 PyTuple_SetItem(args, 2, PyFloat_FromDouble(plot_number));
1538 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_subplot, args);
1540 throw std::runtime_error(
"Call to subplot() failed.");
1547 subplot2grid(
long nrows,
long ncols,
long rowid = 0,
long colid = 0,
long rowspan = 1,
long colspan = 1) {
1548 detail::_interpreter::get();
1549 PyObject *
shape = PyTuple_New(2);
1550 PyTuple_SetItem(
shape, 0, PyLong_FromLong(nrows));
1551 PyTuple_SetItem(
shape, 1, PyLong_FromLong(ncols));
1553 PyObject * loc = PyTuple_New(2);
1554 PyTuple_SetItem(loc, 0, PyLong_FromLong(rowid));
1555 PyTuple_SetItem(loc, 1, PyLong_FromLong(colid));
1557 PyObject * args = PyTuple_New(4);
1558 PyTuple_SetItem(args, 0,
shape);
1559 PyTuple_SetItem(args, 1, loc);
1560 PyTuple_SetItem(args, 2, PyLong_FromLong(rowspan));
1561 PyTuple_SetItem(args, 3, PyLong_FromLong(colspan));
1563 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_subplot2grid, args);
1565 throw std::runtime_error(
"Call to subplot2grid() failed.");
1573 inline void title(
const std::string & titlestr,
const std::map<std::string, std::string> & keywords = {}) {
1574 detail::_interpreter::get();
1575 PyObject * pytitlestr = PyString_FromString(titlestr.c_str());
1576 PyObject * args = PyTuple_New(1);
1577 PyTuple_SetItem(args, 0, pytitlestr);
1579 PyObject * kwargs = PyDict_New();
1580 for (
auto it = keywords.begin(); it != keywords.end(); ++it) {
1581 PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
1584 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_title, args, kwargs);
1586 throw std::runtime_error(
"Call to title() failed.");
1594 const std::map<std::string, std::string> & keywords = {}) {
1595 detail::_interpreter::get();
1596 PyObject * pysuptitlestr = PyString_FromString(suptitlestr.c_str());
1597 PyObject * args = PyTuple_New(1);
1598 PyTuple_SetItem(args, 0, pysuptitlestr);
1600 PyObject * kwargs = PyDict_New();
1601 for (
auto it = keywords.begin(); it != keywords.end(); ++it) {
1602 PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
1605 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_suptitle, args, kwargs);
1607 throw std::runtime_error(
"Call to suptitle() failed.");
1614 inline void axis(
const std::string & axisstr) {
1615 detail::_interpreter::get();
1616 PyObject * str = PyString_FromString(axisstr.c_str());
1617 PyObject * args = PyTuple_New(1);
1618 PyTuple_SetItem(args, 0, str);
1620 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_axis, args);
1622 throw std::runtime_error(
"Call to title() failed.");
1629 detail::_interpreter::get();
1630 PyObject * str = PyString_FromString(
xscale.c_str());
1631 PyObject * args = PyTuple_New(1);
1632 PyTuple_SetItem(args, 0, str);
1634 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_xscale, args);
1636 throw std::runtime_error(
"Call to title() failed.");
1643 detail::_interpreter::get();
1644 PyObject * str = PyString_FromString(
yscale.c_str());
1645 PyObject * args = PyTuple_New(1);
1646 PyTuple_SetItem(args, 0, str);
1648 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_yscale, args);
1650 throw std::runtime_error(
"Call to title() failed.");
1656 inline void xlabel(
const std::string & str,
const std::map<std::string, std::string> & keywords = {}) {
1657 detail::_interpreter::get();
1658 PyObject * pystr = PyString_FromString(str.c_str());
1659 PyObject * args = PyTuple_New(1);
1660 PyTuple_SetItem(args, 0, pystr);
1662 PyObject * kwargs = PyDict_New();
1663 for (
auto it = keywords.begin(); it != keywords.end(); ++it) {
1664 PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
1667 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_xlabel, args, kwargs);
1669 throw std::runtime_error(
"Call to xlabel() failed.");
1676 inline void ylabel(
const std::string & str,
const std::map<std::string, std::string> & keywords = {}) {
1677 detail::_interpreter::get();
1678 PyObject * pystr = PyString_FromString(str.c_str());
1679 PyObject * args = PyTuple_New(1);
1680 PyTuple_SetItem(args, 0, pystr);
1682 PyObject * kwargs = PyDict_New();
1683 for (
auto it = keywords.begin(); it != keywords.end(); ++it) {
1684 PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
1687 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_ylabel, args, kwargs);
1689 throw std::runtime_error(
"Call to ylabel() failed.");
1697 detail::_interpreter::get();
1698 PyObject * pyflag = flag ? Py_True : Py_False;
1701 PyObject * args = PyTuple_New(1);
1702 PyTuple_SetItem(args, 0, pyflag);
1704 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_grid, args);
1706 throw std::runtime_error(
"Call to grid() failed.");
1712 inline void show(
const bool block =
true) {
1715 res = PyObject_CallObject(detail::_interpreter::get().s_python_function_show,
1716 detail::_interpreter::get().s_python_empty_tuple);
1718 PyObject * kwargs = PyDict_New();
1719 PyDict_SetItemString(kwargs,
"block", Py_False);
1720 res = PyObject_Call(detail::_interpreter::get().s_python_function_show,
1721 detail::_interpreter::get().s_python_empty_tuple,
1727 throw std::runtime_error(
"Call to show() failed.");
1733 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_close,
1734 detail::_interpreter::get().s_python_empty_tuple);
1737 throw std::runtime_error(
"Call to close() failed.");
1743 detail::_interpreter::get();
1745 PyObject * kwargs = PyDict_New();
1747 res = PyObject_Call(detail::_interpreter::get().s_python_function_xkcd,
1748 detail::_interpreter::get().s_python_empty_tuple,
1754 throw std::runtime_error(
"Call to show() failed.");
1760 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_draw,
1761 detail::_interpreter::get().s_python_empty_tuple);
1764 throw std::runtime_error(
"Call to draw() failed.");
1769 template <
typename Numeric>
1771 detail::_interpreter::get();
1772 PyObject * args = PyTuple_New(1);
1773 PyTuple_SetItem(args, 0, PyFloat_FromDouble(interval));
1775 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_pause, args);
1777 throw std::runtime_error(
"Call to pause() failed.");
1784 detail::_interpreter::get();
1785 PyObject * pyfilename = PyString_FromString(
filename.c_str());
1787 PyObject * args = PyTuple_New(1);
1788 PyTuple_SetItem(args, 0, pyfilename);
1790 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_save, args);
1792 throw std::runtime_error(
"Call to save() failed.");
1799 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_clf,
1800 detail::_interpreter::get().s_python_empty_tuple);
1803 throw std::runtime_error(
"Call to clf() failed.");
1809 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_ion,
1810 detail::_interpreter::get().s_python_empty_tuple);
1813 throw std::runtime_error(
"Call to ion() failed.");
1818 inline std::vector<std::array<double, 2>>
ginput(
const int numClicks = 1,
1819 const std::map<std::string, std::string> & keywords = {}) {
1820 detail::_interpreter::get();
1821 PyObject * args = PyTuple_New(1);
1822 PyTuple_SetItem(args, 0, PyLong_FromLong(numClicks));
1825 PyObject * kwargs = PyDict_New();
1826 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
1828 PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
1831 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_ginput, args, kwargs);
1836 throw std::runtime_error(
"Call to ginput() failed.");
1838 const size_t len = PyList_Size(res);
1839 std::vector<std::array<double, 2>> out;
1841 for (
size_t i = 0; i < len; i++) {
1842 PyObject * current = PyList_GetItem(res, i);
1843 std::array<double, 2> position;
1844 position[0] = PyFloat_AsDouble(PyTuple_GetItem(current, 0));
1845 position[1] = PyFloat_AsDouble(PyTuple_GetItem(current, 1));
1846 out.push_back(position);
1855 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_tight_layout,
1856 detail::_interpreter::get().s_python_empty_tuple);
1859 throw std::runtime_error(
"Call to tight_layout() failed.");
1868 template <
typename T>
1869 using is_function =
typename std::is_function<std::remove_pointer<std::remove_reference<T>>>::type;
1871 template <
bool obj,
typename T>
1872 struct is_callable_impl;
1874 template <
typename T>
1875 struct is_callable_impl<false, T> {
1876 typedef is_function<T> type;
1879 template <
typename T>
1880 struct is_callable_impl<true, T> {
1884 struct Derived : T, Fallback {};
1886 template <
typename U, U>
1889 template <
typename U>
1890 static std::true_type
test(...);
1893 template <
typename U>
1894 static std::false_type
test(Check<
void (Fallback::*)(), &
U::operator()> *);
1897 typedef decltype(test<Derived>(
nullptr)) type;
1898 typedef decltype(&Fallback::operator()) dtype;
1899 static constexpr bool value = type::value;
1902 template <
typename T>
1903 struct is_callable {
1906 typedef typename is_callable_impl<std::is_class<T>::value, T>::type
type;
1909 template <
typename IsYDataCallable>
1910 struct plot_impl {};
1913 struct plot_impl<std::false_type> {
1914 template <
typename IterableX,
typename IterableY>
1915 bool operator()(
const IterableX & x,
const IterableY &
y,
const std::string & format) {
1918 using std::distance;
1923 assert(xs == ys and
"x and y data must have the same number of elements!");
1925 PyObject * xlist = PyList_New(xs);
1926 PyObject * ylist = PyList_New(ys);
1927 PyObject * pystring = PyString_FromString(format.c_str());
1929 auto itx = begin(x), ity = begin(
y);
1930 for (
size_t i = 0;
i <
xs; ++
i) {
1931 PyList_SetItem(xlist, i, PyFloat_FromDouble(*itx++));
1932 PyList_SetItem(ylist, i, PyFloat_FromDouble(*ity++));
1935 PyObject * plot_args = PyTuple_New(3);
1936 PyTuple_SetItem(plot_args, 0, xlist);
1937 PyTuple_SetItem(plot_args, 1, ylist);
1938 PyTuple_SetItem(plot_args, 2, pystring);
1941 PyObject_CallObject(detail::_interpreter::get().s_python_function_plot, plot_args);
1943 Py_DECREF(plot_args);
1952 struct plot_impl<std::true_type> {
1953 template <
typename Iterable,
typename Callable>
1954 bool operator()(
const Iterable & ticks,
const Callable &
f,
const std::string & format) {
1955 if (begin(ticks) == end(ticks))
1960 std::vector<double>
y;
1961 for (
auto x : ticks)
1963 return plot_impl<std::false_type>()(ticks,
y, format);
1970 template <
typename... Args>
1975 template <
typename A,
typename B,
typename... Args>
1976 bool plot(
const A & a,
const B & b,
const std::string & format, Args... args) {
1977 return detail::plot_impl<typename detail::is_callable<B>::type>()(a, b, format) &&
plot(args...);
1985 plot(
const std::vector<double> & x,
const std::vector<double> &
y,
const std::string & format =
"") {
1989 inline bool plot(
const std::vector<double> &
y,
const std::string & format =
"") {
1993 inline bool plot(
const std::vector<double> & x,
1994 const std::vector<double> &
y,
1995 const std::map<std::string, std::string> & keywords) {
2007 template <
typename Numeric>
2009 const std::vector<Numeric> & x,
2010 const std::vector<Numeric> &
y,
2011 const std::string & format =
"") {
2012 assert(x.size() ==
y.size());
2014 PyObject * kwargs = PyDict_New();
2016 PyDict_SetItemString(kwargs,
"label", PyString_FromString(
name.c_str()));
2021 PyObject * pystring = PyString_FromString(format.c_str());
2023 PyObject * plot_args = PyTuple_New(3);
2024 PyTuple_SetItem(plot_args, 0, xarray);
2025 PyTuple_SetItem(plot_args, 1, yarray);
2026 PyTuple_SetItem(plot_args, 2, pystring);
2029 PyObject_Call(detail::_interpreter::get().s_python_function_plot, plot_args, kwargs);
2032 Py_DECREF(plot_args);
2035 line = PyList_GetItem(res, 0);
2038 set_data_fct = PyObject_GetAttrString(line,
"set_data");
2047 Plot(
const std::string &
name =
"",
const std::string & format =
"")
2048 :
Plot(
name, std::vector<double>(), std::vector<double>(), format) {}
2050 template <
typename Numeric>
2051 bool update(
const std::vector<Numeric> & x,
const std::vector<Numeric> &
y) {
2052 assert(x.size() ==
y.size());
2057 PyObject * plot_args = PyTuple_New(2);
2058 PyTuple_SetItem(plot_args, 0, xarray);
2059 PyTuple_SetItem(plot_args, 1, yarray);
2061 PyObject * res = PyObject_CallObject(set_data_fct, plot_args);
2070 bool clear() {
return update(std::vector<double>(), std::vector<double>()); }
2075 auto remove_fct = PyObject_GetAttrString(line,
"remove");
2076 PyObject * args = PyTuple_New(0);
2077 PyObject * res = PyObject_CallObject(remove_fct, args);
2091 Py_DECREF(set_data_fct);
2094 PyObject * line =
nullptr;
2095 PyObject * set_data_fct =
nullptr;