@@ -33,7 +33,9 @@ namespace matplotlibcpp {
33
33
PyObject *s_python_empty_tuple;
34
34
PyObject *s_python_function_annotate;
35
35
PyObject *s_python_function_close;
36
-
36
+ PyObject *s_python_function_arrow;
37
+ PyObject *s_python_function_contour;
38
+ PyObject *s_python_function_imshow;
37
39
/* For now, _interpreter is implemented as a singleton since its currently not possible to have
38
40
multiple independent embedded python interpreters without patching the python source code
39
41
or starting a separate process for each.
@@ -79,6 +81,9 @@ namespace matplotlibcpp {
79
81
s_python_function_save = PyObject_GetAttrString (pylabmod, " savefig" );
80
82
s_python_function_annotate = PyObject_GetAttrString (pymod," annotate" );
81
83
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" );
82
87
83
88
if ( !s_python_function_show
84
89
|| !s_python_function_figure
@@ -95,6 +100,9 @@ namespace matplotlibcpp {
95
100
|| !s_python_function_save
96
101
|| !s_python_function_annotate
97
102
|| !s_python_function_close
103
+ || !s_python_function_arrow
104
+ || !s_python_function_contour
105
+ || !s_python_function_imshow
98
106
)
99
107
{ throw std::runtime_error (" Couldn't find required function!" ); }
100
108
@@ -113,6 +121,9 @@ namespace matplotlibcpp {
113
121
|| !PyFunction_Check (s_python_function_xlim)
114
122
|| !PyFunction_Check (s_python_function_save)
115
123
|| !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)
116
127
)
117
128
{ throw std::runtime_error (" Python object is unexpectedly not a PyFunction." ); }
118
129
@@ -124,23 +135,112 @@ namespace matplotlibcpp {
124
135
}
125
136
};
126
137
}
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
+
128
228
bool annotate (std::string annotation, double x, double y)
129
229
{
130
230
PyObject * xy = PyTuple_New (2 );
131
231
PyObject * str = PyString_FromString (annotation.c_str ());
132
-
232
+
133
233
PyTuple_SetItem (xy,0 ,PyFloat_FromDouble (x));
134
234
PyTuple_SetItem (xy,1 ,PyFloat_FromDouble (y));
135
-
235
+
136
236
PyObject* kwargs = PyDict_New ();
137
237
PyDict_SetItemString (kwargs, " xy" , xy);
138
-
238
+
139
239
PyObject* args = PyTuple_New (1 );
140
240
PyTuple_SetItem (args, 0 , str);
141
241
142
242
PyObject* res = PyObject_Call (detail::_interpreter::get ().s_python_function_annotate , args, kwargs);
143
-
243
+
144
244
Py_DECREF (args);
145
245
Py_DECREF (kwargs);
146
246
@@ -191,12 +291,12 @@ namespace matplotlibcpp {
191
291
bool hist (const std::vector<Numeric>& y, long bins=10 ,std::string color=" b" , double alpha=1.0 ){
192
292
193
293
PyObject* ylist = PyList_New (y.size ());
194
-
294
+
195
295
PyObject* kwargs = PyDict_New ();
196
296
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 ()));
198
298
PyDict_SetItemString (kwargs, " alpha" , PyFloat_FromDouble (alpha));
199
-
299
+
200
300
for (size_t i = 0 ; i < y.size (); ++i) {
201
301
PyList_SetItem (ylist, i, PyFloat_FromDouble (y.at (i)));
202
302
}
@@ -222,9 +322,9 @@ namespace matplotlibcpp {
222
322
PyObject* kwargs = PyDict_New ();
223
323
PyDict_SetItemString (kwargs, " label" , PyString_FromString (label.c_str ()));
224
324
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 ()));
226
326
PyDict_SetItemString (kwargs, " alpha" , PyFloat_FromDouble (alpha));
227
-
327
+
228
328
for (size_t i = 0 ; i < y.size (); ++i) {
229
329
PyList_SetItem (ylist, i, PyFloat_FromDouble (y.at (i)));
230
330
}
@@ -241,7 +341,7 @@ namespace matplotlibcpp {
241
341
242
342
return res;
243
343
}
244
-
344
+
245
345
template <typename NumericX, typename NumericY>
246
346
bool plot (const std::vector<NumericX>& x, const std::vector<NumericY>& y, const std::string& s = " " )
247
347
{
@@ -295,9 +395,10 @@ namespace matplotlibcpp {
295
395
}
296
396
297
397
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 ) {
299
399
PyObject* kwargs = PyDict_New ();
300
400
PyDict_SetItemString (kwargs, " label" , PyString_FromString (name.c_str ()));
401
+ PyDict_SetItemString (kwargs, " alpha" , PyFloat_FromDouble (alpha));
301
402
302
403
PyObject* xlist = PyList_New (x.size ());
303
404
PyObject* ylist = PyList_New (y.size ());
@@ -335,14 +436,14 @@ namespace matplotlibcpp {
335
436
if (!res) throw std::runtime_error (" Call to figure() failed." );
336
437
Py_DECREF (res);
337
438
}
338
-
439
+
339
440
inline void close (){
340
441
PyObject* res = PyObject_CallObject (detail::_interpreter::get ().s_python_function_close , detail::_interpreter::get ().s_python_empty_tuple );
341
442
if (!res) throw std::runtime_error (" Call to close() failed." );
342
443
343
- Py_DECREF (res);
444
+ Py_DECREF (res);
344
445
}
345
-
446
+
346
447
inline void legend () {
347
448
PyObject* res = PyObject_CallObject (detail::_interpreter::get ().s_python_function_legend , detail::_interpreter::get ().s_python_empty_tuple );
348
449
if (!res) throw std::runtime_error (" Call to legend() failed." );
@@ -383,8 +484,8 @@ namespace matplotlibcpp {
383
484
Py_DECREF (args);
384
485
Py_DECREF (res);
385
486
}
386
-
387
-
487
+
488
+
388
489
double * xlim ()
389
490
{
390
491
@@ -394,13 +495,13 @@ namespace matplotlibcpp {
394
495
double * arr = new double [2 ];
395
496
arr[0 ] = PyFloat_AsDouble (left);
396
497
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." );
399
500
Py_DECREF (res);
400
501
return arr;
401
502
}
402
-
403
-
503
+
504
+
404
505
double * ylim ()
405
506
{
406
507
PyObject* res = PyObject_CallObject (detail::_interpreter::get ().s_python_function_ylim , detail::_interpreter::get ().s_python_empty_tuple );
@@ -409,8 +510,8 @@ namespace matplotlibcpp {
409
510
double * arr = new double [2 ];
410
511
arr[0 ] = PyFloat_AsDouble (left);
411
512
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." );
414
515
Py_DECREF (res);
415
516
return arr;
416
517
}
@@ -645,8 +746,8 @@ namespace matplotlibcpp {
645
746
return plot<double >(x,y,keywords);
646
747
}
647
748
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 );
650
751
}
651
752
652
753
#endif
0 commit comments