diff --git a/examples/basic.png b/examples/basic.png index eef82a22..0a786209 100644 Binary files a/examples/basic.png and b/examples/basic.png differ diff --git a/matplotlibcpp.h b/matplotlibcpp.h index a16d3705..2fe4b7d3 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -32,7 +32,10 @@ namespace matplotlibcpp { PyObject *s_python_function_grid; PyObject *s_python_empty_tuple; PyObject *s_python_function_annotate; - + PyObject *s_python_function_close; + PyObject *s_python_function_arrow; + PyObject *s_python_function_contour; + PyObject *s_python_function_imshow; /* For now, _interpreter is implemented as a singleton since its currently not possible to have multiple independent embedded python interpreters without patching the python source code or starting a separate process for each. @@ -77,8 +80,12 @@ namespace matplotlibcpp { s_python_function_xlim = PyObject_GetAttrString(pymod, "xlim"); s_python_function_save = PyObject_GetAttrString(pylabmod, "savefig"); s_python_function_annotate = PyObject_GetAttrString(pymod,"annotate"); + s_python_function_close = PyObject_GetAttrString(pymod,"close"); + s_python_function_arrow = PyObject_GetAttrString(pymod,"arrow"); + s_python_function_contour = PyObject_GetAttrString(pymod,"contourf"); + s_python_function_imshow = PyObject_GetAttrString(pymod,"imshow"); - if( !s_python_function_show + if( !s_python_function_show || !s_python_function_figure || !s_python_function_plot || !s_python_function_subplot @@ -92,6 +99,10 @@ namespace matplotlibcpp { || !s_python_function_xlim || !s_python_function_save || !s_python_function_annotate + || !s_python_function_close + || !s_python_function_arrow + || !s_python_function_contour + || !s_python_function_imshow ) { throw std::runtime_error("Couldn't find required function!"); } @@ -109,6 +120,10 @@ namespace matplotlibcpp { || !PyFunction_Check(s_python_function_grid) || !PyFunction_Check(s_python_function_xlim) || !PyFunction_Check(s_python_function_save) + || !PyFunction_Check(s_python_function_close) + || !PyFunction_Check(s_python_function_arrow) + || !PyFunction_Check(s_python_function_contour) + || !PyFunction_Check(s_python_function_imshow) ) { throw std::runtime_error("Python object is unexpectedly not a PyFunction."); } @@ -120,23 +135,112 @@ namespace matplotlibcpp { } }; } - + + template + bool imshow(const std::vector > img) + { + PyObject* p_img = PyList_New(img.size()); + for(size_t i = 0; i < img.size(); ++i) { + PyObject * zi = PyList_New(img.at(i).size()); + for (size_t j=0; j + bool contour(const std::vector& x,const std::vector& y,const std::vector >& z) // dosn't work?? long n=10) + { + PyObject * xyz = PyTuple_New(3); + // using python lists + PyObject* xlist = PyList_New(x.size()); + PyObject* ylist = PyList_New(y.size()); + PyObject* zlist = PyList_New(z.size()); + + for(size_t i = 0; i < x.size(); ++i) { + PyList_SetItem(xlist, i, PyFloat_FromDouble(x.at(i))); + PyList_SetItem(ylist, i, PyFloat_FromDouble(y.at(i))); + PyObject * zi = PyList_New(z.at(i).size()); + for (size_t j=0; j& y, long bins=10,std::string color="b", double alpha=1.0){ PyObject* ylist = PyList_New(y.size()); - + PyObject* kwargs = PyDict_New(); PyDict_SetItemString(kwargs, "bins", PyLong_FromLong(bins)); - PyDict_SetItemString(kwargs, "color", PyString_FromString(color.c_str())); + PyDict_SetItemString(kwargs, "color", PyString_FromString(color.c_str())); PyDict_SetItemString(kwargs, "alpha", PyFloat_FromDouble(alpha)); - + for(size_t i = 0; i < y.size(); ++i) { PyList_SetItem(ylist, i, PyFloat_FromDouble(y.at(i))); } @@ -218,9 +322,9 @@ namespace matplotlibcpp { PyObject* kwargs = PyDict_New(); PyDict_SetItemString(kwargs, "label", PyString_FromString(label.c_str())); PyDict_SetItemString(kwargs, "bins", PyLong_FromLong(bins)); - PyDict_SetItemString(kwargs, "color", PyString_FromString(color.c_str())); + PyDict_SetItemString(kwargs, "color", PyString_FromString(color.c_str())); PyDict_SetItemString(kwargs, "alpha", PyFloat_FromDouble(alpha)); - + for(size_t i = 0; i < y.size(); ++i) { PyList_SetItem(ylist, i, PyFloat_FromDouble(y.at(i))); } @@ -237,7 +341,7 @@ namespace matplotlibcpp { return res; } - + template bool plot(const std::vector& x, const std::vector& y, const std::string& s = "") { @@ -291,9 +395,10 @@ namespace matplotlibcpp { } template - bool named_plot(const std::string& name, const std::vector& x, const std::vector& y, const std::string& format = "") { + bool named_plot(const std::string& name, const std::vector& x, const std::vector& y, const std::string& format = "",double alpha=1.0) { PyObject* kwargs = PyDict_New(); PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str())); + PyDict_SetItemString(kwargs, "alpha", PyFloat_FromDouble(alpha)); PyObject* xlist = PyList_New(x.size()); PyObject* ylist = PyList_New(y.size()); @@ -329,11 +434,17 @@ namespace matplotlibcpp { inline void figure(){ PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_figure, detail::_interpreter::get().s_python_empty_tuple); if(!res) throw std::runtime_error("Call to figure() failed."); + Py_DECREF(res); + } + + inline void close(){ + PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_close, detail::_interpreter::get().s_python_empty_tuple); + if(!res) throw std::runtime_error("Call to close() failed."); Py_DECREF(res); - } - inline void legend() { + + inline void legend() { PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_legend, detail::_interpreter::get().s_python_empty_tuple); if(!res) throw std::runtime_error("Call to legend() failed."); @@ -373,35 +484,34 @@ namespace matplotlibcpp { Py_DECREF(args); Py_DECREF(res); } - - + + double * xlim() { - PyObject* args = PyTuple_New(0); - PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_xlim, args); + + PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_xlim, detail::_interpreter::get().s_python_empty_tuple); PyObject * left = PyTuple_GetItem(res,0); PyObject * right = PyTuple_GetItem(res,1); double * arr = new double[2]; arr[0] = PyFloat_AsDouble(left); arr[1] = PyFloat_AsDouble(right); - - if(!res) throw std::runtime_error("Call to xlim() failed."); + + if(!res) throw std::runtime_error("Call to xlim() failed."); Py_DECREF(res); return arr; } - - + + double * ylim() { - PyObject* args = PyTuple_New(0); - PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_ylim, args); + PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_ylim, detail::_interpreter::get().s_python_empty_tuple); PyObject * left = PyTuple_GetItem(res,0); PyObject * right = PyTuple_GetItem(res,1); double * arr = new double[2]; arr[0] = PyFloat_AsDouble(left); arr[1] = PyFloat_AsDouble(right); - - if(!res) throw std::runtime_error("Call to ylim() failed."); + + if(!res) throw std::runtime_error("Call to ylim() failed."); Py_DECREF(res); return arr; } @@ -499,7 +609,7 @@ namespace matplotlibcpp { PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_save, args); if(!res) throw std::runtime_error("Call to save() failed."); - Py_DECREF(pyfilename); + Py_DECREF(args); Py_DECREF(res); } @@ -636,8 +746,8 @@ namespace matplotlibcpp { return plot(x,y,keywords); } - bool named_plot(const std::string& name, const std::vector& x, const std::vector& y, const std::string& format = "") { - return named_plot(name,x,y,format); + bool named_plot(const std::string& name, const std::vector& x, const std::vector& y, const std::string& format = "",double alpha=1.0) { + return named_plot(name,x,y,format,alpha); } #endif