From b26af032c53b64fbe53f5e405f78205cddd2c64b Mon Sep 17 00:00:00 2001 From: Conrad Parker Date: Mon, 23 Oct 2023 08:08:23 +1100 Subject: [PATCH] Add kwargs for text and annotate --- matplotlibcpp.h | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index d93eb6f..8e118f8 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -316,7 +316,8 @@ struct _interpreter { // must be called before the first regular call to matplotlib to have any effect inline void backend(const std::string &name) { detail::s_backend = name; } -inline bool annotate(std::string annotation, double x, double y) { +inline bool annotate(std::string annotation, double x, double y, + const std::map &keywords = {}) { detail::_interpreter::get(); PyObject *xy = PyTuple_New(2); @@ -328,6 +329,11 @@ inline bool annotate(std::string annotation, double x, double y) { PyObject *kwargs = PyDict_New(); PyDict_SetItemString(kwargs, "xy", xy); + for (auto const &item : keywords) { + PyDict_SetItemString(kwargs, item.first.c_str(), + PyString_FromString(item.second.c_str())); + } + PyObject *args = PyTuple_New(1); PyTuple_SetItem(args, 0, str); @@ -1300,7 +1306,8 @@ bool stem(const std::vector &y, const std::string &format = "") { } template -void text(Numeric x, Numeric y, const std::string &s = "") { +void text(Numeric x, Numeric y, const std::string &s = "", + const std::map &keywords = {}) { detail::_interpreter::get(); PyObject *args = PyTuple_New(3); @@ -1308,8 +1315,14 @@ void text(Numeric x, Numeric y, const std::string &s = "") { PyTuple_SetItem(args, 1, PyFloat_FromDouble(y)); PyTuple_SetItem(args, 2, PyString_FromString(s.c_str())); - PyObject *res = PyObject_CallObject( - detail::_interpreter::get().s_python_function_text, args); + PyObject *kwargs = PyDict_New(); + for (auto const &item : keywords) { + PyDict_SetItemString(kwargs, item.first.c_str(), + PyString_FromString(item.second.c_str())); + } + + PyObject *res = PyObject_Call( + detail::_interpreter::get().s_python_function_text, args, kwargs); if (!res) throw std::runtime_error("Call to text() failed.");