@@ -62,6 +62,8 @@ struct _interpreter {
62
62
PyObject *s_python_empty_tuple;
63
63
PyObject *s_python_function_stem;
64
64
PyObject *s_python_function_xkcd;
65
+ PyObject *s_python_function_axhline;
66
+ PyObject *s_python_function_axvline;
65
67
66
68
/* For now, _interpreter is implemented as a singleton since its currently not possible to have
67
69
multiple independent embedded python interpreters without patching the python source code
@@ -165,6 +167,8 @@ struct _interpreter {
165
167
s_python_function_tight_layout = PyObject_GetAttrString (pymod, " tight_layout" );
166
168
s_python_function_stem = PyObject_GetAttrString (pymod, " stem" );
167
169
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" );
168
172
169
173
if ( !s_python_function_show
170
174
|| !s_python_function_close
@@ -195,6 +199,8 @@ struct _interpreter {
195
199
|| !s_python_function_tight_layout
196
200
|| !s_python_function_stem
197
201
|| !s_python_function_xkcd
202
+ || !s_python_function_axhline
203
+ || !s_python_function_axvline
198
204
) { throw std::runtime_error (" Couldn't find required function!" ); }
199
205
200
206
if ( !PyFunction_Check (s_python_function_show)
@@ -225,6 +231,8 @@ struct _interpreter {
225
231
|| !PyFunction_Check (s_python_function_errorbar)
226
232
|| !PyFunction_Check (s_python_function_stem)
227
233
|| !PyFunction_Check (s_python_function_xkcd)
234
+ || !PyFunction_Check (s_python_function_axhline)
235
+ || !PyFunction_Check (s_python_function_axvline)
228
236
) { throw std::runtime_error (" Python object is unexpectedly not a PyFunction." ); }
229
237
230
238
s_python_empty_tuple = PyTuple_New (0 );
@@ -1113,7 +1121,58 @@ inline void xkcd() {
1113
1121
Py_DECREF (kwargs);
1114
1122
1115
1123
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." );
1117
1176
1118
1177
Py_DECREF (res);
1119
1178
}
0 commit comments