@@ -20,6 +20,7 @@ namespace matplotlibcpp {
20
20
PyObject *s_python_function_save;
21
21
PyObject *s_python_function_figure;
22
22
PyObject *s_python_function_plot;
23
+ PyObject *s_python_function_hist;
23
24
PyObject *s_python_function_subplot;
24
25
PyObject *s_python_function_legend;
25
26
PyObject *s_python_function_xlim;
@@ -34,7 +35,6 @@ namespace matplotlibcpp {
34
35
/* For now, _interpreter is implemented as a singleton since its currently not possible to have
35
36
multiple independent embedded python interpreters without patching the python source code
36
37
or starting a separate process for each.
37
-
38
38
http://bytes.com/topic/python/answers/793370-multiple-independent-python-interpreters-c-c-program
39
39
*/
40
40
@@ -64,6 +64,7 @@ namespace matplotlibcpp {
64
64
s_python_function_show = PyObject_GetAttrString (pymod, " show" );
65
65
s_python_function_figure = PyObject_GetAttrString (pymod, " figure" );
66
66
s_python_function_plot = PyObject_GetAttrString (pymod, " plot" );
67
+ s_python_function_hist = PyObject_GetAttrString (pymod," hist" );
67
68
s_python_function_subplot = PyObject_GetAttrString (pymod, " subplot" );
68
69
s_python_function_legend = PyObject_GetAttrString (pymod, " legend" );
69
70
s_python_function_ylim = PyObject_GetAttrString (pymod, " ylim" );
@@ -154,6 +155,63 @@ namespace matplotlibcpp {
154
155
return res;
155
156
}
156
157
158
+ template < typename Numeric>
159
+ bool hist (const std::vector<Numeric>& y, long bins=10 ,std::string color=" b" , double alpha=1.0 ){
160
+
161
+ PyObject* ylist = PyList_New (y.size ());
162
+
163
+ PyObject* kwargs = PyDict_New ();
164
+ PyDict_SetItemString (kwargs, " bins" ,PyFloat_FromDouble (bins));
165
+ PyDict_SetItemString (kwargs," color" ,PyString_FromString (color.c_str ()));
166
+ PyDict_SetItemString (kwargs, " alpha" ,PyFloat_FromDouble (alpha));
167
+
168
+ for (size_t i = 0 ; i < y.size (); ++i) {
169
+ PyList_SetItem (ylist, i, PyFloat_FromDouble (y.at (i)));
170
+ }
171
+
172
+ PyObject* plot_args = PyTuple_New (1 );
173
+
174
+ PyTuple_SetItem (plot_args, 0 , ylist);
175
+
176
+
177
+ PyObject* res = PyObject_Call (detail::_interpreter::get ().s_python_function_hist , plot_args, kwargs);
178
+
179
+
180
+ Py_DECREF (ylist);
181
+ Py_DECREF (plot_args);
182
+ Py_DECREF (kwargs);
183
+ if (res) Py_DECREF (res);
184
+
185
+ return res;
186
+ }
187
+ template < typename Numeric>
188
+ bool named_hist (std::string label,const std::vector<Numeric>& y, long bins=10 ,std::string color=" b" , double alpha=1.0 ){
189
+
190
+ PyObject* ylist = PyList_New (y.size ());
191
+ PyObject* kwargs = PyDict_New ();
192
+ PyDict_SetItemString (kwargs," label" ,PyString_FromString (label.c_str ()));
193
+ PyDict_SetItemString (kwargs, " bins" ,PyFloat_FromDouble (bins));
194
+ PyDict_SetItemString (kwargs," color" ,PyString_FromString (color.c_str ()));
195
+ PyDict_SetItemString (kwargs, " alpha" ,PyFloat_FromDouble (alpha));
196
+
197
+ for (size_t i = 0 ; i < y.size (); ++i) {
198
+ PyList_SetItem (ylist, i, PyFloat_FromDouble (y.at (i)));
199
+ }
200
+
201
+ PyObject* plot_args = PyTuple_New (1 );
202
+ PyTuple_SetItem (plot_args, 0 , ylist);
203
+
204
+ PyObject* res = PyObject_Call (detail::_interpreter::get ().s_python_function_hist , plot_args, kwargs);
205
+
206
+
207
+ Py_DECREF (ylist);
208
+ Py_DECREF (plot_args);
209
+ Py_DECREF (kwargs);
210
+ if (res) Py_DECREF (res);
211
+
212
+ return res;
213
+ }
214
+
157
215
template <typename NumericX, typename NumericY>
158
216
bool plot (const std::vector<NumericX>& x, const std::vector<NumericY>& y, const std::string& s = " " )
159
217
{
@@ -182,6 +240,32 @@ namespace matplotlibcpp {
182
240
183
241
return res;
184
242
}
243
+ template <typename Numeric>
244
+ bool named_plot (const std::string& name, const std::vector<Numeric>& y, const std::string& format = " " ) {
245
+ PyObject* kwargs = PyDict_New ();
246
+ PyDict_SetItemString (kwargs, " label" , PyString_FromString (name.c_str ()));
247
+
248
+ PyObject* ylist = PyList_New (y.size ());
249
+ PyObject* pystring = PyString_FromString (format.c_str ());
250
+
251
+ for (size_t i = 0 ; i < y.size (); ++i) {
252
+ PyList_SetItem (ylist, i, PyFloat_FromDouble (y.at (i)));
253
+ }
254
+
255
+ PyObject* plot_args = PyTuple_New (2 );
256
+
257
+ PyTuple_SetItem (plot_args, 0 , ylist);
258
+ PyTuple_SetItem (plot_args, 1 , pystring);
259
+
260
+ PyObject* res = PyObject_Call (detail::_interpreter::get ().s_python_function_plot , plot_args, kwargs);
261
+
262
+ Py_DECREF (kwargs);
263
+ Py_DECREF (ylist);
264
+ Py_DECREF (plot_args);
265
+ if (res) Py_DECREF (res);
266
+
267
+ return res;
268
+ }
185
269
186
270
template <typename Numeric>
187
271
bool named_plot (const std::string& name, const std::vector<Numeric>& x, const std::vector<Numeric>& y, const std::string& format = " " ) {
@@ -221,7 +305,13 @@ namespace matplotlibcpp {
221
305
return plot (x,y,format);
222
306
}
223
307
308
+ inline void figure (){
309
+ PyObject* res = PyObject_CallObject (detail::_interpreter::get ().s_python_function_figure , detail::_interpreter::get ().s_python_empty_tuple );
310
+ if (!res) throw std::runtime_error (" Call to legend() failed." );
224
311
312
+ Py_DECREF (res);
313
+
314
+ }
225
315
inline void legend () {
226
316
PyObject* res = PyObject_CallObject (detail::_interpreter::get ().s_python_function_legend , detail::_interpreter::get ().s_python_empty_tuple );
227
317
if (!res) throw std::runtime_error (" Call to legend() failed." );
0 commit comments