Skip to content

Add kwargs for text and annotate #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 17 additions & 4 deletions matplotlibcpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string> &keywords = {}) {
detail::_interpreter::get();

PyObject *xy = PyTuple_New(2);
Expand All @@ -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);

Expand Down Expand Up @@ -1300,16 +1306,23 @@ bool stem(const std::vector<Numeric> &y, const std::string &format = "") {
}

template <typename Numeric>
void text(Numeric x, Numeric y, const std::string &s = "") {
void text(Numeric x, Numeric y, const std::string &s = "",
const std::map<std::string, std::string> &keywords = {}) {
detail::_interpreter::get();

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(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.");

Expand Down