Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat: add kwargs for grid function
  • Loading branch information
HangX-Ma committed Aug 31, 2023
commit f2494948073810b484fe6bf61f65c417eabfbfd1
16 changes: 13 additions & 3 deletions matplotlibcpp.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#pragma once
#ifndef __MATPLOTLIBCPP__H__
#define __MATPLOTLIBCPP__H__

// Python headers must be included before any system headers, since
// they define _POSIX_C_SOURCE
Expand Down Expand Up @@ -2499,7 +2501,7 @@ inline void set_zlabel(const std::string &str, const std::map<std::string, std::
if (res) Py_DECREF(res);
}

inline void grid(bool flag)
inline void grid(bool flag, const std::map<std::string, std::string>& keywords = std::map<std::string, std::string>())
{
detail::_interpreter::get();

Expand All @@ -2509,10 +2511,16 @@ inline void grid(bool flag)
PyObject* args = PyTuple_New(1);
PyTuple_SetItem(args, 0, pyflag);

PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_grid, args);
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_grid, args, kwargs);
if(!res) throw std::runtime_error("Call to grid() failed.");

Py_DECREF(args);
Py_DECREF(kwargs);
Py_DECREF(res);
}

Expand Down Expand Up @@ -2632,7 +2640,7 @@ inline void rcparams(const std::map<std::string, std::string>& keywords = {}) {
PyDict_SetItemString(kwargs, it->first.c_str(), PyLong_FromLong(std::stoi(it->second.c_str())));
else PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
}

PyObject * update = PyObject_GetAttrString(detail::_interpreter::get().s_python_function_rcparams, "update");
PyObject * res = PyObject_Call(update, args, kwargs);
if(!res) throw std::runtime_error("Call to rcParams.update() failed.");
Expand Down Expand Up @@ -2961,3 +2969,5 @@ class Plot
};

} // end namespace matplotlibcpp

#endif //!__MATPLOTLIBCPP__H__