forked from soblin/matplotlibcpp17
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyplot.h
More file actions
469 lines (399 loc) · 15 KB
/
Copy pathpyplot.h
File metadata and controls
469 lines (399 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/**
* @file pyplot.h
* @brief corresponding header for matplotlib.pyplot
**/
#pragma once
#include <tuple>
#include <matplotlibcpp17/axes.h> // IWYU pragma: export
#include <matplotlibcpp17/common.h> // IWYU pragma: export
#include <matplotlibcpp17/figure.h> // IWYU pragma: export
#include <pybind11/embed.h> // IWYU pragma: export
#include <pybind11/numpy.h> // IWYU pragma: export
#include <pybind11/pybind11.h> // IWYU pragma: export
#include <pybind11/stl.h> // IWYU pragma: export
#include <pybind11/cast.h> // IWYU pragma: export
namespace matplotlibcpp17::pyplot {
/**
* @brief A class corresponding to pyplot module
**/
struct DECL_STRUCT_ATTR PyPlot {
public:
PyPlot() {}
PyPlot(pybind11::module mod_) {
mod = mod_;
load_attrs();
}
// axes
axes::Axes axes(const pybind11::dict &kwargs = pybind11::dict());
// axis
ObjectWrapper axis(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// cla
ObjectWrapper cla(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// clf
ObjectWrapper clf(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// clim
ObjectWrapper clim(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// colorbar
ObjectWrapper colorbar(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// errorbar
ObjectWrapper errorbar(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// figaspect
std::tuple<double, double>
figaspect(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// figure
figure::Figure figure(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// gca
axes::Axes gca(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// gcf
figure::Figure gcf(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// gci
ObjectWrapper gci(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// grid
ObjectWrapper grid(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// imshow
ObjectWrapper imshow(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// legend
ObjectWrapper legend(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// pause
ObjectWrapper pause(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// plot
ObjectWrapper plot(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// quiver
ObjectWrapper quiver(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// savefig
ObjectWrapper savefig(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// scatter
ObjectWrapper scatter(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// show
ObjectWrapper show(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// step
ObjectWrapper step(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// subplot
axes::Axes subplot(const pybind11::dict &kwargs = pybind11::dict());
axes::Axes subplot(int cri);
// subplots
std::tuple<figure::Figure, axes::Axes>
subplots(const pybind11::dict &kwargs = pybind11::dict());
std::tuple<figure::Figure, std::vector<axes::Axes>>
subplots(int r, int c, const pybind11::dict &kwargs = pybind11::dict());
// title
ObjectWrapper title(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// xlabel
ObjectWrapper xlabel(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// xlim
ObjectWrapper xlim(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// ylabel
ObjectWrapper ylabel(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// ylim
ObjectWrapper ylim(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
private:
void load_attrs() {
LOAD_FUNC_ATTR(axes, mod);
LOAD_FUNC_ATTR(axis, mod);
LOAD_FUNC_ATTR(cla, mod);
LOAD_FUNC_ATTR(clf, mod);
LOAD_FUNC_ATTR(clim, mod);
LOAD_FUNC_ATTR(colorbar, mod);
LOAD_FUNC_ATTR(errorbar, mod);
LOAD_FUNC_ATTR(figaspect, mod);
LOAD_FUNC_ATTR(figure, mod);
LOAD_FUNC_ATTR(gca, mod);
LOAD_FUNC_ATTR(gcf, mod);
LOAD_FUNC_ATTR(gci, mod);
LOAD_FUNC_ATTR(grid, mod);
LOAD_FUNC_ATTR(imshow, mod);
LOAD_FUNC_ATTR(legend, mod);
LOAD_FUNC_ATTR(pause, mod);
LOAD_FUNC_ATTR(plot, mod);
LOAD_FUNC_ATTR(quiver, mod);
LOAD_FUNC_ATTR(savefig, mod);
LOAD_FUNC_ATTR(scatter, mod);
LOAD_FUNC_ATTR(show, mod);
LOAD_FUNC_ATTR(step, mod);
LOAD_FUNC_ATTR(subplot, mod);
LOAD_FUNC_ATTR(subplots, mod);
LOAD_FUNC_ATTR(title, mod);
LOAD_FUNC_ATTR(xlabel, mod);
LOAD_FUNC_ATTR(xlim, mod);
LOAD_FUNC_ATTR(ylabel, mod);
LOAD_FUNC_ATTR(ylim, mod);
}
pybind11::module mod;
pybind11::object axes_attr;
pybind11::object axis_attr;
pybind11::object cla_attr;
pybind11::object clf_attr;
pybind11::object clim_attr;
pybind11::object colorbar_attr;
pybind11::object errorbar_attr;
pybind11::object figaspect_attr;
pybind11::object figure_attr;
pybind11::object gca_attr;
pybind11::object gcf_attr;
pybind11::object gci_attr;
pybind11::object grid_attr;
pybind11::object imshow_attr;
pybind11::object legend_attr;
pybind11::object pause_attr;
pybind11::object plot_attr;
pybind11::object quiver_attr;
pybind11::object savefig_attr;
pybind11::object scatter_attr;
pybind11::object show_attr;
pybind11::object step_attr;
pybind11::object subplot_attr;
pybind11::object subplots_attr;
pybind11::object title_attr;
pybind11::object xlabel_attr;
pybind11::object xlim_attr;
pybind11::object ylabel_attr;
pybind11::object ylim_attr;
};
// axes
inline axes::Axes PyPlot::axes(const pybind11::dict &kwargs) {
pybind11::object ax_obj = axes_attr(**kwargs);
return axes::Axes(ax_obj);
}
// axis
inline ObjectWrapper PyPlot::axis(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = axis_attr(*args, **kwargs);
return ObjectWrapper(std::move(ret));
}
// cla
inline ObjectWrapper PyPlot::cla(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = cla_attr(*args, **kwargs);
return ObjectWrapper(std::move(ret));
}
// clf
inline ObjectWrapper PyPlot::clf(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = clf_attr(*args, **kwargs);
return ObjectWrapper(std::move(ret));
}
// clim
inline ObjectWrapper PyPlot::clim(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = clim_attr(*args, **kwargs);
return ObjectWrapper(std::move(ret));
}
// colorbar
inline ObjectWrapper PyPlot::colorbar(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = colorbar_attr(*args, **kwargs);
return ObjectWrapper(std::move(ret));
}
// errorbar
inline ObjectWrapper PyPlot::errorbar(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = errorbar_attr(*args, **kwargs);
return ObjectWrapper(std::move(ret));
}
// figaspect
inline std::tuple<double, double>
PyPlot::figaspect(const pybind11::tuple &args, const pybind11::dict &kwargs) {
pybind11::list l = figaspect_attr(*args, **kwargs);
double width = l[0].cast<double>();
double height = l[1].cast<double>();
return {width, height};
}
// figure
inline figure::Figure PyPlot::figure(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object fig_obj = figure_attr(*args, **kwargs);
return figure::Figure(fig_obj);
}
// gca
inline axes::Axes PyPlot::gca(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object obj = gca_attr(*args, **kwargs);
return axes::Axes(obj);
}
// gcf
inline figure::Figure PyPlot::gcf(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object obj = gcf_attr(*args, **kwargs);
return figure::Figure(obj);
}
// gci
inline ObjectWrapper PyPlot::gci(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object obj = gci_attr(*args, **kwargs);
return obj;
}
// grid
inline ObjectWrapper PyPlot::grid(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object obj = grid_attr(*args, **kwargs);
return obj;
}
// imshow
inline ObjectWrapper PyPlot::imshow(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object obj = imshow_attr(*args, **kwargs);
return obj;
}
// legend
inline ObjectWrapper PyPlot::legend(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = legend_attr(*args, **kwargs);
return ObjectWrapper(std::move(ret));
}
// pause
inline ObjectWrapper PyPlot::pause(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = pause_attr(*args, **kwargs);
return ObjectWrapper(std::move(ret));
}
// plot
inline ObjectWrapper PyPlot::plot(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = plot_attr(*args, **kwargs);
return ObjectWrapper(std::move(ret));
}
// quiver
inline ObjectWrapper PyPlot::quiver(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = quiver_attr(*args, **kwargs);
return ObjectWrapper(std::move(ret));
}
// scatter
inline ObjectWrapper PyPlot::scatter(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = scatter_attr(*args, **kwargs);
return ObjectWrapper(std::move(ret));
}
// savefig
inline ObjectWrapper PyPlot::savefig(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = savefig_attr(*args, **kwargs);
return ObjectWrapper(std::move(ret));
}
// show
inline ObjectWrapper PyPlot::show(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = show_attr(*args, **kwargs);
return ObjectWrapper(std::move(ret));
}
// step
inline ObjectWrapper PyPlot::step(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = step_attr(*args, **kwargs);
return ObjectWrapper(std::move(ret));
}
// subplot
inline axes::Axes PyPlot::subplot(const pybind11::dict &kwargs) {
return axes::Axes(subplot_attr(**kwargs));
}
inline axes::Axes PyPlot::subplot(int cri) {
pybind11::object obj = subplot_attr(cri);
return axes::Axes(obj);
}
// subplots
inline std::tuple<figure::Figure, axes::Axes>
PyPlot::subplots(const pybind11::dict &kwargs) {
pybind11::list ret = subplots_attr(**kwargs);
pybind11::object fig = ret[0];
pybind11::object ax = ret[1];
return {figure::Figure(fig), axes::Axes(ax)};
}
inline std::tuple<figure::Figure, std::vector<axes::Axes>>
PyPlot::subplots(int r, int c, const pybind11::dict &kwargs) {
// subplots() returns [][] (if r > 1 && c > 1) else []
// return []axes in row-major
// NOTE: equal to Axes.flat
pybind11::tuple args = pybind11::make_tuple(r, c);
pybind11::list ret = subplots_attr(*args, **kwargs);
std::vector<axes::Axes> axes;
pybind11::object fig = ret[0];
figure::Figure figure(fig);
if (r == 1 and c == 1) {
// python returns Axes
axes.push_back(axes::Axes(ret[1]));
} else if (r == 1 or c == 1) {
// python returns []Axes
pybind11::list axs = ret[1];
for (int i = 0; i < r * c; ++i)
axes.push_back(axes::Axes(axs[i]));
} else {
// python returns [][]Axes
pybind11::list axs = ret[1];
for (pybind11::size_t i = 0; i < axs.size(); ++i) {
pybind11::list axsi = axs[i];
for (unsigned j = 0; j < axsi.size(); ++j)
axes.push_back(axes::Axes(axsi[j]));
}
}
return {figure, axes};
}
// title
inline ObjectWrapper PyPlot::title(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = title_attr(*args, **kwargs);
return ObjectWrapper(std::move(ret));
}
// xlabel
inline ObjectWrapper PyPlot::xlabel(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = xlabel_attr(*args, **kwargs);
return ObjectWrapper(std::move(ret));
}
// xlim
inline ObjectWrapper PyPlot::xlim(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = xlim_attr(*args, **kwargs);
return ObjectWrapper(std::move(ret));
}
// ylabel
inline ObjectWrapper PyPlot::ylabel(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = ylabel_attr(*args, **kwargs);
return ObjectWrapper(std::move(ret));
}
// ylim
inline ObjectWrapper PyPlot::ylim(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = ylim_attr(*args, **kwargs);
return ObjectWrapper(std::move(ret));
}
inline PyPlot import() {
auto mod = pybind11::module::import("matplotlib.pyplot");
auto g_pyplot = PyPlot(mod);
return g_pyplot;
}
} // namespace matplotlibcpp17::pyplot
// Args & Kwargs
template <typename... ArgsT> pybind11::tuple Args(ArgsT &&...args) {
return pybind11::make_tuple(std::forward<ArgsT>(args)...);
}
using Kwargs = pybind11::dict;
// Export this
namespace py = pybind11;
using namespace py::literals;