From 63d0e15009a0694271801efacbd55b5a8152fbd3 Mon Sep 17 00:00:00 2001 From: "Yuma.M" Date: Wed, 16 Aug 2017 12:51:00 +0900 Subject: [PATCH] Support draw and pause functions --- matplotlibcpp.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index 4e79c424..2ad734d8 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -31,6 +31,8 @@ namespace matplotlibcpp { struct _interpreter { PyObject *s_python_function_show; + PyObject *s_python_function_draw; + PyObject *s_python_function_pause; PyObject *s_python_function_save; PyObject *s_python_function_figure; PyObject *s_python_function_plot; @@ -105,6 +107,8 @@ namespace matplotlibcpp { if(!pylabmod) { throw std::runtime_error("Error loading module pylab!"); } s_python_function_show = PyObject_GetAttrString(pymod, "show"); + s_python_function_draw = PyObject_GetAttrString(pymod, "draw"); + s_python_function_pause = PyObject_GetAttrString(pymod, "pause"); s_python_function_figure = PyObject_GetAttrString(pymod, "figure"); s_python_function_plot = PyObject_GetAttrString(pymod, "plot"); s_python_function_fill_between = PyObject_GetAttrString(pymod, "fill_between"); @@ -125,6 +129,8 @@ namespace matplotlibcpp { s_python_function_tight_layout = PyObject_GetAttrString(pymod, "tight_layout"); if( !s_python_function_show + || !s_python_function_draw + || !s_python_function_pause || !s_python_function_figure || !s_python_function_plot || !s_python_function_fill_between @@ -146,6 +152,8 @@ namespace matplotlibcpp { ) { throw std::runtime_error("Couldn't find required function!"); } if ( !PyFunction_Check(s_python_function_show) + || !PyFunction_Check(s_python_function_draw) + || !PyFunction_Check(s_python_function_pause) || !PyFunction_Check(s_python_function_figure) || !PyFunction_Check(s_python_function_plot) || !PyFunction_Check(s_python_function_fill_between) @@ -651,6 +659,30 @@ namespace matplotlibcpp { Py_DECREF(res); } + inline void draw() + { + PyObject* res = PyObject_CallObject( + detail::_interpreter::get().s_python_function_draw, + detail::_interpreter::get().s_python_empty_tuple); + + if (!res) throw std::runtime_error("Call to draw() failed."); + + Py_DECREF(res); + } + + template + void pause(Numeric interval) + { + PyObject* args = PyTuple_New(1); + PyTuple_SetItem(args, 0, PyFloat_FromDouble(interval)); + + PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_pause, args); + if(!res) throw std::runtime_error("Call to pause() failed."); + + Py_DECREF(args); + Py_DECREF(res); + } + inline void save(const std::string& filename) { PyObject* pyfilename = PyString_FromString(filename.c_str());