From b26af032c53b64fbe53f5e405f78205cddd2c64b Mon Sep 17 00:00:00 2001 From: Conrad Parker Date: Mon, 23 Oct 2023 08:08:23 +1100 Subject: [PATCH 1/3] 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."); From 79738777ea0234522a7bc1f1e926e2d99fd7850b Mon Sep 17 00:00:00 2001 From: Conrad Parker Date: Fri, 27 Oct 2023 09:18:52 +1100 Subject: [PATCH 2/3] Fix compile warnings --- matplotlibcpp.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index 8e118f8..8048bf1 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -457,8 +457,8 @@ template PyObject *get_2darray(const Matrix &A) { double *vd_begin = static_cast(PyArray_DATA(varray)); - for (std::size_t i = 0; i < A.rows(); ++i) { - for (std::size_t j = 0; j < A.cols(); ++j) { + for (ssize_t i = 0; i < A.rows(); ++i) { + for (ssize_t j = 0; j < A.cols(); ++j) { *(vd_begin + i * A.cols() + j) = A(i, j); } } @@ -550,10 +550,10 @@ bool plot(const VectorY &y, const std::string &format = "", // Note: cannot be an unsigned type for some reason, yields an overflow // problem.. std::vector x(y.size()); - for (int i = 0; i < x.size(); ++i) + for (size_t i = 0; i < x.size(); ++i) x.at(i) = i; - return plot(x, y, format); + return plot(x, y, format, keywords); } // @brief standard plot function if x data is not specified and the formatting From 3d5b1dbb751d7c08200db938b0905c4c52cfad15 Mon Sep 17 00:00:00 2001 From: Conrad Parker Date: Fri, 27 Oct 2023 09:33:29 +1100 Subject: [PATCH 3/3] Fix warnings --- matplotlibcpp.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index 8048bf1..e217653 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -550,7 +550,7 @@ bool plot(const VectorY &y, const std::string &format = "", // Note: cannot be an unsigned type for some reason, yields an overflow // problem.. std::vector x(y.size()); - for (size_t i = 0; i < x.size(); ++i) + for (std::size_t i = 0; i < x.size(); ++i) x.at(i) = i; return plot(x, y, format, keywords); @@ -562,7 +562,7 @@ template > bool plot(const VectorY &y, const std::map &keywords) { std::vector x(y.size()); - for (int i = 0; i < x.size(); ++i) + for (std::size_t i = 0; i < x.size(); ++i) x.at(i) = i; return plot(x, y, "", keywords); @@ -1075,6 +1075,11 @@ bool bar(const std::vector &y, std::string ec = "black", PyDict_SetItemString(kwargs, "ls", PyString_FromString(ls.c_str())); PyDict_SetItemString(kwargs, "lw", PyFloat_FromDouble(lw)); + for (auto const &item : keywords) { + PyDict_SetItemString(kwargs, item.first.c_str(), + PyString_FromString(item.second.c_str())); + } + PyObject *plot_args = PyTuple_New(2); PyTuple_SetItem(plot_args, 0, xarray); PyTuple_SetItem(plot_args, 1, yarray);