Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions matplotlibcpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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<typename Numeric>
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());
Expand Down