Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2cbc314

Browse files
committed
Added hist and named_hist
1 parent 741350f commit 2cbc314

File tree

1 file changed

+91
-1
lines changed

1 file changed

+91
-1
lines changed

matplotlibcpp.h

+91-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace matplotlibcpp {
2020
PyObject *s_python_function_save;
2121
PyObject *s_python_function_figure;
2222
PyObject *s_python_function_plot;
23+
PyObject *s_python_function_hist;
2324
PyObject *s_python_function_subplot;
2425
PyObject *s_python_function_legend;
2526
PyObject *s_python_function_xlim;
@@ -34,7 +35,6 @@ namespace matplotlibcpp {
3435
/* For now, _interpreter is implemented as a singleton since its currently not possible to have
3536
multiple independent embedded python interpreters without patching the python source code
3637
or starting a separate process for each.
37-
3838
http://bytes.com/topic/python/answers/793370-multiple-independent-python-interpreters-c-c-program
3939
*/
4040

@@ -64,6 +64,7 @@ namespace matplotlibcpp {
6464
s_python_function_show = PyObject_GetAttrString(pymod, "show");
6565
s_python_function_figure = PyObject_GetAttrString(pymod, "figure");
6666
s_python_function_plot = PyObject_GetAttrString(pymod, "plot");
67+
s_python_function_hist = PyObject_GetAttrString(pymod,"hist");
6768
s_python_function_subplot = PyObject_GetAttrString(pymod, "subplot");
6869
s_python_function_legend = PyObject_GetAttrString(pymod, "legend");
6970
s_python_function_ylim = PyObject_GetAttrString(pymod, "ylim");
@@ -154,6 +155,63 @@ namespace matplotlibcpp {
154155
return res;
155156
}
156157

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+
157215
template<typename NumericX, typename NumericY>
158216
bool plot(const std::vector<NumericX>& x, const std::vector<NumericY>& y, const std::string& s = "")
159217
{
@@ -182,6 +240,32 @@ namespace matplotlibcpp {
182240

183241
return res;
184242
}
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+
}
185269

186270
template<typename Numeric>
187271
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 {
221305
return plot(x,y,format);
222306
}
223307

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.");
224311

312+
Py_DECREF(res);
313+
314+
}
225315
inline void legend() {
226316
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_legend, detail::_interpreter::get().s_python_empty_tuple);
227317
if(!res) throw std::runtime_error("Call to legend() failed.");

0 commit comments

Comments
 (0)