From 84148ab721450a45c0e390409d2c5fbe4e6cb071 Mon Sep 17 00:00:00 2001 From: Hashim Abdul Date: Tue, 2 Mar 2021 08:53:42 -0500 Subject: [PATCH] template to handle strings on x axis w/ legend bool named_plot(const std::string& name, const std::vector& x, const std::vector& y, const std::string& format = "") { .. } added template to be able to have named legend plotting: ex: time(x) vs signal(y) with legend. Prior to this change, the current library implements simple plots with no legends and with this change the above should be possible. --- matplotlibcpp.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index 93a72be..f8858c9 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -1394,6 +1394,33 @@ bool named_plot(const std::string& name, const std::vector& x, const st return res; } +template +bool named_plot(const std::string& name, const std::vector& x, const std::vector& y, const std::string& format = "") +{ + detail::_interpreter::get(); + + PyObject* kwargs = PyDict_New(); + PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str())); + + PyObject* xarray = detail::get_array(x); + PyObject* yarray = detail::get_array(y); + + PyObject* pystring = PyString_FromString(format.c_str()); + + PyObject* plot_args = PyTuple_New(3); + PyTuple_SetItem(plot_args, 0, xarray); + PyTuple_SetItem(plot_args, 1, yarray); + PyTuple_SetItem(plot_args, 2, pystring); + + PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_plot, plot_args, kwargs); + + Py_DECREF(kwargs); + Py_DECREF(plot_args); + if (res) Py_DECREF(res); + + return res; +} + template bool named_semilogx(const std::string& name, const std::vector& x, const std::vector& y, const std::string& format = "") {