From ef4af5b1371c3d7beeada653045300ca36400285 Mon Sep 17 00:00:00 2001 From: AtsushiSakai Date: Sat, 16 Jan 2016 14:14:54 +0900 Subject: [PATCH 1/5] add title function --- examples/basic.cpp | 4 +++- matplotlibcpp.h | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/examples/basic.cpp b/examples/basic.cpp index deae814..de6ac42 100644 --- a/examples/basic.cpp +++ b/examples/basic.cpp @@ -24,8 +24,10 @@ int main() // Set x-axis to interval [0,1000000] plt::xlim(0, 1000*1000); + // Add graph title + plt::title("sample figure"); // Enable legend. plt::legend(); - // Show plot + // save figure plt::save("./basic.png"); } diff --git a/matplotlibcpp.h b/matplotlibcpp.h index bb4cd0f..68f3425 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -23,6 +23,7 @@ namespace matplotlibcpp { PyObject *s_python_function_legend; PyObject *s_python_function_xlim; PyObject *s_python_function_ylim; + PyObject *s_python_function_title; PyObject *s_python_empty_tuple; /* For now, _interpreter is implemented as a singleton since its currently not possible to have @@ -60,6 +61,7 @@ namespace matplotlibcpp { s_python_function_plot = PyObject_GetAttrString(pymod, "plot"); s_python_function_legend = PyObject_GetAttrString(pymod, "legend"); s_python_function_ylim = PyObject_GetAttrString(pymod, "ylim"); + s_python_function_title = PyObject_GetAttrString(pymod, "title"); s_python_function_xlim = PyObject_GetAttrString(pymod, "xlim"); s_python_function_save = PyObject_GetAttrString(pylabmod, "savefig"); @@ -70,6 +72,7 @@ namespace matplotlibcpp { || !s_python_function_plot || !s_python_function_legend || !s_python_function_xlim + || !s_python_function_title || !s_python_function_ylim) { throw std::runtime_error("Couldnt find required function!"); } @@ -79,6 +82,7 @@ namespace matplotlibcpp { || !PyFunction_Check(s_python_function_plot) || !PyFunction_Check(s_python_function_legend) || !PyFunction_Check(s_python_function_xlim) + || !PyFunction_Check(s_python_function_title) || !PyFunction_Check(s_python_function_ylim)) { throw std::runtime_error("Python object is unexpectedly not a PyFunction."); } @@ -244,6 +248,21 @@ namespace matplotlibcpp { Py_DECREF(res); } + inline void title(const std::string &titlestr) + { + PyObject* pytitlestr = PyString_FromString(titlestr.c_str()); + PyObject* args = PyTuple_New(1); + PyTuple_SetItem(args, 0, pytitlestr); + + PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_title, + args); + if(!res) throw std::runtime_error("Call to title() failed."); + + // if(pytitlestr) Py_DECREF(pytitlestr); + // if(args) Py_DECREF(args); + // if(res) Py_DECREF(res); + } + inline void show() { PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_show, detail::_interpreter::get().s_python_empty_tuple); From 1a3e848c302169acaa376cafa041613244c26a85 Mon Sep 17 00:00:00 2001 From: AtsushiSakai Date: Sat, 16 Jan 2016 14:17:00 +0900 Subject: [PATCH 2/5] remove comment --- matplotlibcpp.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index 68f3425..6bbd329 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -258,9 +258,8 @@ namespace matplotlibcpp { args); if(!res) throw std::runtime_error("Call to title() failed."); - // if(pytitlestr) Py_DECREF(pytitlestr); - // if(args) Py_DECREF(args); - // if(res) Py_DECREF(res); + //if PyDeCRFF, the show function doesn't wook on Mac OS + } inline void show() From 622e27638d16f40d3289cdb55d9747a574c874af Mon Sep 17 00:00:00 2001 From: AtsushiSakai Date: Sat, 16 Jan 2016 14:20:31 +0900 Subject: [PATCH 3/5] format code --- matplotlibcpp.h | 1 - 1 file changed, 1 deletion(-) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index 6bbd329..63cde89 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -259,7 +259,6 @@ namespace matplotlibcpp { if(!res) throw std::runtime_error("Call to title() failed."); //if PyDeCRFF, the show function doesn't wook on Mac OS - } inline void show() From ace84ae7fd7329ae3c59189cd3d5df988a9947f7 Mon Sep 17 00:00:00 2001 From: AtsushiSakai Date: Sat, 16 Jan 2016 14:24:23 +0900 Subject: [PATCH 4/5] format code --- matplotlibcpp.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index 63cde89..0c3db20 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -248,17 +248,16 @@ namespace matplotlibcpp { Py_DECREF(res); } - inline void title(const std::string &titlestr) + inline void title(const std::string &titlestr) { PyObject* pytitlestr = PyString_FromString(titlestr.c_str()); PyObject* args = PyTuple_New(1); PyTuple_SetItem(args, 0, pytitlestr); - PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_title, - args); + PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_title, args); if(!res) throw std::runtime_error("Call to title() failed."); - //if PyDeCRFF, the show function doesn't wook on Mac OS + //if PyDeCRFF, the show function doesn't wook on Mac OS } inline void show() From 335ffa61f69cff025d8b3945e0141f9b6b272a38 Mon Sep 17 00:00:00 2001 From: AtsushiSakai Date: Sat, 16 Jan 2016 14:29:48 +0900 Subject: [PATCH 5/5] format code --- matplotlibcpp.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index 0c3db20..59a97b9 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -248,7 +248,8 @@ namespace matplotlibcpp { Py_DECREF(res); } - inline void title(const std::string &titlestr) + + inline void title(const std::string &titlestr) { PyObject* pytitlestr = PyString_FromString(titlestr.c_str()); PyObject* args = PyTuple_New(1); @@ -257,7 +258,7 @@ namespace matplotlibcpp { PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_title, args); if(!res) throw std::runtime_error("Call to title() failed."); - //if PyDeCRFF, the show function doesn't wook on Mac OS + //if PyDeCRFF, the show function doesn't wook on Mac OS } inline void show()