From a179e3b57a0774316610235336428dd774cd76a3 Mon Sep 17 00:00:00 2001 From: jacketsj Date: Mon, 31 Jul 2023 22:25:18 -0700 Subject: [PATCH 1/2] Add set_xscale and set_yscale --- matplotlibcpp.h | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index d95d46a..6169e67 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -70,6 +70,8 @@ struct _interpreter { PyObject *s_python_function_subplot2grid; PyObject *s_python_function_legend; PyObject *s_python_function_xlim; + PyObject *s_python_function_set_xscale; + PyObject *s_python_function_set_yscale; PyObject *s_python_function_ion; PyObject *s_python_function_ginput; PyObject *s_python_function_ylim; @@ -246,6 +248,8 @@ struct _interpreter { s_python_function_subplot2grid = safe_import(pymod, "subplot2grid"); s_python_function_legend = safe_import(pymod, "legend"); s_python_function_xlim = safe_import(pymod, "xlim"); + s_python_function_set_xscale = safe_import(pymod, "set_xscale"); + s_python_function_set_yscale = safe_import(pymod, "set_yscale"); s_python_function_ylim = safe_import(pymod, "ylim"); s_python_function_title = safe_import(pymod, "title"); s_python_function_axis = safe_import(pymod, "axis"); @@ -883,6 +887,52 @@ bool arrow(Numeric x, Numeric y, Numeric end_x, Numeric end_y, const std::string return res; } +void set_xscale(std::string value, + const std::map &keywords = {}) +{ + detail::_interpreter::get(); + + PyObject* pystr = PyString_FromString(value.c_str()); + PyObject* args = PyTuple_New(1); + PyTuple_SetItem(args, 0, pystr); + + PyObject* kwargs = PyDict_New(); + for (auto it = keywords.begin(); it != keywords.end(); ++it) { + PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str())); + } + + PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_set_xscale, args, kwargs); + + if(!res) throw std::runtime_error("Call to set_xscale() failed."); + + Py_DECREF(args); + Py_DECREF(kwargs); + Py_DECREF(res); +} + +void set_yscale(std::string value, + const std::map &keywords = {}) +{ + detail::_interpreter::get(); + + PyObject* pystr = PyString_FromString(value.c_str()); + PyObject* args = PyTuple_New(1); + PyTuple_SetItem(args, 0, pystr); + + PyObject* kwargs = PyDict_New(); + for (auto it = keywords.begin(); it != keywords.end(); ++it) { + PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str())); + } + + PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_set_yscale, args, kwargs); + + if(!res) throw std::runtime_error("Call to set_yscale() failed."); + + Py_DECREF(args); + Py_DECREF(kwargs); + Py_DECREF(res); +} + template< typename Numeric> bool hist(const std::vector& y, long bins=10,std::string color="b", double alpha=1.0, bool cumulative=false) From 4eac8bccdc4cce42a98bb29ca46a14a8a31dbf29 Mon Sep 17 00:00:00 2001 From: jacketsj Date: Mon, 31 Jul 2023 22:37:27 -0700 Subject: [PATCH 2/2] set_xscale->xscale and set_yscale->yscale --- matplotlibcpp.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index 6169e67..3b09100 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -70,8 +70,8 @@ struct _interpreter { PyObject *s_python_function_subplot2grid; PyObject *s_python_function_legend; PyObject *s_python_function_xlim; - PyObject *s_python_function_set_xscale; - PyObject *s_python_function_set_yscale; + PyObject *s_python_function_xscale; + PyObject *s_python_function_yscale; PyObject *s_python_function_ion; PyObject *s_python_function_ginput; PyObject *s_python_function_ylim; @@ -248,8 +248,8 @@ struct _interpreter { s_python_function_subplot2grid = safe_import(pymod, "subplot2grid"); s_python_function_legend = safe_import(pymod, "legend"); s_python_function_xlim = safe_import(pymod, "xlim"); - s_python_function_set_xscale = safe_import(pymod, "set_xscale"); - s_python_function_set_yscale = safe_import(pymod, "set_yscale"); + s_python_function_xscale = safe_import(pymod, "xscale"); + s_python_function_yscale = safe_import(pymod, "yscale"); s_python_function_ylim = safe_import(pymod, "ylim"); s_python_function_title = safe_import(pymod, "title"); s_python_function_axis = safe_import(pymod, "axis"); @@ -887,7 +887,7 @@ bool arrow(Numeric x, Numeric y, Numeric end_x, Numeric end_y, const std::string return res; } -void set_xscale(std::string value, +void xscale(std::string value, const std::map &keywords = {}) { detail::_interpreter::get(); @@ -901,16 +901,16 @@ void set_xscale(std::string value, PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str())); } - PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_set_xscale, args, kwargs); + PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_xscale, args, kwargs); - if(!res) throw std::runtime_error("Call to set_xscale() failed."); + if(!res) throw std::runtime_error("Call to xscale() failed."); Py_DECREF(args); Py_DECREF(kwargs); Py_DECREF(res); } -void set_yscale(std::string value, +void yscale(std::string value, const std::map &keywords = {}) { detail::_interpreter::get(); @@ -924,9 +924,9 @@ void set_yscale(std::string value, PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str())); } - PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_set_yscale, args, kwargs); + PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_yscale, args, kwargs); - if(!res) throw std::runtime_error("Call to set_yscale() failed."); + if(!res) throw std::runtime_error("Call to yscale() failed."); Py_DECREF(args); Py_DECREF(kwargs);