8000 Fix xkcd exception message, add 'axhline' & 'axvline' · tttapa/matplotlib-cpp@99adb9c · GitHub
[go: up one dir, main page]

Skip to content

Commit 99adb9c

Browse files
committed
Fix xkcd exception message, add 'axhline' & 'axvline'
1 parent 00a4766 commit 99adb9c

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

matplotlibcpp.h

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ struct _interpreter {
6262
PyObject *s_python_empty_tuple;
6363
PyObject *s_python_function_stem;
6464
PyObject *s_python_function_xkcd;
65+
PyObject *s_python_function_axhline;
66+
PyObject *s_python_function_axvline;
6567

6668
/* For now, _interpreter is implemented as a singleton since its currently not possible to have
6769
multiple independent embedded python interpreters without patching the python source code
@@ -165,6 +167,8 @@ struct _interpreter {
165167
s_python_function_tight_layout = PyObject_GetAttrString(pymod, "tight_layout");
166168
s_python_function_stem = PyObject_GetAttrString(pymod, "stem");
167169
s_python_function_xkcd = PyObject_GetAttrString(pymod, "xkcd");
170+
s_python_function_axhline = PyObject_GetAttrString(pymod, "axhline");
171+
s_python_function_axvline = PyObject_GetAttrString(pymod, "axvline");
168172

169173
if( !s_python_function_show
170174
|| !s_python_function_close
@@ -195,6 +199,8 @@ struct _interpreter {
195199
|| !s_python_function_tight_layout
196200
|| !s_python_function_stem
197201
|| !s_python_function_xkcd
202+
|| !s_python_function_axhline
203+
|| !s_python_function_axvline
198204
) { throw std::runtime_error("Couldn't find required function!"); }
199205

200206
if ( !PyFunction_Check(s_python_function_show)
@@ -225,6 +231,8 @@ struct _interpreter {
225231
|| !PyFunction_Check(s_python_function_errorbar)
226232
|| !PyFunction_Check(s_python_function_stem)
227233
|| !PyFunction_Check(s_python_function_xkcd)
234+
|| !PyFunction_Check(s_python_function_axhline)
235+
|| !PyFunction_Check(s_python_function_axvline)
228236
) { throw std::runtime_error("Python object is unexpectedly not a PyFunction."); }
229237

230238
s_python_empty_tuple = PyTuple_New(0);
@@ -1113,7 +1121,58 @@ inline void xkcd() {
11131121
Py_DECREF(kwargs);
11141122

11151123
if (!res)
1116-
throw std::runtime_error("Call to show() failed.");
1124+
throw std::runtime_error("Call to xkcd() failed.");
1125+
1126+
Py_DECREF(res);
1127+
}
1128+
1129+
inline void axhline(const double y = 0, const std::string& linestyle = "", const std::string &color = "", const double xmin = 0, const double xmax = 1) {
1130+
PyObject *res;
1131+
PyObject *args = PyTuple_New(3);
1132+
PyObject *kwargs = PyDict_New();
1133+
1134+
PyObject *pylinestyle = PyString_FromString(linestyle.c_str());
1135+
PyObject *pycolor = PyString_FromString(color.c_str());
1136+
1137+
PyDict_SetItemString(kwargs, "linestyle", PyString_FromString(linestyle.c_str()));
1138+
PyDict_SetItemString(kwargs, "color", PyString_FromString(color.c_str()));
1139+
1140+
PyTuple_SetItem(args, 0, PyFloat_FromDouble(y));
1141+
PyTuple_SetItem(args, 1, PyFloat_FromDouble(xmin));
1142+
PyTuple_SetItem(args, 2, PyFloat_FromDouble(xmax));
1143+
1144+
res = PyObject_Call(detail::_interpreter::get().s_python_function_axhline,
1145+
args, kwargs);
1146+
1147+
Py_DECREF(args);
1148+
Py_DECREF(kwargs);
1149+
1150+
if (!res)
1151+
throw std::runtime_error("Call to axhline() failed.");
1152+
1153+
Py_DECREF(res);
1154+
}
1155+
1156+
inline void axvline(const double x = 0, const std::string& linestyle = "", const std::string &color = "", const double ymin = 0, const double ymax = 1) {
1157+
PyObject *res;
1158+
PyObject *args = PyTuple_New(3);
1159+
PyObject *kwargs = PyDict_New();
1160+
1161+
PyDict_SetItemString(kwargs, "linestyle", PyString_FromString(linestyle.c_str()));
1162+
PyDict_SetItemString(kwargs, "color", PyString_FromString(color.c_str()));
1163+
1164+
PyTuple_SetItem(args, 0, PyFloat_FromDouble(x));
1165+
PyTuple_SetItem(args, 1, PyFloat_FromDouble(ymin));
1166+
PyTuple_SetItem(args, 2, PyFloat_FromDouble(ymax));
1167+
1168+
res = PyObject_Call(detail::_interpreter::get().s_python_function_axvline,
1169+
args, kwargs);
1170+
1171+
Py_DECREF(args);
1172+
Py_DECREF(kwargs);
1173+
1174+
if (!res)
1175+
throw std::runtime_error("Call to axvline() failed.");
11171176

11181177
Py_DECREF(res);
11191178
}

0 commit comments

Comments
 (0)
0