Skip to content

Commit 8babc98

Browse files
committed
Add kwargs for title, xlabel, ylabel
1 parent f64f1f6 commit 8babc98

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

matplotlibcpp.h

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,29 +1321,41 @@ inline void subplot(long nrows, long ncols, long plot_number)
13211321
Py_DECREF(res);
13221322
}
13231323

1324-
inline void title(const std::string &titlestr)
1324+
inline void title(const std::string &titlestr, const std::map<std::string, std::string> &keywords = {})
13251325
{
13261326
PyObject* pytitlestr = PyString_FromString(titlestr.c_str());
13271327
PyObject* args = PyTuple_New(1);
13281328
PyTuple_SetItem(args, 0, pytitlestr);
13291329

1330-
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_title, args);
1330+
PyObject* kwargs = PyDict_New();
1331+
for (auto it = keywords.begin(); it != keywords.end(); ++it) {
1332+
PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
1333+
}
1334+
1335+
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_title, args, kwargs);
13311336
if(!res) throw std::runtime_error("Call to title() failed.");
13321337

13331338
Py_DECREF(args);
1339+
Py_DECREF(kwargs);
13341340
Py_DECREF(res);
13351341
}
13361342

1337-
inline void suptitle(const std::string &suptitlestr)
1343+
inline void suptitle(const std::string &suptitlestr, const std::map<std::string, std::string> &keywords = {})
13381344
{
13391345
PyObject* pysuptitlestr = PyString_FromString(suptitlestr.c_str());
13401346
PyObject* args = PyTuple_New(1);
13411347
PyTuple_SetItem(args, 0, pysuptitlestr);
13421348

1343-
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_suptitle, args);
1349+
PyObject* kwargs = PyDict_New();
1350+
for (auto it = keywords.begin(); it != keywords.end(); ++it) {
1351+
PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
1352+
}
1353+
1354+
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_suptitle, args, kwargs);
13441355
if(!res) throw std::runtime_error("Call to suptitle() failed.");
13451356

13461357
Py_DECREF(args);
1358+
Py_DECREF(kwargs);
13471359
Py_DECREF(res);
13481360
}
13491361

@@ -1360,29 +1372,41 @@ inline void axis(const std::string &axisstr)
13601372
Py_DECREF(res);
13611373
}
13621374

1363-
inline void xlabel(const std::string &str)
1375+
inline void xlabel(const std::string &str, const std::map<std::string, std::string> &keywords = {})
13641376
{
13651377
PyObject* pystr = PyString_FromString(str.c_str());
13661378
PyObject* args = PyTuple_New(1);
13671379
PyTuple_SetItem(args, 0, pystr);
13681380

1369-
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_xlabel, args);
1381+
PyObject* kwargs = PyDict_New();
1382+
for (auto it = keywords.begin(); it != keywords.end(); ++it) {
1383+
PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
1384+
}
1385+
1386+
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_xlabel, args, kwargs);
13701387
if(!res) throw std::runtime_error("Call to xlabel() failed.");
13711388

13721389
Py_DECREF(args);
1390+
Py_DECREF(kwargs);
13731391
Py_DECREF(res);
13741392
}
13751393

1376-
inline void ylabel(const std::string &str)
1394+
inline void ylabel(const std::string &str, const std::map<std::string, std::string>& keywords = {})
13771395
{
13781396
PyObject* pystr = PyString_FromString(str.c_str());
13791397
PyObject* args = PyTuple_New(1);
13801398
PyTuple_SetItem(args, 0, pystr);
13811399

1382-
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_ylabel, args);
1400+
PyObject* kwargs = PyDict_New();
1401+
for (auto it = keywords.begin(); it != keywords.end(); ++it) {
1402+
PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
1403+
}
1404+
1405+
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_ylabel, args, kwargs);
13831406
if(!res) throw std::runtime_error("Call to ylabel() failed.");
13841407

13851408
Py_DECREF(args);
1409+
Py_DECREF(kwargs);
13861410
Py_DECREF(res);
13871411
}
13881412

0 commit comments

Comments
 (0)