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