From 14346be71d18173823d327b34ee3c70006969237 Mon Sep 17 00:00:00 2001 From: Jean-Marco Alameddine Date: Tue, 19 May 2020 16:58:10 +0200 Subject: [PATCH] Change code so that plot3 can be called several times for one figure. Furthermore, alpha parameter can optionally be passed to the plot3 function --- matplotlibcpp.h | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index ea2e4fb..b289b87 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -302,10 +302,10 @@ template <> struct select_npy_type { const static NPY_TYPES type = NPY // Sanity checks; comment them out or change the numpy type below if you're compiling on // a platform where they don't apply -static_assert(sizeof(long long) == 8); -template <> struct select_npy_type { const static NPY_TYPES type = NPY_INT64; }; -static_assert(sizeof(unsigned long long) == 8); -template <> struct select_npy_type { const static NPY_TYPES type = NPY_UINT64; }; +//static_assert(sizeof(long long) == 8); +//template <> struct select_npy_type { const static NPY_TYPES type = NPY_INT64; }; +//static_assert(sizeof(unsigned long long) == 8); +//template <> struct select_npy_type { const static NPY_TYPES type = NPY_UINT64; }; // TODO: add int, long, etc. template @@ -525,6 +525,7 @@ template void plot3(const std::vector &x, const std::vector &y, const std::vector &z, + double alpha = 1., const std::map &keywords = std::map()) { @@ -551,6 +552,20 @@ void plot3(const std::vector &x, if (!axis3dmod) { throw std::runtime_error("Error loading module mpl_toolkits.mplot3d!"); } } + static PyObject *axis = nullptr; + if(axis == nullptr) { + PyObject *fig = PyObject_CallObject(matplotlibcpp::detail::_interpreter::get().s_python_function_figure, + matplotlibcpp::detail::_interpreter::get().s_python_empty_tuple); + + PyObject *gca_kwargs = PyDict_New(); + PyDict_SetItemString(gca_kwargs, "projection", PyString_FromString("3d")); + + PyObject *gca = PyObject_GetAttrString(fig, "gca"); + if (!gca) throw std::runtime_error("No gca"); + Py_INCREF(gca); + axis = PyObject_Call(gca, matplotlibcpp::detail::_interpreter::get().s_python_empty_tuple, gca_kwargs); + } + assert(x.size() == y.size()); assert(y.size() == z.size()); @@ -572,27 +587,11 @@ void plot3(const std::vector &x, PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str())); } - - PyObject *fig = - PyObject_CallObject(detail::_interpreter::get().s_python_function_figure, - detail::_interpreter::get().s_python_empty_tuple); - if (!fig) throw std::runtime_error("Call to figure() failed."); - - PyObject *gca_kwargs = PyDict_New(); - PyDict_SetItemString(gca_kwargs, "projection", PyString_FromString("3d")); - - PyObject *gca = PyObject_GetAttrString(fig, "gca"); - if (!gca) throw std::runtime_error("No gca"); - Py_INCREF(gca); - PyObject *axis = PyObject_Call( - gca, detail::_interpreter::get().s_python_empty_tuple, gca_kwargs); + PyDict_SetItemString(kwargs, "alpha", PyFloat_FromDouble(alpha)); if (!axis) throw std::runtime_error("No axis"); Py_INCREF(axis); - Py_DECREF(gca); - Py_DECREF(gca_kwargs); - PyObject *plot3 = PyObject_GetAttrString(axis, "plot"); if (!plot3) throw std::runtime_error("No 3D line plot"); Py_INCREF(plot3);