Skip to content

Commit f9a8f6e

Browse files
committed
Added contour and imshow plots
1 parent 7dd3c6b commit f9a8f6e

File tree

1 file changed

+127
-26
lines changed

1 file changed

+127
-26
lines changed

matplotlibcpp.h

Lines changed: 127 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ namespace matplotlibcpp {
3333
PyObject *s_python_empty_tuple;
3434
PyObject *s_python_function_annotate;
3535
PyObject *s_python_function_close;
36-
36+
PyObject *s_python_function_arrow;
37+
PyObject *s_python_function_contour;
38+
PyObject *s_python_function_imshow;
3739
/* For now, _interpreter is implemented as a singleton since its currently not possible to have
3840
multiple independent embedded python interpreters without patching the python source code
3941
or starting a separate process for each.
@@ -79,6 +81,9 @@ namespace matplotlibcpp {
7981
s_python_function_save = PyObject_GetAttrString(pylabmod, "savefig");
8082
s_python_function_annotate = PyObject_GetAttrString(pymod,"annotate");
8183
s_python_function_close = PyObject_GetAttrString(pymod,"close");
84+
s_python_function_arrow = PyObject_GetAttrString(pymod,"arrow");
85+
s_python_function_contour = PyObject_GetAttrString(pymod,"contourf");
86+
s_python_function_imshow = PyObject_GetAttrString(pymod,"imshow");
8287

8388
if( !s_python_function_show
8489
|| !s_python_function_figure
@@ -95,6 +100,9 @@ namespace matplotlibcpp {
95100
|| !s_python_function_save
96101
|| !s_python_function_annotate
97102
|| !s_python_function_close
103+
|| !s_python_function_arrow
104+
|| !s_python_function_contour
105+
|| !s_python_function_imshow
98106
)
99107
{ throw std::runtime_error("Couldn't find required function!"); }
100108

@@ -113,6 +121,9 @@ namespace matplotlibcpp {
113121
|| !PyFunction_Check(s_python_function_xlim)
114122
|| !PyFunction_Check(s_python_function_save)
115123
|| !PyFunction_Check(s_python_function_close)
124+
|| !PyFunction_Check(s_python_function_arrow)
125+
|| !PyFunction_Check(s_python_function_contour)
126+
|| !PyFunction_Check(s_python_function_imshow)
116127
)
117128
{ throw std::runtime_error("Python object is unexpectedly not a PyFunction."); }
118129

@@ -124,23 +135,112 @@ namespace matplotlibcpp {
124135
}
125136
};
126137
}
127-
138+
139+
template<typename Numeric>
140+
bool imshow(const std::vector<std::vector<Numeric> > img)
141+
{
142+
PyObject* p_img = PyList_New(img.size());
143+
for(size_t i = 0; i < img.size(); ++i) {
144+
PyObject * zi = PyList_New(img.at(i).size());
145+
for (size_t j=0; j<img[i].size();++j)
146+
{
147+
PyList_SetItem(zi, j, PyFloat_FromDouble(img.at(i).at(j)));
148+
}
149+
PyList_SetItem(p_img, i, zi);
150+
}
151+
PyObject * args = PyTuple_New(1);
152+
PyTuple_SetItem(args,0,p_img);
153+
154+
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_imshow, args);
155+
156+
Py_DECREF(args);
157+
158+
if(res) Py_DECREF(res);
159+
160+
return res;
161+
}
162+
163+
template<typename Numeric>
164+
bool contour(const std::vector<Numeric>& x,const std::vector<Numeric>& y,const std::vector<std::vector<Numeric> >& z) // dosn't work?? long n=10)
165+
{
166+
PyObject * xyz = PyTuple_New(3);
167+
// using python lists
168+
PyObject* xlist = PyList_New(x.size());
169+
PyObject* ylist = PyList_New(y.size());
170+
PyObject* zlist = PyList_New(z.size());
171+
172+
for(size_t i = 0; i < x.size(); ++i) {
173+
PyList_SetItem(xlist, i, PyFloat_FromDouble(x.at(i)));
174+
PyList_SetItem(ylist, i, PyFloat_FromDouble(y.at(i)));
175+
PyObject * zi = PyList_New(z.at(i).size());
176+
for (size_t j=0; j<z[i].size();++j)
177+
{
178+
PyList_SetItem(zi, j, PyFloat_FromDouble(z.at(i).at(j)));
179+
180+
}
181+
PyList_SetItem(zlist, i, zi);
182+
}
183+
PyTuple_SetItem(xyz,0,xlist);
184+
PyTuple_SetItem(xyz,1,ylist);
185+
PyTuple_SetItem(xyz,2,zlist);
186+
//PyTuple_SetItem(xyz,3,PyLong_FromLong(n));
187+
188+
// PyObject* kwargs = PyDict_New();
189+
//PyDict_SetItem(kwargs, PyLong_FromLong(3),PyLong_FromLong(n));
190+
191+
// PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_contour, xyz, kwargs);
192+
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_contour, xyz);
193+
194+
Py_DECREF(xyz);
195+
// Py_DECREF(kwargs);
196+
197+
if(res) Py_DECREF(res);
198+
199+
return res;
200+
}
201+
202+
203+
bool arrow(double x,double y, double dx,double dy, double h_width=0.02, double h_length=0.1)
204+
{
205+
PyObject * xy = PyTuple_New(4);
206+
207+
PyTuple_SetItem(xy,0,PyFloat_FromDouble(x));
208+
PyTuple_SetItem(xy,1,PyFloat_FromDouble(y));
209+
PyTuple_SetItem(xy,2,PyFloat_FromDouble(dx));
210+
PyTuple_SetItem(xy,3,PyFloat_FromDouble(dy));
211+
212+
PyObject* kwargs = PyDict_New();
213+
PyDict_SetItemString(kwargs, "fc", PyString_FromString("k"));
214+
PyDict_SetItemString(kwargs, "ec", PyString_FromString("k"));
215+
PyDict_SetItemString(kwargs, "head_width", PyFloat_FromDouble(h_width));
216+
PyDict_SetItemString(kwargs, "head_length", PyFloat_FromDouble(h_length));
217+
218+
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_arrow, xy, kwargs);
219+
220+
Py_DECREF(xy);
221+
Py_DECREF(kwargs);
222+
223+
if(res) Py_DECREF(res);
224+
225+
return res;
226+
}
227+
128228
bool annotate(std::string annotation, double x, double y)
129229
{
130230
PyObject * xy = PyTuple_New(2);
131231
PyObject * str = PyString_FromString(annotation.c_str());
132-
232+
133233
PyTuple_SetItem(xy,0,PyFloat_FromDouble(x));
134234
PyTuple_SetItem(xy,1,PyFloat_FromDouble(y));
135-
235+
136236
PyObject* kwargs = PyDict_New();
137237
PyDict_SetItemString(kwargs, "xy", xy);
138-
238+
139239
PyObject* args = PyTuple_New(1);
140240
PyTuple_SetItem(args, 0, str);
141241

142242
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_annotate, args, kwargs);
143-
243+
144244
Py_DECREF(args);
145245
Py_DECREF(kwargs);
146246

@@ -191,12 +291,12 @@ namespace matplotlibcpp {
191291
bool hist(const std::vector<Numeric>& y, long bins=10,std::string color="b", double alpha=1.0){
192292

193293
PyObject* ylist = PyList_New(y.size());
194-
294+
195295
PyObject* kwargs = PyDict_New();
196296
PyDict_SetItemString(kwargs, "bins", PyLong_FromLong(bins));
197-
PyDict_SetItemString(kwargs, "color", PyString_FromString(color.c_str()));
297+
PyDict_SetItemString(kwargs, "color", PyString_FromString(color.c_str()));
198298
PyDict_SetItemString(kwargs, "alpha", PyFloat_FromDouble(alpha));
199-
299+
200300
for(size_t i = 0; i < y.size(); ++i) {
201301
PyList_SetItem(ylist, i, PyFloat_FromDouble(y.at(i)));
202302
}
@@ -222,9 +322,9 @@ namespace matplotlibcpp {
222322
PyObject* kwargs = PyDict_New();
223323
PyDict_SetItemString(kwargs, "label", PyString_FromString(label.c_str()));
224324
PyDict_SetItemString(kwargs, "bins", PyLong_FromLong(bins));
225-
PyDict_SetItemString(kwargs, "color", PyString_FromString(color.c_str()));
325+
PyDict_SetItemString(kwargs, "color", PyString_FromString(color.c_str()));
226326
PyDict_SetItemString(kwargs, "alpha", PyFloat_FromDouble(alpha));
227-
327+
228328
for(size_t i = 0; i < y.size(); ++i) {
229329
PyList_SetItem(ylist, i, PyFloat_FromDouble(y.at(i)));
230330
}
@@ -241,7 +341,7 @@ namespace matplotlibcpp {
241341

242342
return res;
243343
}
244-
344+
245345
template<typename NumericX, typename NumericY>
246346
bool plot(const std::vector<NumericX>& x, const std::vector<NumericY>& y, const std::string& s = "")
247347
{
@@ -295,9 +395,10 @@ namespace matplotlibcpp {
295395
}
296396

297397
template<typename Numeric>
298-
bool named_plot(const std::string& name, const std::vector<Numeric>& x, const std::vector<Numeric>& y, const std::string& format = "") {
398+
bool named_plot(const std::string& name, const std::vector<Numeric>& x, const std::vector<Numeric>& y, const std::string& format = "",double alpha=1.0) {
299399
PyObject* kwargs = PyDict_New();
300400
PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
401+
PyDict_SetItemString(kwargs, "alpha", PyFloat_FromDouble(alpha));
301402

302403
PyObject* xlist = PyList_New(x.size());
303404
PyObject* ylist = PyList_New(y.size());
@@ -335,14 +436,14 @@ namespace matplotlibcpp {
335436
if(!res) throw std::runtime_error("Call to figure() failed.");
336437
Py_DECREF(res);
337438
}
338-
439+
339440
inline void close(){
340441
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_close, detail::_interpreter::get().s_python_empty_tuple);
341442
if(!res) throw std::runtime_error("Call to close() failed.");
342443

343-
Py_DECREF(res);
444+
Py_DECREF(res);
344445
}
345-
446+
346447
inline void legend() {
347448
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_legend, detail::_interpreter::get().s_python_empty_tuple);
348449
if(!res) throw std::runtime_error("Call to legend() failed.");
@@ -383,8 +484,8 @@ namespace matplotlibcpp {
383484
Py_DECREF(args);
384485
Py_DECREF(res);
385486
}
386-
387-
487+
488+
388489
double * xlim()
389490
{
390491

@@ -394,13 +495,13 @@ namespace matplotlibcpp {
394495
double * arr = new double[2];
395496
arr[0] = PyFloat_AsDouble(left);
396497
arr[1] = PyFloat_AsDouble(right);
397-
398-
if(!res) throw std::runtime_error("Call to xlim() failed.");
498+
499+
if(!res) throw std::runtime_error("Call to xlim() failed.");
399500
Py_DECREF(res);
400501
return arr;
401502
}
402-
403-
503+
504+
404505
double * ylim()
405506
{
406507
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_ylim, detail::_interpreter::get().s_python_empty_tuple);
@@ -409,8 +510,8 @@ namespace matplotlibcpp {
409510
double * arr = new double[2];
410511
arr[0] = PyFloat_AsDouble(left);
411512
arr[1] = PyFloat_AsDouble(right);
412-
413-
if(!res) throw std::runtime_error("Call to ylim() failed.");
513+
514+
if(!res) throw std::runtime_error("Call to ylim() failed.");
414515
Py_DECREF(res);
415516
return arr;
416517
}
@@ -645,8 +746,8 @@ namespace matplotlibcpp {
645746
return plot<double>(x,y,keywords);
646747
}
647748

648-
bool named_plot(const std::string& name, const std::vector<double>& x, const std::vector<double>& y, const std::string& format = "") {
649-
return named_plot<double>(name,x,y,format);
749+
bool named_plot(const std::string& name, const std::vector<double>& x, const std::vector<double>& y, const std::string& format = "",double alpha=1.0) {
750+
return named_plot<double>(name,x,y,format,alpha);
650751
}
651752

652753
#endif

0 commit comments

Comments
 (0)