Skip to content

Commit 6586ec6

Browse files
committed
replace save with savefig including kwargs support
1 parent 87e3926 commit 6586ec6

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

matplotlibcpp.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
/*****************************************************************
3-
Python headers must be included before any system headers, since
4-
they define _POSIX_C_SOURCE
3+
Python headers must be included before any system headers, since
4+
they define _POSIX_C_SOURCE
55
******************************************************************/
66
#include <Python.h>
77

@@ -2685,28 +2685,31 @@ inline void pause(Numeric interval) {
26852685
Py_DECREF(res);
26862686
}
26872687

2688-
inline void save(const std::string& filename, const int dpi = 0) {
2689-
detail::_interpreter::get();
2690-
2688+
inline void savefig(const std::string& filename,
2689+
const std::map<std::string, std::string>& keywords = {}) {
26912690
PyObject* pyfilename = PyString_FromString(filename.c_str());
26922691

26932692
PyObject* args = PyTuple_New(1);
26942693
PyTuple_SetItem(args, 0, pyfilename);
26952694

26962695
PyObject* kwargs = PyDict_New();
2697-
2698-
if (dpi > 0) {
2699-
PyDict_SetItemString(kwargs, "dpi", PyLong_FromLong(dpi));
2696+
for (auto it = keywords.begin(); it != keywords.end(); ++it) {
2697+
PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
27002698
}
27012699

27022700
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_save, args, kwargs);
2703-
if (!res) throw std::runtime_error("Call to save() failed.");
2701+
if (!res) throw std::runtime_error("Call to savefig() failed.");
27042702

2705-
Py_DECREF(args);
27062703
Py_DECREF(kwargs);
2704+
Py_DECREF(args);
27072705
Py_DECREF(res);
27082706
}
27092707

2708+
inline void save(const std::string& filename) {
2709+
std::cerr << "matplotlibcpp::save is deprecated, use savefig instead\n";
2710+
matplotlibcpp::savefig(filename);
2711+
}
2712+
27102713
inline void rcparams(const std::map<std::string, std::string>& keywords = {}) {
27112714
detail::_interpreter::get();
27122715
PyObject* args = PyTuple_New(0);

0 commit comments

Comments
 (0)