From c70ad9613c67f7ecb7f369fb04750cbb22176726 Mon Sep 17 00:00:00 2001 From: Cooper Harasyn Date: Fri, 5 Oct 2018 16:05:25 -0400 Subject: [PATCH 1/3] Added text and suptitle --- examples/subplot.cpp | 31 +++++++++++++++++++++++++++++++ matplotlibcpp.h | 43 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 examples/subplot.cpp diff --git a/examples/subplot.cpp b/examples/subplot.cpp new file mode 100644 index 0000000..bee322e --- /dev/null +++ b/examples/subplot.cpp @@ -0,0 +1,31 @@ +#define _USE_MATH_DEFINES +#include +#include "../matplotlibcpp.h" + +using namespace std; +namespace plt = matplotlibcpp; + +int main() +{ + // Prepare data + int n = 500; + std::vector x(n), y(n), z(n), w(n,2); + for(int i=0; i& y, const std::string& format = "") return stem(x, y, format); } +template +void text(Numeric x, Numeric y, const std::string& format = "") +{ + PyObject* args = PyTuple_New(3); + PyTuple_SetItem(args, 0, PyFloat_FromDouble(x)); + PyTuple_SetItem(args, 1, PyFloat_FromDouble(y)); + PyTuple_SetItem(args, 2, PyString_FromString(format.c_str())); + + PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_text, args); + if(!res) throw std::runtime_error("Call to text() failed."); + + Py_DECREF(args); + Py_DECREF(res); +} + + inline void figure() { PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_figure, detail::_interpreter::get().s_python_empty_tuple); @@ -769,7 +793,7 @@ inline void figure_size(size_t w, size_t h) PyDict_SetItemString(kwargs, "figsize", size); PyDict_SetItemString(kwargs, "dpi", PyLong_FromSize_t(dpi)); - PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_figure, + PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_figure, detail::_interpreter::get().s_python_empty_tuple, kwargs); Py_DECREF(kwargs); @@ -893,7 +917,7 @@ inline void xticks(const std::vector &ticks, const std::vector &ticks, const std::vector Date: Fri, 5 Oct 2018 16:08:40 -0400 Subject: [PATCH 2/3] Fixed whitespace --- matplotlibcpp.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index a71bf8a..c3153e7 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -793,7 +793,7 @@ inline void figure_size(size_t w, size_t h) PyDict_SetItemString(kwargs, "figsize", size); PyDict_SetItemString(kwargs, "dpi", PyLong_FromSize_t(dpi)); - PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_figure, + PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_figure, detail::_interpreter::get().s_python_empty_tuple, kwargs); Py_DECREF(kwargs); @@ -917,7 +917,7 @@ inline void xticks(const std::vector &ticks, const std::vector &ticks, const std::vector Date: Fri, 5 Oct 2018 16:10:17 -0400 Subject: [PATCH 3/3] Chose a better name for the parameter to text --- matplotlibcpp.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index c3153e7..7b5f88d 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -759,12 +759,12 @@ bool stem(const std::vector& y, const std::string& format = "") } template -void text(Numeric x, Numeric y, const std::string& format = "") +void text(Numeric x, Numeric y, const std::string& s = "") { PyObject* args = PyTuple_New(3); PyTuple_SetItem(args, 0, PyFloat_FromDouble(x)); PyTuple_SetItem(args, 1, PyFloat_FromDouble(y)); - PyTuple_SetItem(args, 2, PyString_FromString(format.c_str())); + PyTuple_SetItem(args, 2, PyString_FromString(s.c_str())); PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_text, args); if(!res) throw std::runtime_error("Call to text() failed.");