From 73d633f5cae92a6b6c2f5b6140e6317f66d0f540 Mon Sep 17 00:00:00 2001 From: Damyn Chipman Date: Thu, 15 Sep 2022 11:50:50 -0600 Subject: [PATCH 1/2] Removing type selectors for numpy array conversion. --- matplotlibcpp.h | 7 ------- 1 file changed, 7 deletions(-) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index d95d46ad..cd222589 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -348,13 +348,6 @@ template <> struct select_npy_type { const static NPY_TYPES type = NPY template <> struct select_npy_type { const static NPY_TYPES type = NPY_ULONG; }; template <> struct select_npy_type { const static NPY_TYPES type = NPY_UINT64; }; -// 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; }; - template PyObject* get_array(const std::vector& v) { From 8e280d272385cfb762d27dc9bff20ccc9a1cbbcf Mon Sep 17 00:00:00 2001 From: Damyn Chipman Date: Thu, 15 Sep 2022 17:07:29 -0600 Subject: [PATCH 2/2] Adding savefig function. --- matplotlibcpp.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index cd222589..1c4bba47 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -2615,6 +2615,29 @@ inline void pause(Numeric interval) Py_DECREF(res); } +inline void savefig(const std::string &filename, + const std::map &keywords = {}) { + PyObject *pyfilename = PyString_FromString(filename.c_str()); + + PyObject *args = PyTuple_New(1); + PyTuple_SetItem(args, 0, pyfilename); + + 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_save, args, kwargs); + if (!res) + throw std::runtime_error("Call to save() failed."); + + Py_DECREF(kwargs); + Py_DECREF(args); + Py_DECREF(res); +} + inline void save(const std::string& filename, const int dpi=0) { detail::_interpreter::get();