Skip to content

Commit 791fa41

Browse files
committed
Add wrapper for plt.fill_between
1 parent b90d6e9 commit 791fa41

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

matplotlibcpp.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace matplotlibcpp {
3232
PyObject *s_python_function_save;
3333
PyObject *s_python_function_figure;
3434
PyObject *s_python_function_plot;
35+
PyObject *s_python_function_fill_between;
3536
PyObject *s_python_function_hist;
3637
PyObject *s_python_function_subplot;
3738
PyObject *s_python_function_legend;
@@ -85,6 +86,7 @@ namespace matplotlibcpp {
8586
s_python_function_show = PyObject_GetAttrString(pymod, "show");
8687
s_python_function_figure = PyObject_GetAttrString(pymod, "figure");
8788
s_python_function_plot = PyObject_GetAttrString(pymod, "plot");
89+
s_python_function_fill_between = PyObject_GetAttrString(pymod, "fill_between");
8890
s_python_function_hist = PyObject_GetAttrString(pymod,"hist");
8991
s_python_function_subplot = PyObject_GetAttrString(pymod, "subplot");
9092
s_python_function_legend = PyObject_GetAttrString(pymod, "legend");
@@ -103,6 +105,7 @@ namespace matplotlibcpp {
103105
if( !s_python_function_show
104106
|| !s_python_function_figure
105107
|| !s_python_function_plot
108+
|| !s_python_function_fill_between
106109
|| !s_python_function_subplot
107110
|| !s_python_function_legend
108111
|| !s_python_function_ylim
@@ -121,6 +124,7 @@ namespace matplotlibcpp {
121124
if ( !PyFunction_Check(s_python_function_show)
122125
|| !PyFunction_Check(s_python_function_figure)
123126
|| !PyFunction_Check(s_python_function_plot)
127+
|| !PyFunction_Check(s_python_function_fill_between)
124128
|| !PyFunction_Check(s_python_function_subplot)
125129
|| !PyFunction_Check(s_python_function_legend)
126130
|| !PyFunction_Check(s_python_function_annotate)
@@ -204,6 +208,45 @@ namespace matplotlibcpp {
204208
return res;
205209
}
206210

211+
template< typename Numeric >
212+
bool fill_between(const std::vector<Numeric>& x, const std::vector<Numeric>& y1, const std::vector<Numeric>& y2, const std::map<std::string, std::string>& keywords)
213+
{
214+
assert(x.size() == y1.size());
215+
assert(x.size() == y2.size());
216+
217+
// using python lists
218+
PyObject* xlist = PyList_New(x.size());
219+
PyObject* y1list = PyList_New(y1.size());
220+
PyObject* y2list = PyList_New(y2.size());
221+
222+
for(size_t i = 0; i < x.size(); ++i) {
223+
PyList_SetItem(xlist, i, PyFloat_FromDouble(x.at(i)));
224+
PyList_SetItem(y1list, i, PyFloat_FromDouble(y1.at(i)));
225+
PyList_SetItem(y2list, i, PyFloat_FromDouble(y2.at(i)));
226+
}
227+
228+
// construct positional args
229+
PyObject* args = PyTuple_New(3);
230+
PyTuple_SetItem(args, 0, xlist);
231+
PyTuple_SetItem(args, 1, y1list);
232+
PyTuple_SetItem(args, 2, y2list);
233+
234+
// construct keyword args
235+
PyObject* kwargs = PyDict_New();
236+
for(std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
237+
{
238+
PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
239+
}
240+
241+
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_fill_between, args, kwargs);
242+
243+
Py_DECREF(args);
244+
Py_DECREF(kwargs);
245+
if(res) Py_DECREF(res);
246+
247+
return res;
248+
}
249+
207250
template< typename Numeric>
208251
bool hist(const std::vector<Numeric>& y, long bins=10,std::string color="b", double alpha=1.0)
209252
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy