From c6eb3b529ba76c3722d47daca2acdb958b90f390 Mon Sep 17 00:00:00 2001 From: MamoruSobue Date: Tue, 8 Feb 2022 13:49:40 +0900 Subject: [PATCH 1/8] removed util.h --- include/matplotlibcpp17/util.h | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 include/matplotlibcpp17/util.h diff --git a/include/matplotlibcpp17/util.h b/include/matplotlibcpp17/util.h deleted file mode 100644 index e69de29..0000000 From fb41da113ca036df66c467741c0c4e36ec9bd755 Mon Sep 17 00:00:00 2001 From: soblin Date: Sat, 12 Feb 2022 17:14:16 +0900 Subject: [PATCH 2/8] renamed LOAD_(NON)VOID_ATTR to LOAD_FUNC_ATTR --- include/matplotlibcpp17/animation.h | 2 +- include/matplotlibcpp17/axes.h | 68 +++++++++++++-------------- include/matplotlibcpp17/collections.h | 4 +- include/matplotlibcpp17/common.h | 7 +-- include/matplotlibcpp17/figure.h | 16 +++---- include/matplotlibcpp17/pyplot.h | 40 ++++++++-------- include/matplotlibcpp17/text.h | 2 +- 7 files changed, 67 insertions(+), 72 deletions(-) diff --git a/include/matplotlibcpp17/animation.h b/include/matplotlibcpp17/animation.h index fbd4623..7a08ed0 100644 --- a/include/matplotlibcpp17/animation.h +++ b/include/matplotlibcpp17/animation.h @@ -14,7 +14,7 @@ struct DECL_STRUCT_ATTR ArtistAnimation { self = attr(*args, **kwargs); load_attrs(); } - void load_attrs() { LOAD_VOID_ATTR(save, self); } + void load_attrs() { LOAD_FUNC_ATTR(save, self); } pybind11::object self; // save diff --git a/include/matplotlibcpp17/axes.h b/include/matplotlibcpp17/axes.h index dce31c2..505a946 100644 --- a/include/matplotlibcpp17/axes.h +++ b/include/matplotlibcpp17/axes.h @@ -29,40 +29,40 @@ struct DECL_STRUCT_ATTR Axes { load_attrs(); } void load_attrs() { - LOAD_VOID_ATTR(add_artist, self); - LOAD_VOID_ATTR(add_collection, self); - LOAD_VOID_ATTR(add_patch, self); - LOAD_VOID_ATTR(axhline, self); - LOAD_NONVOID_ATTR(bar, self); - LOAD_VOID_ATTR(bar_label, self); - LOAD_NONVOID_ATTR(barh, self); - LOAD_VOID_ATTR(fill, self); - LOAD_VOID_ATTR(fill_between, self); - LOAD_VOID_ATTR(fill_betweenx, self); - LOAD_VOID_ATTR(get_lines, self); - LOAD_VOID_ATTR(get_xaxis_transform, self); - LOAD_NONVOID_ATTR(get_xlim, self); - LOAD_NONVOID_ATTR(get_xticklabels, self); - LOAD_VOID_ATTR(grid, self); - LOAD_NONVOID_ATTR(hist, self); - LOAD_VOID_ATTR(hist2d, self); - LOAD_VOID_ATTR(invert_yaxis, self); - LOAD_NONVOID_ATTR(legend, self); - LOAD_VOID_ATTR(plot, self); - LOAD_NONVOID_ATTR(quiver, self); - LOAD_NONVOID_ATTR(quiverkey, self); - LOAD_NONVOID_ATTR(scatter, self); - LOAD_VOID_ATTR(set, self); - LOAD_VOID_ATTR(set_aspect, self); - LOAD_VOID_ATTR(set_title, self); - LOAD_VOID_ATTR(set_xlabel, self); - LOAD_VOID_ATTR(set_xlim, self); - LOAD_VOID_ATTR(set_xticks, self); - LOAD_VOID_ATTR(set_ylabel, self); - LOAD_VOID_ATTR(set_ylim, self); - LOAD_VOID_ATTR(set_yticks, self); - LOAD_VOID_ATTR(text, self); - LOAD_VOID_ATTR(tick_params, self); + LOAD_FUNC_ATTR(add_artist, self); + LOAD_FUNC_ATTR(add_collection, self); + LOAD_FUNC_ATTR(add_patch, self); + LOAD_FUNC_ATTR(axhline, self); + LOAD_FUNC_ATTR(bar, self); + LOAD_FUNC_ATTR(bar_label, self); + LOAD_FUNC_ATTR(barh, self); + LOAD_FUNC_ATTR(fill, self); + LOAD_FUNC_ATTR(fill_between, self); + LOAD_FUNC_ATTR(fill_betweenx, self); + LOAD_FUNC_ATTR(get_lines, self); + LOAD_FUNC_ATTR(get_xaxis_transform, self); + LOAD_FUNC_ATTR(get_xlim, self); + LOAD_FUNC_ATTR(get_xticklabels, self); + LOAD_FUNC_ATTR(grid, self); + LOAD_FUNC_ATTR(hist, self); + LOAD_FUNC_ATTR(hist2d, self); + LOAD_FUNC_ATTR(invert_yaxis, self); + LOAD_FUNC_ATTR(legend, self); + LOAD_FUNC_ATTR(plot, self); + LOAD_FUNC_ATTR(quiver, self); + LOAD_FUNC_ATTR(quiverkey, self); + LOAD_FUNC_ATTR(scatter, self); + LOAD_FUNC_ATTR(set, self); + LOAD_FUNC_ATTR(set_aspect, self); + LOAD_FUNC_ATTR(set_title, self); + LOAD_FUNC_ATTR(set_xlabel, self); + LOAD_FUNC_ATTR(set_xlim, self); + LOAD_FUNC_ATTR(set_xticks, self); + LOAD_FUNC_ATTR(set_ylabel, self); + LOAD_FUNC_ATTR(set_ylim, self); + LOAD_FUNC_ATTR(set_yticks, self); + LOAD_FUNC_ATTR(text, self); + LOAD_FUNC_ATTR(tick_params, self); } pybind11::object self; diff --git a/include/matplotlibcpp17/collections.h b/include/matplotlibcpp17/collections.h index 6f73ba9..3806817 100644 --- a/include/matplotlibcpp17/collections.h +++ b/include/matplotlibcpp17/collections.h @@ -15,7 +15,7 @@ struct DECL_STRUCT_ATTR PathCollection { load_attrs(); } void load_attrs() { - LOAD_NONVOID_ATTR(legend_elements, self); + LOAD_FUNC_ATTR(legend_elements, self); } pybind11::object self; @@ -47,7 +47,7 @@ struct DECL_STRUCT_ATTR PatchCollection { self = attr(*args, **kwargs); load_attrs(); } - void load_attrs() { LOAD_VOID_ATTR(set_array, self); } + void load_attrs() { LOAD_FUNC_ATTR(set_array, self); } pybind11::object self; pybind11::object unwrap() { return self; } diff --git a/include/matplotlibcpp17/common.h b/include/matplotlibcpp17/common.h index 5d1bb90..ffa8f95 100644 --- a/include/matplotlibcpp17/common.h +++ b/include/matplotlibcpp17/common.h @@ -1,12 +1,7 @@ #ifndef MATPLOTLIBCPP17_COMMON_H #define MATPLOTLIBCPP17_COMMON_H -#define LOAD_VOID_ATTR(obj, mod) \ - do { \ - obj = mod.attr(#obj); \ - } while (0) - -#define LOAD_NONVOID_ATTR(obj, mod) \ +#define LOAD_FUNC_ATTR(obj, mod) \ do { \ obj##_attr = mod.attr(#obj); \ } while (0) diff --git a/include/matplotlibcpp17/figure.h b/include/matplotlibcpp17/figure.h index 7e3cf7c..37822cb 100644 --- a/include/matplotlibcpp17/figure.h +++ b/include/matplotlibcpp17/figure.h @@ -15,14 +15,14 @@ struct DECL_STRUCT_ATTR Figure { load_attrs(); } void load_attrs() { - LOAD_NONVOID_ATTR(add_axes, self); - LOAD_NONVOID_ATTR(add_gridspec, self); - LOAD_NONVOID_ATTR(add_subplot, self); - LOAD_VOID_ATTR(align_labels, self); - LOAD_VOID_ATTR(colorbar, self); - LOAD_VOID_ATTR(savefig, self); - LOAD_VOID_ATTR(suptitle, self); - LOAD_VOID_ATTR(tight_layout, self); + LOAD_FUNC_ATTR(add_axes, self); + LOAD_FUNC_ATTR(add_gridspec, self); + LOAD_FUNC_ATTR(add_subplot, self); + LOAD_FUNC_ATTR(align_labels, self); + LOAD_FUNC_ATTR(colorbar, self); + LOAD_FUNC_ATTR(savefig, self); + LOAD_FUNC_ATTR(suptitle, self); + LOAD_FUNC_ATTR(tight_layout, self); } pybind11::object self; diff --git a/include/matplotlibcpp17/pyplot.h b/include/matplotlibcpp17/pyplot.h index ce98b8c..84ad843 100644 --- a/include/matplotlibcpp17/pyplot.h +++ b/include/matplotlibcpp17/pyplot.h @@ -18,26 +18,26 @@ struct DECL_STRUCT_ATTR PyPlot { load_attrs(); } void load_attrs() { - LOAD_NONVOID_ATTR(axes, mod); - LOAD_VOID_ATTR(axis, mod); - LOAD_VOID_ATTR(cla, mod); - LOAD_VOID_ATTR(clf, mod); - LOAD_NONVOID_ATTR(figure, mod); - LOAD_NONVOID_ATTR(gca, mod); - LOAD_NONVOID_ATTR(gcf, mod); - LOAD_VOID_ATTR(legend, mod); - LOAD_VOID_ATTR(pause, mod); - LOAD_VOID_ATTR(plot, mod); - LOAD_VOID_ATTR(quiver, mod); - LOAD_VOID_ATTR(savefig, mod); - LOAD_VOID_ATTR(scatter, mod); - LOAD_VOID_ATTR(show, mod); - LOAD_NONVOID_ATTR(subplot, mod); - LOAD_NONVOID_ATTR(subplots, mod); - LOAD_VOID_ATTR(xlabel, mod); - LOAD_VOID_ATTR(xlim, mod); - LOAD_VOID_ATTR(ylabel, mod); - LOAD_VOID_ATTR(ylim, mod); + LOAD_FUNC_ATTR(axes, mod); + LOAD_FUNC_ATTR(axis, mod); + LOAD_FUNC_ATTR(cla, mod); + LOAD_FUNC_ATTR(clf, mod); + LOAD_FUNC_ATTR(figure, mod); + LOAD_FUNC_ATTR(gca, mod); + LOAD_FUNC_ATTR(gcf, 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(subplot, mod); + LOAD_FUNC_ATTR(subplots, mod); + LOAD_FUNC_ATTR(xlabel, mod); + LOAD_FUNC_ATTR(xlim, mod); + LOAD_FUNC_ATTR(ylabel, mod); + LOAD_FUNC_ATTR(ylim, mod); } pybind11::module mod; diff --git a/include/matplotlibcpp17/text.h b/include/matplotlibcpp17/text.h index 48a880a..898f4ff 100644 --- a/include/matplotlibcpp17/text.h +++ b/include/matplotlibcpp17/text.h @@ -12,7 +12,7 @@ struct DECL_STRUCT_ATTR Text { self = text; load_attrs(); } - void load_attrs() { LOAD_VOID_ATTR(set_rotation, self); } + void load_attrs() { LOAD_FUNC_ATTR(set_rotation, self); } pybind11::object self; pybind11::object set_rotation; }; From 1b75adfa5d985ed05e149a6f749fffa6f7f4342a Mon Sep 17 00:00:00 2001 From: soblin Date: Sat, 12 Feb 2022 19:29:24 +0900 Subject: [PATCH 3/8] in header, all function takes tuple and dict --- CMakeLists.txt | 12 +- gallery/artist_animation/animate_decay.cpp | 11 +- gallery/artist_animation/cla_pause.cpp | 8 +- gallery/artist_animation/random_walk.cpp | 6 +- include/matplotlibcpp17/animation.h | 17 +- include/matplotlibcpp17/axes.h | 432 ++++++++++++++++----- include/matplotlibcpp17/collections.h | 38 +- include/matplotlibcpp17/container.h | 7 +- include/matplotlibcpp17/figure.h | 117 ++++-- include/matplotlibcpp17/gridspec.h | 11 +- include/matplotlibcpp17/legend.h | 10 +- include/matplotlibcpp17/patches.h | 20 +- include/matplotlibcpp17/pyplot.h | 249 +++++++++--- include/matplotlibcpp17/quiver.h | 10 +- include/matplotlibcpp17/text.h | 15 +- 15 files changed, 699 insertions(+), 264 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9da12aa..d889a16 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,11 +24,11 @@ endfunction() if(${ADD_DEMO}) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_FLAGS "-Wall -g -DUSE_GUI=${USE_GUI}") - add_subdirectory(gallery/lines_bars_and_markers) - add_subdirectory(gallery/subplots_axes_and_figures) - add_subdirectory(gallery/statistics) - add_subdirectory(gallery/images_contours_and_fields) - add_subdirectory(gallery/shapes_and_collections) + # add_subdirectory(gallery/lines_bars_and_markers) + # add_subdirectory(gallery/subplots_axes_and_figures) + # add_subdirectory(gallery/statistics) + # add_subdirectory(gallery/images_contours_and_fields) + # add_subdirectory(gallery/shapes_and_collections) add_subdirectory(gallery/artist_animation) endif() @@ -37,5 +37,5 @@ if(NOT DEFINED DO_TEST) endif() if(${DO_TEST}) - add_subdirectory(tests) + # add_subdirectory(tests) endif() diff --git a/gallery/artist_animation/animate_decay.cpp b/gallery/artist_animation/animate_decay.cpp index 7d493ad..abdf959 100644 --- a/gallery/artist_animation/animate_decay.cpp +++ b/gallery/artist_animation/animate_decay.cpp @@ -22,8 +22,8 @@ int main() { const int N = 200; vector ts, ys; auto [fig, ax] = plt.subplots(); - ax.set_xlim(0, 10); - ax.set_ylim(-1.1, 1.1); + ax.set_xlim(args_(0, 10)); + ax.set_ylim(args_(-1.1, 1.1)); py::list artist_list; for (int i = 0; i < N; ++i) { double t = i * 1.0 / 10; @@ -32,8 +32,9 @@ int main() { ys.push_back(y); auto [xmin, xmax] = ax.get_xlim(); if (t >= xmax) - ax.set_xlim(xmin, 2 * xmax); - py::object line = ax.plot(ts, ys, "color"_a = "blue", "lw"_a = 1); + ax.set_xlim(args_(xmin, 2 * xmax)); + py::object line = + ax.plot(args_(ts, ys), kwargs_("color"_a = "blue", "lw"_a = 1)); artist_list.append(line); } auto ani = ArtistAnimation(args_(fig.unwrap(), artist_list), @@ -41,7 +42,7 @@ int main() { #if USE_GUI plt.show(); #else - ani.save("animate_decay.gif", "writer"_a = "pillow"); + ani.save(args_("animate_decay.gif"), kwargs_("writer"_a = "pillow")); #endif return 0; } diff --git a/gallery/artist_animation/cla_pause.cpp b/gallery/artist_animation/cla_pause.cpp index 89fe56e..ed5e777 100644 --- a/gallery/artist_animation/cla_pause.cpp +++ b/gallery/artist_animation/cla_pause.cpp @@ -18,11 +18,11 @@ int main() { vector data; const int N = 10; for (int i = 0; i <= N; ++i) { - plt.xlim(0, 10); - plt.ylim(0, 10); + plt.xlim(args_(0, 10)); + plt.ylim(args_(0, 10)); data.push_back(i); - plt.plot(data); - plt.pause(1); + plt.plot(args_(data)); + plt.pause(args_(1)); plt.cla(); } return 0; diff --git a/gallery/artist_animation/random_walk.cpp b/gallery/artist_animation/random_walk.cpp index a9122f6..1108799 100644 --- a/gallery/artist_animation/random_walk.cpp +++ b/gallery/artist_animation/random_walk.cpp @@ -34,7 +34,7 @@ int main() { py::scoped_interpreter guard{}; auto plt = matplotlibcpp17::pyplot::import(); auto fig = plt.figure(); - auto ax = fig.add_subplot(py::tuple(), kwargs_("projection"_a = "3d")); + auto ax = fig.add_subplot(py::make_tuple(), kwargs_("projection"_a = "3d")); py::list artist_list; const int M = 4; const int N = 30; @@ -55,7 +55,7 @@ int main() { ys.push_back(pos[1]); zs.push_back(pos[2]); }); - ax.plot(xs, ys, zs, "color"_a = colors[i]); + ax.plot(args_(xs, ys, zs), kwargs_("color"_a = colors[i])); } artist_list.append(ax.get_lines()); } @@ -64,7 +64,7 @@ int main() { #if USE_GUI plt.show(); #else - ani.save("random_walk.gif", "writer"_a = "pillow"); + ani.save(args_("random_walk.gif"), kwargs_("writer"_a = "pillow")); #endif return 0; } diff --git a/include/matplotlibcpp17/animation.h b/include/matplotlibcpp17/animation.h index 7a08ed0..68b63fd 100644 --- a/include/matplotlibcpp17/animation.h +++ b/include/matplotlibcpp17/animation.h @@ -8,19 +8,30 @@ namespace matplotlibcpp17::animation { struct DECL_STRUCT_ATTR ArtistAnimation { +public: ArtistAnimation(const pybind11::tuple &args, const pybind11::dict &kwargs) { pybind11::object attr = pybind11::module::import("matplotlib.animation") .attr("ArtistAnimation"); self = attr(*args, **kwargs); load_attrs(); } + // save + pybind11::object save(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); + +private: void load_attrs() { LOAD_FUNC_ATTR(save, self); } pybind11::object self; - - // save - pybind11::object save; + pybind11::object save_attr; }; +// save +pybind11::object ArtistAnimation::save(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = save_attr(*args, **kwargs); + return ret; +} + } // namespace matplotlibcpp17::animation #endif /* MATPLOTLIBCPP17_ANIMATION_H */ diff --git a/include/matplotlibcpp17/axes.h b/include/matplotlibcpp17/axes.h index 505a946..3630957 100644 --- a/include/matplotlibcpp17/axes.h +++ b/include/matplotlibcpp17/axes.h @@ -24,181 +24,261 @@ namespace matplotlibcpp17::axes { * @brief A wrapper class for matplotlib.axes.Axes **/ struct DECL_STRUCT_ATTR Axes { +public: Axes(pybind11::object axes) { self = axes; load_attrs(); } - void load_attrs() { - LOAD_FUNC_ATTR(add_artist, self); - LOAD_FUNC_ATTR(add_collection, self); - LOAD_FUNC_ATTR(add_patch, self); - LOAD_FUNC_ATTR(axhline, self); - LOAD_FUNC_ATTR(bar, self); - LOAD_FUNC_ATTR(bar_label, self); - LOAD_FUNC_ATTR(barh, self); - LOAD_FUNC_ATTR(fill, self); - LOAD_FUNC_ATTR(fill_between, self); - LOAD_FUNC_ATTR(fill_betweenx, self); - LOAD_FUNC_ATTR(get_lines, self); - LOAD_FUNC_ATTR(get_xaxis_transform, self); - LOAD_FUNC_ATTR(get_xlim, self); - LOAD_FUNC_ATTR(get_xticklabels, self); - LOAD_FUNC_ATTR(grid, self); - LOAD_FUNC_ATTR(hist, self); - LOAD_FUNC_ATTR(hist2d, self); - LOAD_FUNC_ATTR(invert_yaxis, self); - LOAD_FUNC_ATTR(legend, self); - LOAD_FUNC_ATTR(plot, self); - LOAD_FUNC_ATTR(quiver, self); - LOAD_FUNC_ATTR(quiverkey, self); - LOAD_FUNC_ATTR(scatter, self); - LOAD_FUNC_ATTR(set, self); - LOAD_FUNC_ATTR(set_aspect, self); - LOAD_FUNC_ATTR(set_title, self); - LOAD_FUNC_ATTR(set_xlabel, self); - LOAD_FUNC_ATTR(set_xlim, self); - LOAD_FUNC_ATTR(set_xticks, self); - LOAD_FUNC_ATTR(set_ylabel, self); - LOAD_FUNC_ATTR(set_ylim, self); - LOAD_FUNC_ATTR(set_yticks, self); - LOAD_FUNC_ATTR(text, self); - LOAD_FUNC_ATTR(tick_params, self); - } - pybind11::object self; - /** * @fn unwrap() - * @brief return python object for passing this wrapper class to plotting function + * @brief return python object for passing this wrapper class to plotting + *function **/ pybind11::object unwrap() { return self; } - /** - * @fn add_artist(*args, **kwargs) - **/ - pybind11::object add_artist; + // add_artist + pybind11::object add_artist(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); - /** - * @fn add_collection(*args, **kwargs) - **/ - pybind11::object add_collection; + // add_collection + pybind11::object + add_collection(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // add_patch - pybind11::object add_patch; + pybind11::object add_patch(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // axhline - pybind11::object axhline; + pybind11::object axhline(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // bar - container::BarContainer bar(const pybind11::tuple &args, - const pybind11::dict &kwargs); - pybind11::object bar_attr; + container::BarContainer bar(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // bar_label - pybind11::object bar_label; + pybind11::object bar_label(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // barh - container::BarContainer barh(const pybind11::tuple &args, - const pybind11::dict &kwargs); - pybind11::object barh_attr; + container::BarContainer barh(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // fill - pybind11::object fill; + pybind11::object fill(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // fill_between - pybind11::object fill_between; + pybind11::object + fill_between(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // fill_betweenx - pybind11::object fill_betweenx; + pybind11::object + fill_betweenx(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // get_lines - pybind11::object get_lines; + pybind11::object get_lines(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // get_xaxis_transform - /// TODO: currently returns py::object - pybind11::object get_xaxis_transform; + pybind11::object + get_xaxis_transform(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // get_xlim std::tuple get_xlim(); - pybind11::object get_xlim_attr; // get_xticklabels std::vector get_xticklabels(); - pybind11::object get_xticklabels_attr; // grid - pybind11::object grid; + pybind11::object grid(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // hist std::tuple, std::vector, container::BarContainer> - hist(const pybind11::tuple &args, const pybind11::dict &kwargs); - pybind11::object hist_attr; + hist(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // hist2d - pybind11::object hist2d; + pybind11::object hist2d(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // invert_yaxis - pybind11::object invert_yaxis; + pybind11::object + invert_yaxis(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // legend - legend::Legend legend(const pybind11::tuple &args, - const pybind11::dict &kwargs); - - pybind11::object legend_attr; + legend::Legend legend(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // plot - // TODO: should return `artists` for gif? - pybind11::object plot; + pybind11::object plot(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // quiver - quiver::Quiver quiver(const pybind11::tuple &args, - const pybind11::dict &kwargs); - pybind11::object quiver_attr; + quiver::Quiver quiver(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // quiverkey - quiver::QuiverKey quiverkey(const pybind11::tuple &args, - const pybind11::dict &kwargs); - pybind11::object quiverkey_attr; + quiver::QuiverKey quiverkey(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // scatter - // TODO: should return `artist` for gif? - collections::PathCollection scatter(const pybind11::tuple &args, - const pybind11::dict &kwargs); - pybind11::object scatter_attr; + collections::PathCollection + scatter(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // set - pybind11::object set; + pybind11::object set(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); - // set - pybind11::object set_aspect; + // set_aspect + pybind11::object set_aspect(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // set_title - pybind11::object set_title; + pybind11::object set_title(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // set_xlabel - pybind11::object set_xlabel; + pybind11::object set_xlabel(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // set_xlim - pybind11::object set_xlim; + pybind11::object set_xlim(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // set_xticks - pybind11::object set_xticks; + pybind11::object set_xticks(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // set_ylabel - pybind11::object set_ylabel; + pybind11::object set_ylabel(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // set_ylim - pybind11::object set_ylim; + pybind11::object set_ylim(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // set_yticks - pybind11::object set_yticks; + pybind11::object set_yticks(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // text - pybind11::object text; + pybind11::object text(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // tick_params - pybind11::object tick_params; + pybind11::object tick_params(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); + +private: + void load_attrs() { + LOAD_FUNC_ATTR(add_artist, self); + LOAD_FUNC_ATTR(add_collection, self); + LOAD_FUNC_ATTR(add_patch, self); + LOAD_FUNC_ATTR(axhline, self); + LOAD_FUNC_ATTR(bar, self); + LOAD_FUNC_ATTR(bar_label, self); + LOAD_FUNC_ATTR(barh, self); + LOAD_FUNC_ATTR(fill, self); + LOAD_FUNC_ATTR(fill_between, self); + LOAD_FUNC_ATTR(fill_betweenx, self); + LOAD_FUNC_ATTR(get_lines, self); + LOAD_FUNC_ATTR(get_xaxis_transform, self); + LOAD_FUNC_ATTR(get_xlim, self); + LOAD_FUNC_ATTR(get_xticklabels, self); + LOAD_FUNC_ATTR(grid, self); + LOAD_FUNC_ATTR(hist, self); + LOAD_FUNC_ATTR(hist2d, self); + LOAD_FUNC_ATTR(invert_yaxis, self); + LOAD_FUNC_ATTR(legend, self); + LOAD_FUNC_ATTR(plot, self); + LOAD_FUNC_ATTR(quiver, self); + LOAD_FUNC_ATTR(quiverkey, self); + LOAD_FUNC_ATTR(scatter, self); + LOAD_FUNC_ATTR(set, self); + LOAD_FUNC_ATTR(set_aspect, self); + LOAD_FUNC_ATTR(set_title, self); + LOAD_FUNC_ATTR(set_xlabel, self); + LOAD_FUNC_ATTR(set_xlim, self); + LOAD_FUNC_ATTR(set_xticks, self); + LOAD_FUNC_ATTR(set_ylabel, self); + LOAD_FUNC_ATTR(set_ylim, self); + LOAD_FUNC_ATTR(set_yticks, self); + LOAD_FUNC_ATTR(text, self); + LOAD_FUNC_ATTR(tick_params, self); + } + pybind11::object self; + pybind11::object add_artist_attr; + pybind11::object add_collection_attr; + pybind11::object add_patch_attr; + pybind11::object axhline_attr; + pybind11::object bar_attr; + pybind11::object bar_label_attr; + pybind11::object barh_attr; + pybind11::object fill_attr; + pybind11::object fill_between_attr; + pybind11::object fill_betweenx_attr; + pybind11::object get_lines_attr; + pybind11::object get_xaxis_transform_attr; + pybind11::object get_xlim_attr; + pybind11::object get_xticklabels_attr; + pybind11::object grid_attr; + pybind11::object hist_attr; + pybind11::object hist2d_attr; + pybind11::object invert_yaxis_attr; + pybind11::object legend_attr; + pybind11::object plot_attr; + pybind11::object quiver_attr; + pybind11::object quiverkey_attr; + pybind11::object scatter_attr; + pybind11::object set_attr; + pybind11::object set_aspect_attr; + pybind11::object set_title_attr; + pybind11::object set_xlabel_attr; + pybind11::object set_xlim_attr; + pybind11::object set_xticks_attr; + pybind11::object set_ylabel_attr; + pybind11::object set_ylim_attr; + pybind11::object set_yticks_attr; + pybind11::object text_attr; + pybind11::object tick_params_attr; }; +// add_artist +pybind11::object Axes::add_artist(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = add_artist_attr(*args, **kwargs); + return ret; +} + +// add_collection +pybind11::object Axes::add_collection(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = add_collection_attr(*args, **kwargs); + return ret; +} + +// add_patch +pybind11::object Axes::add_patch(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = add_patch_attr(*args, **kwargs); + return ret; +} + +// axhline +pybind11::object Axes::axhline(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = axhline_attr(*args, **kwargs); + return ret; +} + // bar container::BarContainer Axes::bar(const pybind11::tuple &args, const pybind11::dict &kwargs) { @@ -206,6 +286,13 @@ container::BarContainer Axes::bar(const pybind11::tuple &args, return container::BarContainer(obj); } +// bar_label +pybind11::object Axes::bar_label(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = bar_label_attr(*args, **kwargs); + return ret; +} + // barh container::BarContainer Axes::barh(const pybind11::tuple &args, const pybind11::dict &kwargs) { @@ -213,6 +300,34 @@ container::BarContainer Axes::barh(const pybind11::tuple &args, return container::BarContainer(obj); } +// fill +pybind11::object Axes::fill(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = fill_attr(*args, **kwargs); + return ret; +} + +// fill_between +pybind11::object Axes::fill_between(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = fill_between_attr(*args, **kwargs); + return ret; +} + +// fill_betweenx +pybind11::object Axes::fill_betweenx(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = fill_betweenx_attr(*args, **kwargs); + return ret; +} + +// get_lines +pybind11::object Axes::get_lines(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = get_lines_attr(*args, **kwargs); + return ret; +} + // get_xlim std::tuple Axes::get_xlim() { pybind11::list ret = get_xlim_attr(); @@ -231,6 +346,13 @@ std::vector Axes::get_xticklabels() { return texts; } +// grid +pybind11::object Axes::grid(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = grid_attr(*args, **kwargs); + return ret; +} + // hist std::tuple, std::vector, container::BarContainer> Axes::hist(const pybind11::tuple &args, const pybind11::dict &kwargs) { @@ -248,36 +370,132 @@ Axes::hist(const pybind11::tuple &args, const pybind11::dict &kwargs) { return {N, bins, container::BarContainer(patches_obj)}; } +// hist2d +pybind11::object Axes::hist2d(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = hist2d_attr(*args, **kwargs); + return ret; +} + +// invert_yaxis +pybind11::object Axes::invert_yaxis(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = invert_yaxis_attr(*args, **kwargs); + return ret; +} + // legend -legend::Legend Axes::legend(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()) { +legend::Legend Axes::legend(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = legend_attr(*args, **kwargs); return legend::Legend(obj); } +// plot +pybind11::object Axes::plot(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = plot_attr(*args, **kwargs); + return ret; +} + // quiver -quiver::Quiver Axes::quiver(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()) { +quiver::Quiver Axes::quiver(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = quiver_attr(*args, **kwargs); return quiver::Quiver(obj); } // quiverkey -quiver::QuiverKey -Axes::quiverkey(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()) { +quiver::QuiverKey Axes::quiverkey(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = quiverkey_attr(*args, **kwargs); return quiver::QuiverKey(obj); } // scatter -collections::PathCollection -Axes::scatter(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()) { +collections::PathCollection Axes::scatter(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = scatter_attr(*args, **kwargs); return collections::PathCollection(obj); } +// set +pybind11::object Axes::set(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = set_attr(*args, **kwargs); + return ret; +} + +// set_aspect +pybind11::object Axes::set_aspect(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = set_aspect_attr(*args, **kwargs); + return ret; +} + +// set_title +pybind11::object Axes::set_title(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = set_title_attr(*args, **kwargs); + return ret; +} + +// set_xlabel +pybind11::object Axes::set_xlabel(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = set_xlabel_attr(*args, **kwargs); + return ret; +} + +// set_xlim +pybind11::object Axes::set_xlim(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = set_xlim_attr(*args, **kwargs); + return ret; +} + +// set_xticks +pybind11::object Axes::set_xticks(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = set_xticks_attr(*args, **kwargs); + return ret; +} + +// set_ylabel +pybind11::object Axes::set_ylabel(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = set_ylabel_attr(*args, **kwargs); + return ret; +} + +// set_ylim +pybind11::object Axes::set_ylim(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = set_ylim_attr(*args, **kwargs); + return ret; +} + +// set_yticks +pybind11::object Axes::set_yticks(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = set_yticks_attr(*args, **kwargs); + return ret; +} + +// text +pybind11::object Axes::text(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = text_attr(*args, **kwargs); + return ret; +} + +// tick_params +pybind11::object Axes::tick_params(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = tick_params_attr(*args, **kwargs); + return ret; +} + } // namespace matplotlibcpp17::axes #endif /* MATPLOTLIBCPP17_AXES_H */ diff --git a/include/matplotlibcpp17/collections.h b/include/matplotlibcpp17/collections.h index 3806817..ecacdf2 100644 --- a/include/matplotlibcpp17/collections.h +++ b/include/matplotlibcpp17/collections.h @@ -10,29 +10,30 @@ namespace matplotlibcpp17::collections { struct DECL_STRUCT_ATTR PathCollection { +public: PathCollection(pybind11::object pathcollection) { self = pathcollection; load_attrs(); } - void load_attrs() { - LOAD_FUNC_ATTR(legend_elements, self); - } - pybind11::object self; - pybind11::object unwrap() { return self; } // legend_elements - pybind11::object legend_elements_attr; std::pair - legend_elements(const pybind11::tuple &args, const pybind11::dict &kwargs); + legend_elements(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); + +private: + void load_attrs() { LOAD_FUNC_ATTR(legend_elements, self); } + pybind11::object self; + pybind11::object legend_elements_attr; }; // legend_elements /// NOTE: this func does not return list of Line2Ds(handles) and list of /// strs(labels) unlike original python func -std::pair PathCollection::legend_elements( - const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()) { +std::pair +PathCollection::legend_elements(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::list ret = legend_elements_attr(*args, **kwargs); pybind11::object handles = ret[0]; pybind11::object labels = ret[1]; @@ -40,6 +41,7 @@ std::pair PathCollection::legend_elements( } struct DECL_STRUCT_ATTR PatchCollection { +public: PatchCollection(const pybind11::tuple &args = pybind11::tuple(), const pybind11::dict &kwargs = pybind11::dict()) { pybind11::object attr = pybind11::module::import("matplotlib.collections") @@ -47,15 +49,23 @@ struct DECL_STRUCT_ATTR PatchCollection { self = attr(*args, **kwargs); load_attrs(); } - void load_attrs() { LOAD_FUNC_ATTR(set_array, self); } - pybind11::object self; - pybind11::object unwrap() { return self; } // set_array - pybind11::object set_array; + pybind11::object set_array(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); + +private: + void load_attrs() { LOAD_FUNC_ATTR(set_array, self); } + pybind11::object self; + pybind11::object set_array_attr; }; +pybind11::object PatchCollection::set_array(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = set_array_attr(*args, **kwargs); + return ret; +} } // namespace matplotlibcpp17::collections #endif /* MATPLOTLIBCPP17_COLLECTIONS_H */ diff --git a/include/matplotlibcpp17/container.h b/include/matplotlibcpp17/container.h index 7e109f4..8b70955 100644 --- a/include/matplotlibcpp17/container.h +++ b/include/matplotlibcpp17/container.h @@ -8,12 +8,13 @@ namespace matplotlibcpp17::container { struct DECL_STRUCT_ATTR BarContainer { +public: BarContainer(pybind11::object bar_container) { self = bar_container; } - pybind11::object self; - - // for passing as python object pybind11::object unwrap() { return self; } + +private: + pybind11::object self; }; } // namespace matplotlibcpp17::container diff --git a/include/matplotlibcpp17/figure.h b/include/matplotlibcpp17/figure.h index 37822cb..2a4fc9f 100644 --- a/include/matplotlibcpp17/figure.h +++ b/include/matplotlibcpp17/figure.h @@ -10,77 +10,126 @@ namespace matplotlibcpp17::figure { struct DECL_STRUCT_ATTR Figure { +public: Figure(pybind11::object figure) { self = figure; load_attrs(); } - void load_attrs() { - LOAD_FUNC_ATTR(add_axes, self); - LOAD_FUNC_ATTR(add_gridspec, self); - LOAD_FUNC_ATTR(add_subplot, self); - LOAD_FUNC_ATTR(align_labels, self); - LOAD_FUNC_ATTR(colorbar, self); - LOAD_FUNC_ATTR(savefig, self); - LOAD_FUNC_ATTR(suptitle, self); - LOAD_FUNC_ATTR(tight_layout, self); - } - pybind11::object self; - // for passing as python object pybind11::object unwrap() { return self; } // add_axes - axes::Axes add_axes(const pybind11::tuple &args, - const pybind11::dict &kwargs); - pybind11::object add_axes_attr; + axes::Axes add_axes(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // add_gridspec - gridspec::GridSpec add_gridspec(int nrow, int ncol, - const pybind11::dict &kwargs); - pybind11::object add_gridspec_attr; + gridspec::GridSpec + add_gridspec(int nrow, int ncol, + const pybind11::dict &kwargs = pybind11::dict()); // add_subplot - axes::Axes add_subplot(const pybind11::tuple &args, - const pybind11::dict &kwargs); - pybind11::object add_subplot_attr; + axes::Axes add_subplot(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // align_labels - pybind11::object align_labels; + pybind11::object + align_labels(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // colorbar - pybind11::object colorbar; + pybind11::object colorbar(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // savefig - pybind11::object savefig; + pybind11::object savefig(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // suptitle - pybind11::object suptitle; + pybind11::object suptitle(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // tight_layout - pybind11::object tight_layout; + pybind11::object + tight_layout(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); + +private: + void load_attrs() { + LOAD_FUNC_ATTR(add_axes, self); + LOAD_FUNC_ATTR(add_gridspec, self); + LOAD_FUNC_ATTR(add_subplot, self); + LOAD_FUNC_ATTR(align_labels, self); + LOAD_FUNC_ATTR(colorbar, self); + LOAD_FUNC_ATTR(savefig, self); + LOAD_FUNC_ATTR(suptitle, self); + LOAD_FUNC_ATTR(tight_layout, self); + } + pybind11::object self; + pybind11::object add_axes_attr; + pybind11::object add_gridspec_attr; + pybind11::object add_subplot_attr; + pybind11::object align_labels_attr; + pybind11::object colorbar_attr; + pybind11::object savefig_attr; + pybind11::object suptitle_attr; + pybind11::object tight_layout_attr; }; // add_axes -axes::Axes Figure::add_axes(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()) { +axes::Axes Figure::add_axes(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = add_axes_attr(*args, **kwargs); return axes::Axes(obj); } // add_gridspec -gridspec::GridSpec -Figure::add_gridspec(int nrow, int ncol, - const pybind11::dict &kwargs = pybind11::dict()) { +gridspec::GridSpec Figure::add_gridspec(int nrow, int ncol, + const pybind11::dict &kwargs) { return gridspec::GridSpec(nrow, ncol, kwargs); } // add_subplot -axes::Axes -Figure::add_subplot(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()) { +axes::Axes Figure::add_subplot(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = add_subplot_attr(*args, **kwargs); return axes::Axes(obj); } + +// align_labels +pybind11::object Figure::align_labels(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = align_labels_attr(*args, **kwargs); + return ret; +} + +// colorbar +pybind11::object Figure::colorbar(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = colorbar_attr(*args, **kwargs); + return ret; +} + +// savefig +pybind11::object Figure::savefig(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = savefig_attr(*args, **kwargs); + return ret; +} + +// suptitle +pybind11::object Figure::suptitle(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = suptitle_attr(*args, **kwargs); + return ret; +} + +// tight_layout +pybind11::object Figure::tight_layout(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = tight_layout_attr(*args, **kwargs); + return ret; +} + } // namespace matplotlibcpp17::figure #endif /* MATPLOTLIBCPP17_FIGURE_H */ diff --git a/include/matplotlibcpp17/gridspec.h b/include/matplotlibcpp17/gridspec.h index f37f953..6dac158 100644 --- a/include/matplotlibcpp17/gridspec.h +++ b/include/matplotlibcpp17/gridspec.h @@ -8,14 +8,17 @@ namespace matplotlibcpp17::gridspec { struct DECL_STRUCT_ATTR SubplotSpec { +public: SubplotSpec(pybind11::object subplotspec) { self = subplotspec; } - pybind11::object self; - // for passing as python object pybind11::object unwrap() { return self; } + +private: + pybind11::object self; }; struct DECL_STRUCT_ATTR GridSpec { +public: GridSpec(int nrow_, int ncol_, const pybind11::dict &kwargs = pybind11::dict()) { nrow = nrow_; @@ -23,14 +26,14 @@ struct DECL_STRUCT_ATTR GridSpec { gridspec_attr = pybind11::module::import("matplotlib.gridspec").attr("GridSpec"); self = gridspec_attr(nrow, ncol, **kwargs); - load_attrs(); } - void load_attrs() { return; } template SubplotSpec operator()(const Rows &r, const Cols &c) { pybind11::object obj = self[pybind11::make_tuple(r, c)]; return SubplotSpec(obj); } + +private: int nrow, ncol; pybind11::object self; pybind11::object gridspec_attr; diff --git a/include/matplotlibcpp17/legend.h b/include/matplotlibcpp17/legend.h index e113a10..c9ccdcb 100644 --- a/include/matplotlibcpp17/legend.h +++ b/include/matplotlibcpp17/legend.h @@ -8,17 +8,17 @@ namespace matplotlibcpp17::legend { struct DECL_STRUCT_ATTR Legend { +public: Legend(pybind11::object obj) { self = obj; - load_attrs(); } - void load_attrs() { return; } - pybind11::object self; - - // for passing as python object pybind11::object unwrap() { return self; } + +private: + pybind11::object self; }; + } // namespace matplotlibcpp17::legend #endif /* MATPLOTLIBCPP17_LEGEND_H */ diff --git a/include/matplotlibcpp17/patches.h b/include/matplotlibcpp17/patches.h index 2a69c03..f60f4cc 100644 --- a/include/matplotlibcpp17/patches.h +++ b/include/matplotlibcpp17/patches.h @@ -8,60 +8,64 @@ namespace matplotlibcpp17::patches { struct DECL_STRUCT_ATTR Circle { +public: Circle(const pybind11::tuple &args = pybind11::tuple(), const pybind11::dict &kwargs = pybind11::dict()) { circle_attr = pybind11::module::import("matplotlib.patches").attr("Circle"); self = circle_attr(*args, **kwargs); } - pybind11::object self; - // for passing as python object pybind11::object unwrap() { return self; } +private: + pybind11::object self; pybind11::object circle_attr; }; struct DECL_STRUCT_ATTR Ellipse { +public: Ellipse(const pybind11::tuple &args = pybind11::tuple(), const pybind11::dict &kwargs = pybind11::dict()) { ellipse_attr = pybind11::module::import("matplotlib.patches").attr("Ellipse"); self = ellipse_attr(*args, **kwargs); } - pybind11::object self; - // for passing as python object pybind11::object unwrap() { return self; } +private: + pybind11::object self; pybind11::object ellipse_attr; }; struct DECL_STRUCT_ATTR Rectangle { +public: Rectangle(const pybind11::tuple &args = pybind11::tuple(), const pybind11::dict &kwargs = pybind11::dict()) { rectangle_attr = pybind11::module::import("matplotlib.patches").attr("Rectangle"); self = rectangle_attr(*args, **kwargs); } - pybind11::object self; - // for passing as python object pybind11::object unwrap() { return self; } +private: + pybind11::object self; pybind11::object rectangle_attr; }; struct DECL_STRUCT_ATTR Wedge { +public: Wedge(const pybind11::tuple &args = pybind11::tuple(), const pybind11::dict &kwargs = pybind11::dict()) { wedge_attr = pybind11::module::import("matplotlib.patches").attr("Wedge"); self = wedge_attr(*args, **kwargs); } - pybind11::object self; - // for passing as python object pybind11::object unwrap() { return self; } +private: + pybind11::object self; pybind11::object wedge_attr; }; diff --git a/include/matplotlibcpp17/pyplot.h b/include/matplotlibcpp17/pyplot.h index 84ad843..01322f0 100644 --- a/include/matplotlibcpp17/pyplot.h +++ b/include/matplotlibcpp17/pyplot.h @@ -12,136 +12,231 @@ namespace matplotlibcpp17::pyplot { struct DECL_STRUCT_ATTR PyPlot { +public: PyPlot() {} PyPlot(pybind11::module mod_) { mod = mod_; load_attrs(); } - 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(figure, mod); - LOAD_FUNC_ATTR(gca, mod); - LOAD_FUNC_ATTR(gcf, 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(subplot, mod); - LOAD_FUNC_ATTR(subplots, mod); - LOAD_FUNC_ATTR(xlabel, mod); - LOAD_FUNC_ATTR(xlim, mod); - LOAD_FUNC_ATTR(ylabel, mod); - LOAD_FUNC_ATTR(ylim, mod); - } - pybind11::module mod; - // axes axes::Axes axes(const pybind11::dict &kwargs); - pybind11::object axes_attr; // axis - pybind11::object axis; + pybind11::object axis(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // cla - pybind11::object cla; + pybind11::object cla(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // clf - pybind11::object clf; + pybind11::object clf(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // figure - figure::Figure figure(const pybind11::tuple &args, - const pybind11::dict &kwargs); - pybind11::object figure_attr; + figure::Figure figure(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // gca - axes::Axes gca(const pybind11::tuple &args, const pybind11::dict &kwargs); - pybind11::object gca_attr; + axes::Axes gca(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // gcf - figure::Figure gcf(const pybind11::tuple &args, const pybind11::dict &kwargs); - pybind11::object gcf_attr; + figure::Figure gcf(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // legend - pybind11::object legend; + pybind11::object legend(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // pause - pybind11::object pause; + pybind11::object pause(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // plot - // TODO: should return `artists` for gif? - pybind11::object plot; + pybind11::object plot(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // quiver - pybind11::object quiver; + pybind11::object quiver(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // savefig - pybind11::object savefig; + pybind11::object savefig(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // scatter - pybind11::object scatter; + pybind11::object scatter(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // show - pybind11::object show; + pybind11::object show(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // subplot - axes::Axes subplot(const pybind11::dict &kwargs); + axes::Axes subplot(const pybind11::dict &kwargs = pybind11::dict()); axes::Axes subplot(int cri); - pybind11::object subplot_attr; // subplots - std::tuple subplots(const pybind11::dict &kwargs); + std::tuple + subplots(const pybind11::dict &kwargs = pybind11::dict()); std::tuple> - subplots(int r, int c, const pybind11::dict &kwargs); - pybind11::object subplots_attr; + subplots(int r, int c, const pybind11::dict &kwargs = pybind11::dict()); // xlabel - pybind11::object xlabel; + pybind11::object xlabel(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // xlim - pybind11::object xlim; + pybind11::object xlim(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // ylabel - pybind11::object ylabel; + pybind11::object ylabel(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // ylim - pybind11::object ylim; + pybind11::object 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(figure, mod); + LOAD_FUNC_ATTR(gca, mod); + LOAD_FUNC_ATTR(gcf, 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(subplot, mod); + LOAD_FUNC_ATTR(subplots, 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 figure_attr; + pybind11::object gca_attr; + pybind11::object gcf_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 subplot_attr; + pybind11::object subplots_attr; + pybind11::object xlabel_attr; + pybind11::object xlim_attr; + pybind11::object ylabel_attr; + pybind11::object ylim_attr; }; // axes -axes::Axes PyPlot::axes(const pybind11::dict &kwargs = pybind11::dict()) { +axes::Axes PyPlot::axes(const pybind11::dict &kwargs) { pybind11::object ax_obj = axes_attr(**kwargs); return axes::Axes(ax_obj); } +// axis +pybind11::object PyPlot::axis(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = axis_attr(*args, **kwargs); + return ret; +} + +// cla +pybind11::object PyPlot::cla(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = cla_attr(*args, **kwargs); + return ret; +} + +// clf +pybind11::object PyPlot::clf(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = clf_attr(*args, **kwargs); + return ret; +} + // figure -figure::Figure PyPlot::figure(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()) { +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 -axes::Axes PyPlot::gca(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()) { +axes::Axes PyPlot::gca(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = gca_attr(*args, **kwargs); return axes::Axes(obj); } // gcf -figure::Figure PyPlot::gcf(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()) { +figure::Figure PyPlot::gcf(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = gcf_attr(*args, **kwargs); return figure::Figure(obj); } +// legend +pybind11::object PyPlot::legend(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = legend_attr(*args, **kwargs); + return ret; +} + +// pause +pybind11::object PyPlot::pause(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = pause_attr(*args, **kwargs); + return ret; +} + +// plot +pybind11::object PyPlot::plot(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = plot_attr(*args, **kwargs); + return ret; +} + +// quiver +pybind11::object PyPlot::quiver(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = quiver_attr(*args, **kwargs); + return ret; +} + +// savefig +pybind11::object PyPlot::savefig(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = savefig_attr(*args, **kwargs); + return ret; +} + +// show +pybind11::object PyPlot::show(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = show_attr(*args, **kwargs); + return ret; +} + // subplot -axes::Axes PyPlot::subplot(const pybind11::dict &kwargs = pybind11::dict()) { +axes::Axes PyPlot::subplot(const pybind11::dict &kwargs) { return axes::Axes(subplot_attr(**kwargs)); } @@ -152,7 +247,7 @@ axes::Axes PyPlot::subplot(int cri) { // subplots std::tuple -PyPlot::subplots(const pybind11::dict &kwargs = pybind11::dict()) { +PyPlot::subplots(const pybind11::dict &kwargs) { pybind11::list ret = subplots_attr(**kwargs); pybind11::object fig = ret[0]; pybind11::object ax = ret[1]; @@ -160,8 +255,7 @@ PyPlot::subplots(const pybind11::dict &kwargs = pybind11::dict()) { } std::tuple> -PyPlot::subplots(int r, int c, - const pybind11::dict &kwargs = pybind11::dict()) { +PyPlot::subplots(int r, int c, const pybind11::dict &kwargs) { pybind11::tuple args = pybind11::make_tuple(r, c); pybind11::list ret = subplots_attr(*args, **kwargs); std::vector axes; @@ -179,8 +273,35 @@ PyPlot::subplots(int r, int c, return {figure, axes}; } +// xlabel +pybind11::object PyPlot::xlabel(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = xlabel_attr(*args, **kwargs); + return ret; +} + +// xlim +pybind11::object PyPlot::xlim(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = xlim_attr(*args, **kwargs); + return ret; +} + +// ylabel +pybind11::object PyPlot::ylabel(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = ylabel_attr(*args, **kwargs); + return ret; +} + +// ylim +pybind11::object PyPlot::ylim(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = ylim_attr(*args, **kwargs); + return ret; +} + PyPlot import() { - // PyPlot singleton auto mod = pybind11::module::import("matplotlib.pyplot"); auto g_pyplot = PyPlot(mod); return g_pyplot; diff --git a/include/matplotlibcpp17/quiver.h b/include/matplotlibcpp17/quiver.h index 4be6534..516d13f 100644 --- a/include/matplotlibcpp17/quiver.h +++ b/include/matplotlibcpp17/quiver.h @@ -8,18 +8,22 @@ namespace matplotlibcpp17::quiver { struct DECL_STRUCT_ATTR Quiver { +public: Quiver(pybind11::object q) { self = q; } - pybind11::object self; pybind11::object unwrap() { return self; } + +private: + pybind11::object self; }; struct DECL_STRUCT_ATTR QuiverKey { QuiverKey(pybind11::object qk) { self = qk; } - pybind11::object self; - // for passing as python object pybind11::object unwrap() { return self; } + +private: + pybind11::object self; }; } // namespace matplotlibcpp17::quiver diff --git a/include/matplotlibcpp17/text.h b/include/matplotlibcpp17/text.h index 898f4ff..4bd6c8a 100644 --- a/include/matplotlibcpp17/text.h +++ b/include/matplotlibcpp17/text.h @@ -8,15 +8,28 @@ namespace matplotlibcpp17::text { struct DECL_STRUCT_ATTR Text { +public: Text(pybind11::object text) { self = text; load_attrs(); } + + pybind11::object + set_rotation(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); + +private: void load_attrs() { LOAD_FUNC_ATTR(set_rotation, self); } pybind11::object self; - pybind11::object set_rotation; + pybind11::object set_rotation_attr; }; +pybind11::object Text::set_rotation(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = set_rotation_attr(*args, **kwargs); + return ret; +} + } // namespace matplotlibcpp17::text #endif /* MATPLOTLIBCPP17_TEXT_H */ From e04f66bd824625369df141e9a3970f9c34b0e2ad Mon Sep 17 00:00:00 2001 From: soblin Date: Sat, 12 Feb 2022 19:31:55 +0900 Subject: [PATCH 4/8] migrated .cpp in images_contours_and_fields --- CMakeLists.txt | 2 +- gallery/images_contours_and_fields/quiver_demo.cpp | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d889a16..376199a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,7 @@ if(${ADD_DEMO}) # add_subdirectory(gallery/lines_bars_and_markers) # add_subdirectory(gallery/subplots_axes_and_figures) # add_subdirectory(gallery/statistics) - # add_subdirectory(gallery/images_contours_and_fields) + add_subdirectory(gallery/images_contours_and_fields) # add_subdirectory(gallery/shapes_and_collections) add_subdirectory(gallery/artist_animation) endif() diff --git a/gallery/images_contours_and_fields/quiver_demo.cpp b/gallery/images_contours_and_fields/quiver_demo.cpp index 7a29f96..831d40a 100644 --- a/gallery/images_contours_and_fields/quiver_demo.cpp +++ b/gallery/images_contours_and_fields/quiver_demo.cpp @@ -30,7 +30,7 @@ int main1() { [](double y) { return sin(y); }); auto plt = matplotlibcpp17::pyplot::import(); auto [fig1, ax1] = plt.subplots(); - ax1.set_title("Arrows scale with plot width, not view"); + ax1.set_title(args_("Arrows scale with plot width, not view")); auto Q = ax1.quiver(args_(X, Y, U, V), kwargs_("units"_a = "width")); auto qk = ax1.quiverkey(args_(Q.unwrap(), 0.9, 0.9, 2, R"($2 \frac{m}{s}$)"), @@ -38,7 +38,7 @@ int main1() { #if USE_GUI plt.show(); #else - plt.savefig("quiver_demo_1.png"); + plt.savefig(args_("quiver_demo_1.png")); #endif return 0; } @@ -58,7 +58,7 @@ int main2() { [](double y) { return sin(y); }); auto plt = matplotlibcpp17::pyplot::import(); auto [fig1, ax1] = plt.subplots(); - ax1.set_title("pivot='mid'; every third arrow; units='inches'"); + ax1.set_title(args_("pivot='mid'; every third arrow; units='inches'")); auto Q = ax1.quiver(args_(X, Y, U, V), kwargs_("pivot"_a = "mid", "units"_a = "inches")); auto qk = @@ -69,7 +69,7 @@ int main2() { #if USE_GUI plt.show(); #else - plt.savefig("quiver_demo_2.png"); + plt.savefig(args_("quiver_demo_2.png")); #endif return 0; } @@ -92,7 +92,7 @@ int main3() { } auto plt = matplotlibcpp17::pyplot::import(); auto [fig1, ax1] = plt.subplots(); - ax1.set_title("pivot='tip'; scales with x view"); + ax1.set_title(args_("pivot='tip'; scales with x view")); auto Q = ax1.quiver(args_(X, Y, U, V, M), kwargs_("units"_a = "x", "pivot"_a = "tip", "width"_a = 0.022, "scale"_a = 1.0 / 0.15)); @@ -104,7 +104,7 @@ int main3() { #if USE_GUI plt.show(); #else - plt.savefig("quiver_demo_3.png"); + plt.savefig(args_("quiver_demo_3.png")); #endif return 0; From d2cac9d26ce4da13b38220a69f3dd34d8981e7fe Mon Sep 17 00:00:00 2001 From: soblin Date: Sat, 12 Feb 2022 20:10:16 +0900 Subject: [PATCH 5/8] migrated .cpp in lines_bars_and_markers --- CMakeLists.txt | 2 +- .../lines_bars_and_markers/bar_label_demo.cpp | 42 +++++++------- gallery/lines_bars_and_markers/fill.cpp | 12 ++-- .../fill_between_demo.cpp | 56 +++++++++---------- .../fill_betweenx_demo.cpp | 18 +++--- .../lines_bars_and_markers/scatter_hist.cpp | 16 +++--- .../lines_bars_and_markers/scatter_symbol.cpp | 15 ++--- .../scatter_with_legend.cpp | 11 ++-- .../lines_bars_and_markers/simple_plot.cpp | 11 ++-- .../multiple_figs_demo.cpp | 2 +- include/matplotlibcpp17/axes.h | 7 +++ include/matplotlibcpp17/pyplot.h | 7 +++ 12 files changed, 107 insertions(+), 92 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 376199a..b9810ed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,7 @@ endfunction() if(${ADD_DEMO}) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_FLAGS "-Wall -g -DUSE_GUI=${USE_GUI}") - # add_subdirectory(gallery/lines_bars_and_markers) + add_subdirectory(gallery/lines_bars_and_markers) # add_subdirectory(gallery/subplots_axes_and_figures) # add_subdirectory(gallery/statistics) add_subdirectory(gallery/images_contours_and_fields) diff --git a/gallery/lines_bars_and_markers/bar_label_demo.cpp b/gallery/lines_bars_and_markers/bar_label_demo.cpp index 37652e2..67c668b 100644 --- a/gallery/lines_bars_and_markers/bar_label_demo.cpp +++ b/gallery/lines_bars_and_markers/bar_label_demo.cpp @@ -33,20 +33,20 @@ int main1() { auto p2 = ax.bar( args_(ind, womenMeans, width), kwargs_("bottom"_a = menMeans, "yerr"_a = womenStd, "label"_a = "Women")); - ax.axhline(0, "color"_a = "grey", "linewidth"_a = 0.8); - ax.set_ylabel("Scores"); - ax.set_title("Scores by group and gender"); - ax.set_xticks(ind, py::make_tuple("G1", "G2", "G3", "G4", "G5")); + ax.axhline(args_(0), kwargs_("color"_a = "grey", "linewidth"_a = 0.8)); + ax.set_ylabel(args_("Scores")); + ax.set_title(args_("Scores by group and gender")); + ax.set_xticks(args_(ind, py::make_tuple("G1", "G2", "G3", "G4", "G5"))); ax.legend(); // Label with label_type 'center' instead of the default 'edge' - ax.bar_label(p1.unwrap(), "label_type"_a = "center"); - ax.bar_label(p2.unwrap(), "label_type"_a = "center"); - ax.bar_label(p2.unwrap()); + ax.bar_label(args_(p1.unwrap()), kwargs_("label_type"_a = "center")); + ax.bar_label(args_(p2.unwrap()), kwargs_("label_type"_a = "center")); + ax.bar_label(args_(p2.unwrap())); #if USE_GUI plt.show(); #else - plt.savefig("bar_label_demo1.png"); + plt.savefig(args_("bar_label_demo1.png")); #endif return 0; } @@ -62,18 +62,18 @@ int main2() { auto [fig, ax] = plt.subplots(); auto hbars = ax.barh(args_(y_pos, performance), kwargs_("xerr"_a = error, "align"_a = "center")); - ax.set_yticks(y_pos, "labels"_a = people); + ax.set_yticks(args_(y_pos), kwargs_("labels"_a = people)); ax.invert_yaxis(); // labels read top-to-bottom - ax.set_xlabel("Performance"); - ax.set_title("How fast do you want to go today?"); + ax.set_xlabel(args_("Performance")); + ax.set_title(args_("How fast do you want to go today?")); // Label with specially formatted floats - ax.bar_label(hbars.unwrap(), "fmt"_a = "%.2f"); - ax.set_xlim("right"_a = 15); // adjust xlim to fit labels + ax.bar_label(args_(hbars.unwrap()), kwargs_("fmt"_a = "%.2f")); + ax.set_xlim(args_(), kwargs_("right"_a = 15)); // adjust xlim to fit labels #if USE_GUI plt.show(); #else - plt.savefig("bar_label_demo2.png"); + plt.savefig(args_("bar_label_demo2.png")); #endif return 0; } @@ -89,10 +89,10 @@ int main3() { auto [fig, ax] = plt.subplots(); auto hbars = ax.barh(args_(y_pos, performance), kwargs_("xerr"_a = error, "align"_a = "center")); - ax.set_yticks(y_pos, "labels"_a = people); + ax.set_yticks(args_(y_pos), kwargs_("labels"_a = people)); ax.invert_yaxis(); // labels read top-to-bottom - ax.set_xlabel("Performance"); - ax.set_title("How fast do you want to go today?"); + ax.set_xlabel(args_("Performance")); + ax.set_title(args_("How fast do you want to go today?")); // Label with specially formatted floats vector labels; @@ -101,13 +101,13 @@ int main3() { stm << std::fixed << std::setprecision(2) << e; return "±" + stm.str(); }); - ax.bar_label(hbars.unwrap(), "labels"_a = labels, "padding"_a = 8, - "color"_a = "b", "fontsize"_a = 14); - ax.set_xlim("right"_a = 15); // adjust xlim to fit labels + ax.bar_label(args_(hbars.unwrap()), kwargs_("labels"_a = labels, "padding"_a = 8, + "color"_a = "b", "fontsize"_a = 14)); + ax.set_xlim(args_(), kwargs_("right"_a = 15)); // adjust xlim to fit labels #if USE_GUI plt.show(); #else - plt.savefig("bar_label_demo3.png"); + plt.savefig(args_("bar_label_demo3.png")); #endif return 0; } diff --git a/gallery/lines_bars_and_markers/fill.cpp b/gallery/lines_bars_and_markers/fill.cpp index d431d48..da6af0e 100644 --- a/gallery/lines_bars_and_markers/fill.cpp +++ b/gallery/lines_bars_and_markers/fill.cpp @@ -36,15 +36,15 @@ int main() { kwargs_("figsize"_a = py::make_tuple(9, 3), "subplot_kw"_a = py::dict("aspect"_a = "equal"))); auto ax1 = axes[0], ax2 = axes[1], ax3 = axes[2]; - ax1.fill(x, y); - ax2.fill(x, y, "facecolor"_a = "lightsalmon", "edgecolor"_a = "orangered", - "linewidth"_a = 3); - ax3.fill(x, y, "facecolor"_a = "none", "edgecolor"_a = "purple", - "linewidth"_a = 3); + ax1.fill(args_(x, y)); + ax2.fill(args_(x, y), kwargs_("facecolor"_a = "lightsalmon", "edgecolor"_a = "orangered", + "linewidth"_a = 3)); + ax3.fill(args_(x, y), kwargs_("facecolor"_a = "none", "edgecolor"_a = "purple", + "linewidth"_a = 3)); #if USE_GUI plt.show(); #else - plt.savefig("fill.png"); + plt.savefig(args_("fill.png")); #endif return 0; } diff --git a/gallery/lines_bars_and_markers/fill_between_demo.cpp b/gallery/lines_bars_and_markers/fill_between_demo.cpp index 13abfa8..092f6f2 100644 --- a/gallery/lines_bars_and_markers/fill_between_demo.cpp +++ b/gallery/lines_bars_and_markers/fill_between_demo.cpp @@ -39,20 +39,20 @@ int main1() { auto [fig, axes] = plt.subplots( 3, 1, kwargs_("sharex"_a = true, "figsize"_a = py::make_tuple(6, 6))); auto ax1 = axes[0], ax2 = axes[1], ax3 = axes[2]; - ax1.fill_between(x, y1); - ax1.set_title("fill between y1 and 0"); + ax1.fill_between(args_(x, y1)); + ax1.set_title(args_("fill between y1 and 0")); - ax2.fill_between(x, y1, 1); - ax2.set_title("fill between y1 and 1"); + ax2.fill_between(args_(x, y1, 1)); + ax2.set_title(args_("fill between y1 and 1")); - ax3.fill_between(x, y1, y2); - ax3.set_title("fill between y1 and y2"); - ax3.set_xlabel("x"); + ax3.fill_between(args_(x, y1, y2)); + ax3.set_title(args_("fill between y1 and y2")); + ax3.set_xlabel(args_("x")); fig.tight_layout(); #if USE_GUI plt.show(); #else - plt.savefig("fill_between_demo_1.png"); + plt.savefig(args_("fill_between_demo_1.png")); #endif return 0; } @@ -66,30 +66,30 @@ int main2() { auto [fig, axs] = plt.subplots(2, 1, kwargs_("sharex"_a = true)); auto ax1 = axs[0], ax2 = axs[1]; - ax1.set_title("interpolation=False"); - ax1.plot(x, y1, "o--"); - ax1.plot(x, y2, "o--"); + ax1.set_title(args_("interpolation=False")); + ax1.plot(args_(x, y1, "o--")); + ax1.plot(args_(x, y2, "o--")); vector where = {true, true, false, false}; - ax1.fill_between(x, y1, y2, "where"_a = where, "color"_a = "C0", - "alpha"_a = 0.3); + ax1.fill_between(args_(x, y1, y2), kwargs_("where"_a = where, "color"_a = "C0", + "alpha"_a = 0.3)); where = {false, false, true, true}; - ax1.fill_between(x, y1, y2, "where"_a = where, "color"_a = "C1", - "alpha"_a = 0.3); + ax1.fill_between(args_(x, y1, y2), kwargs_("where"_a = where, "color"_a = "C1", + "alpha"_a = 0.3)); - ax2.set_title("interpolation=True"); - ax2.plot(x, y1, "o--"); - ax2.plot(x, y2, "o--"); + ax2.set_title(args_("interpolation=True")); + ax2.plot(args_(x, y1, "o--")); + ax2.plot(args_(x, y2, "o--")); where = {true, true, false, false}; - ax2.fill_between(x, y1, y2, "where"_a = where, "color"_a = "C0", - "alpha"_a = 0.3, "interpolate"_a = true); + ax2.fill_between(args_(x, y1, y2), kwargs_("where"_a = where, "color"_a = "C0", + "alpha"_a = 0.3, "interpolate"_a = true)); where = {false, false, true, true}; - ax2.fill_between(x, y1, y2, "where"_a = where, "color"_a = "C1", - "alpha"_a = 0.3, "interpolate"_a = true); + ax2.fill_between(args_(x, y1, y2), kwargs_("where"_a = where, "color"_a = "C1", + "alpha"_a = 0.3, "interpolate"_a = true)); fig.tight_layout(); #if USE_GUI plt.show(); #else - plt.savefig("fill_between_demo_2.png"); + plt.savefig(args_("fill_between_demo_2.png")); #endif return 0; } @@ -101,18 +101,18 @@ int main3() { vector y; transform(x.begin(), x.end(), back_inserter(y), [](double x) { return sin(x); }); - ax.plot(x, y, "color"_a = "black"); + ax.plot(args_(x, y), kwargs_("color"_a = "black")); double threshold = 0.75; - ax.axhline(threshold, "color"_a = "green", "lw"_a = 2, "alpha"_a = 0.7); + ax.axhline(args_(threshold), kwargs_("color"_a = "green", "lw"_a = 2, "alpha"_a = 0.7)); vector where; transform(y.begin(), y.end(), back_inserter(where), [&threshold](double y) { return y > threshold; }); - ax.fill_between(x, 0, 1, "where"_a = where, "color"_a = "green", - "alpha"_a = 0.5, "transform"_a = ax.get_xaxis_transform()); + ax.fill_between(args_(x, 0, 1), kwargs_("where"_a = where, "color"_a = "green", + "alpha"_a = 0.5, "transform"_a = ax.get_xaxis_transform())); #if USE_GUI plt.show(); #else - plt.savefig("fill_between_demo_3.png"); + plt.savefig(args_("fill_between_demo_3.png")); #endif return 0; } diff --git a/gallery/lines_bars_and_markers/fill_betweenx_demo.cpp b/gallery/lines_bars_and_markers/fill_betweenx_demo.cpp index eb111af..6c4b22d 100644 --- a/gallery/lines_bars_and_markers/fill_betweenx_demo.cpp +++ b/gallery/lines_bars_and_markers/fill_betweenx_demo.cpp @@ -39,20 +39,20 @@ int main1() { 1, 3, kwargs_("sharey"_a = true, "figsize"_a = py::make_tuple(6, 6))); auto ax1 = axes[0], ax2 = axes[1], ax3 = axes[2]; - ax1.fill_betweenx(y, 0, x1); - ax1.set_title("between (x1, 0)"); + ax1.fill_betweenx(args_(y, 0, x1)); + ax1.set_title(args_("between (x1, 0)")); - ax2.fill_betweenx(y, x1, 1); - ax2.set_title("between (x1, 1)"); - ax2.set_xlabel("x"); - - ax3.fill_betweenx(y, x1, x2); - ax3.set_title("between (x1, x2)"); + ax2.fill_betweenx(args_(y, x1, 1)); + ax2.set_title(args_("between (x1, 1)")); + ax2.set_xlabel(args_("x")); + + ax3.fill_betweenx(args_(y, x1, x2)); + ax3.set_title(args_("between (x1, x2)")); #if USE_GUI plt.show(); #else - plt.savefig("fill_betweenx_demo.png"); + plt.savefig(args_("fill_betweenx_demo.png")); #endif return 0; } diff --git a/gallery/lines_bars_and_markers/scatter_hist.cpp b/gallery/lines_bars_and_markers/scatter_hist.cpp index 817a4d8..2393f5c 100644 --- a/gallery/lines_bars_and_markers/scatter_hist.cpp +++ b/gallery/lines_bars_and_markers/scatter_hist.cpp @@ -48,14 +48,14 @@ int main() { 0.2}; const vector rect_histy = {left + width + spacing, bottom, 0.2, height}; - auto fig = plt.figure(kwargs_("figsize"_a = py::make_tuple(8, 8))); + auto fig = plt.figure(args_(), kwargs_("figsize"_a = py::make_tuple(8, 8))); auto ax = fig.add_axes(args_(rect_scatter)); auto ax_histx = fig.add_axes(args_(rect_histx), kwargs_("sharex"_a = ax.unwrap())); auto ax_histy = fig.add_axes(args_(rect_histy), kwargs_("sharey"_a = ax.unwrap())); - ax_histx.tick_params("axis"_a = "x", "labelbottom"_a = false); - ax_histy.tick_params("axis"_a = "y", "labelleft"_a = false); + ax_histx.tick_params(args_(), kwargs_("axis"_a = "x", "labelbottom"_a = false)); + ax_histy.tick_params(args_(), kwargs_("axis"_a = "y", "labelleft"_a = false)); ax.scatter(args_(x, y)); const double binwidth = 0.25; auto abx_max = [](const vector &v) -> double { @@ -73,12 +73,12 @@ int main() { #if USE_GUI plt.show(); #else - plt.savefig("scatter_hist1.png"); + plt.savefig(args_("scatter_hist1.png")); #endif } // cell2 { - auto fig = plt.figure(kwargs_("figsize"_a = py::make_tuple(8, 8))); + auto fig = plt.figure(args_(), kwargs_("figsize"_a = py::make_tuple(8, 8))); auto gs = fig.add_gridspec(2, 2, kwargs_("width_ratios"_a = py::make_tuple(7, 2), "height_ratios"_a = py::make_tuple(2, 7), @@ -90,8 +90,8 @@ int main() { kwargs_("sharex"_a = ax.unwrap())); auto ax_histy = fig.add_subplot(args_(gs(1, 1).unwrap()), kwargs_("sharey"_a = ax.unwrap())); - ax_histx.tick_params("axis"_a = "x", "labelbottom"_a = false); - ax_histy.tick_params("axis"_a = "y", "labelleft"_a = false); + ax_histx.tick_params(args_(), kwargs_("axis"_a = "x", "labelbottom"_a = false)); + ax_histy.tick_params(args_(), kwargs_("axis"_a = "y", "labelleft"_a = false)); ax.scatter(args_(x, y)); const double binwidth = 0.25; auto abx_max = [](const vector &v) -> double { @@ -109,7 +109,7 @@ int main() { #if USE_GUI plt.show(); #else - plt.savefig("scatter_hist2.png"); + plt.savefig(args_("scatter_hist2.png")); #endif } return 0; diff --git a/gallery/lines_bars_and_markers/scatter_symbol.cpp b/gallery/lines_bars_and_markers/scatter_symbol.cpp index d93f3bc..2d1b344 100644 --- a/gallery/lines_bars_and_markers/scatter_symbol.cpp +++ b/gallery/lines_bars_and_markers/scatter_symbol.cpp @@ -25,10 +25,11 @@ template std::vector arange(T start, T end, T h) { } using namespace std; +using namespace matplotlibcpp17; int main() { py::scoped_interpreter guard{}; - auto plt = matplotlibcpp17::pyplot::import(); + auto plt = pyplot::import(); vector x = {0., 2., 4., 6., 8., 10., 12., 14., 16., 18., 20., 22., 24., 26., 28., 30., 32., 34., 36., 38., 40., 42., 44., 46., 48.}; @@ -44,15 +45,15 @@ int main() { 1126.45270023, 1278.31780809, 886.56768427, 769.13688434, 953.93522899, 538.35320778, 811.14962318, 1225.04291605, 628.81456741, 1094.89690779, 1006.37932941, 759.34401418, 1237.90122592, 689.78115093, 1159.15645671}; - plt.scatter(x, y, s, "c"_a = "g", "alpha"_a = 0.5, - "marker"_a = R"($\clubsuit$)", "label"_a = "Luck"); - plt.xlabel("Leprechauns"); - plt.ylabel("Gold"); - plt.legend("loc"_a = "upper left"); + plt.scatter(args_(x, y, s), kwargs_("c"_a = "g", "alpha"_a = 0.5, + "marker"_a = R"($\clubsuit$)", "label"_a = "Luck")); + plt.xlabel(args_("Leprechauns")); + plt.ylabel(args_("Gold")); + plt.legend(kwargs_("loc"_a = "upper left")); #if USE_GUI plt.show(); #else - plt.savefig("scatter_symbol.png"); + plt.savefig(args_("scatter_symbol.png")); #endif return 0; } diff --git a/gallery/lines_bars_and_markers/scatter_with_legend.cpp b/gallery/lines_bars_and_markers/scatter_with_legend.cpp index ec4301b..b5c7a10 100644 --- a/gallery/lines_bars_and_markers/scatter_with_legend.cpp +++ b/gallery/lines_bars_and_markers/scatter_with_legend.cpp @@ -34,11 +34,11 @@ int main1() { "alpha"_a = 0.3, "edgecolors"_a = "none")); } ax.legend(); - ax.grid(true); + ax.grid(args_(true)); #if USE_GUI plt.show(); #else - plt.savefig("scatter_with_legend1.png"); + plt.savefig(args_("scatter_with_legend1.png")); #endif return 0; } @@ -76,11 +76,10 @@ int main2() { auto legend1 = ax.legend(args_(handles, labels), kwargs_("loc"_a = "lower left", "title"_a = "Classes")); - ax.add_artist(legend1.unwrap()); + ax.add_artist(args_(legend1.unwrap())); } { - auto [handles, labels] = scatter.legend_elements( - py::tuple(), kwargs_("prop"_a = "sizes", "alpha"_a = 0.6)); + auto [handles, labels] = scatter.legend_elements(args_(), kwargs_("prop"_a = "sizes", "alpha"_a = 0.6)); auto legend2 = ax.legend(args_(handles, labels), kwargs_("loc"_a = "upper right", "title"_a = "Sizes")); @@ -88,7 +87,7 @@ int main2() { #if USE_GUI plt.show(); #else - plt.savefig("scatter_with_legend2.png"); + plt.savefig(args_("scatter_with_legend2.png")); #endif return 0; } diff --git a/gallery/lines_bars_and_markers/simple_plot.cpp b/gallery/lines_bars_and_markers/simple_plot.cpp index e636335..0ae25d3 100644 --- a/gallery/lines_bars_and_markers/simple_plot.cpp +++ b/gallery/lines_bars_and_markers/simple_plot.cpp @@ -25,25 +25,26 @@ template std::vector arange(T start, T end, T h) { } using namespace std; +using namespace matplotlibcpp17; int main() { py::scoped_interpreter guard{}; - auto plt = matplotlibcpp17::pyplot::import(); + auto plt = pyplot::import(); vector t = arange(0.0, 2.0, 0.01); decltype(t) s; transform(t.begin(), t.end(), back_inserter(s), [](double x) { return 1.0 + sin(2 * M_PI * x); }); auto [fig, ax] = plt.subplots(); - ax.plot(t, s, "color"_a = "blue", "linewidth"_a = 1.0); - ax.set("xlabel"_a = "time (s)", "ylabel"_a = "voltage (mV)", - "title"_a = "About as simple as it gets, folks"); + ax.plot(args_(t, s), kwargs_("color"_a = "blue", "linewidth"_a = 1.0)); + ax.set(args_(), kwargs_("xlabel"_a = "time (s)", "ylabel"_a = "voltage (mV)", + "title"_a = "About as simple as it gets, folks")); ax.grid(); #if USE_GUI plt.show(); #else - plt.savefig("simple_plot.png"); + plt.savefig(args_("simple_plot.png")); #endif return 0; } diff --git a/gallery/subplots_axes_and_figures/multiple_figs_demo.cpp b/gallery/subplots_axes_and_figures/multiple_figs_demo.cpp index b00081a..ae1b0e3 100644 --- a/gallery/subplots_axes_and_figures/multiple_figs_demo.cpp +++ b/gallery/subplots_axes_and_figures/multiple_figs_demo.cpp @@ -57,7 +57,7 @@ int main() { plt.subplot(211); plt.plot(t, s2, "s"); auto ax = plt.gca(); - ax.set_xticks(py::tuple()); + ax.set_xticks(args_()); #if USE_GUI plt.show(); #else diff --git a/include/matplotlibcpp17/axes.h b/include/matplotlibcpp17/axes.h index 3630957..3fa31ac 100644 --- a/include/matplotlibcpp17/axes.h +++ b/include/matplotlibcpp17/axes.h @@ -328,6 +328,13 @@ pybind11::object Axes::get_lines(const pybind11::tuple &args, return ret; } +// get_xaxis_transform +pybind11::object Axes::get_xaxis_transform(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = get_xaxis_transform_attr(*args, **kwargs); + return ret; +} + // get_xlim std::tuple Axes::get_xlim() { pybind11::list ret = get_xlim_attr(); diff --git a/include/matplotlibcpp17/pyplot.h b/include/matplotlibcpp17/pyplot.h index 01322f0..cdc439e 100644 --- a/include/matplotlibcpp17/pyplot.h +++ b/include/matplotlibcpp17/pyplot.h @@ -221,6 +221,13 @@ pybind11::object PyPlot::quiver(const pybind11::tuple &args, return ret; } +// scatter +pybind11::object PyPlot::scatter(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = scatter_attr(*args, **kwargs); + return ret; +} + // savefig pybind11::object PyPlot::savefig(const pybind11::tuple &args, const pybind11::dict &kwargs) { From cac4001048b74f01eb46cd73544ba8441254793f Mon Sep 17 00:00:00 2001 From: soblin Date: Sat, 12 Feb 2022 20:16:41 +0900 Subject: [PATCH 6/8] migrated .cpp in shapes_and_collections --- CMakeLists.txt | 2 +- gallery/shapes_and_collections/patch_collection.cpp | 8 ++++---- .../patches_circle_rectangle.cpp | 12 ++++++------ include/matplotlibcpp17/pyplot.h | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b9810ed..465c57b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,7 @@ if(${ADD_DEMO}) # add_subdirectory(gallery/subplots_axes_and_figures) # add_subdirectory(gallery/statistics) add_subdirectory(gallery/images_contours_and_fields) - # add_subdirectory(gallery/shapes_and_collections) + add_subdirectory(gallery/shapes_and_collections) add_subdirectory(gallery/artist_animation) endif() diff --git a/gallery/shapes_and_collections/patch_collection.cpp b/gallery/shapes_and_collections/patch_collection.cpp index 623fd1e..bc76576 100644 --- a/gallery/shapes_and_collections/patch_collection.cpp +++ b/gallery/shapes_and_collections/patch_collection.cpp @@ -59,13 +59,13 @@ int main() { py::list colors = py::cast(colors_); auto p = collections::PatchCollection(args_(patches), kwargs_("alpha"_a = 0.4)); - p.set_array(colors); - ax.add_collection(p.unwrap()); - fig.colorbar(p.unwrap(), "ax"_a = ax.unwrap()); + p.set_array(args_(colors)); + ax.add_collection(args_(p.unwrap())); + fig.colorbar(args_(p.unwrap()), kwargs_("ax"_a = ax.unwrap())); #if USE_GUI plt.show(); #else - plt.savefig("patch_collection.png"); + plt.savefig(args_("patch_collection.png")); #endif return 0; } diff --git a/gallery/shapes_and_collections/patches_circle_rectangle.cpp b/gallery/shapes_and_collections/patches_circle_rectangle.cpp index 65acdaa..115600d 100644 --- a/gallery/shapes_and_collections/patches_circle_rectangle.cpp +++ b/gallery/shapes_and_collections/patches_circle_rectangle.cpp @@ -26,14 +26,14 @@ int main() { kwargs_("fc"_a = "b", "ec"_a = "y")); auto r = Rectangle(args_(py::make_tuple(0, 0), 0.25, 0.5), kwargs_("ec"_a = "#000000", "fill"_a = false)); - ax.add_patch(c.unwrap()); - ax.add_patch(e.unwrap()); - ax.add_patch(r.unwrap()); - plt.axis("scaled"); - ax.set_aspect("equal"); + ax.add_patch(args_(c.unwrap())); + ax.add_patch(args_(e.unwrap())); + ax.add_patch(args_(r.unwrap())); + plt.axis(args_("scaled")); + ax.set_aspect(args_("equal")); #if USE_GUI plt.show(); #else - plt.savefig("patches_circle_rectangle.png"); + plt.savefig(args_("patches_circle_rectangle.png")); #endif } diff --git a/include/matplotlibcpp17/pyplot.h b/include/matplotlibcpp17/pyplot.h index cdc439e..b371e32 100644 --- a/include/matplotlibcpp17/pyplot.h +++ b/include/matplotlibcpp17/pyplot.h @@ -19,7 +19,7 @@ struct DECL_STRUCT_ATTR PyPlot { load_attrs(); } // axes - axes::Axes axes(const pybind11::dict &kwargs); + axes::Axes axes(const pybind11::dict &kwargs = pybind11::dict()); // axis pybind11::object axis(const pybind11::tuple &args = pybind11::tuple(), From b1536f73630fac0f4601ed01a5620352a45ea123 Mon Sep 17 00:00:00 2001 From: soblin Date: Sat, 12 Feb 2022 20:43:35 +0900 Subject: [PATCH 7/8] migrated .cpp in subplots_axes_and_figures --- CMakeLists.txt | 2 +- .../align_labels_demo.cpp | 18 ++++++------- .../gridspec_multicolumn.cpp | 27 ++++++++++--------- .../multiple_figs_demo.cpp | 16 +++++------ include/matplotlibcpp17/axes.h | 14 ++++++++++ 5 files changed, 46 insertions(+), 31 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 465c57b..4d4e55c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,7 +25,7 @@ if(${ADD_DEMO}) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_FLAGS "-Wall -g -DUSE_GUI=${USE_GUI}") add_subdirectory(gallery/lines_bars_and_markers) - # add_subdirectory(gallery/subplots_axes_and_figures) + add_subdirectory(gallery/subplots_axes_and_figures) # add_subdirectory(gallery/statistics) add_subdirectory(gallery/images_contours_and_fields) add_subdirectory(gallery/shapes_and_collections) diff --git a/gallery/subplots_axes_and_figures/align_labels_demo.cpp b/gallery/subplots_axes_and_figures/align_labels_demo.cpp index cdbf065..41d9513 100644 --- a/gallery/subplots_axes_and_figures/align_labels_demo.cpp +++ b/gallery/subplots_axes_and_figures/align_labels_demo.cpp @@ -32,31 +32,31 @@ using namespace matplotlibcpp17; int main() { py::scoped_interpreter guard{}; auto plt = matplotlibcpp17::pyplot::import(); - auto fig = plt.figure(kwargs_("tight_layout"_a = true)); + auto fig = plt.figure(args_(), kwargs_("tight_layout"_a = true)); auto gs = GridSpec(2, 2); // instead of gs[0, :] auto ax = fig.add_subplot(args_(gs(0, py::slice(0, 2, 1)).unwrap())); - ax.plot(arange(0, 1000000, 10000)); - ax.set_ylabel("YLabel0"); - ax.set_xlabel("XLabel0"); + ax.plot(args_(arange(0, 1000000, 10000))); + ax.set_ylabel(args_("YLabel0")); + ax.set_xlabel(args_("XLabel0")); for (auto i : {0, 1}) { ax = fig.add_subplot(args_(gs(1, i).unwrap())); auto ys = arange(1.0, 0.0, -0.1); decltype(ys) xs; transform(ys.begin(), ys.end(), back_inserter(xs), [](double x) { return x * 2000; }); - ax.plot(xs, ys); - ax.set_ylabel(string("YLabel1 " + to_string(i))); - ax.set_xlabel(string("XLabel1 " + to_string(i))); + ax.plot(args_(xs, ys)); + ax.set_ylabel(args_(string("YLabel1 " + to_string(i)))); + ax.set_xlabel(args_(string("XLabel1 " + to_string(i)))); if (i == 0) { for (auto &&tick : ax.get_xticklabels()) - tick.set_rotation(55); + tick.set_rotation(args_(55)); } } fig.align_labels(); #if USE_GUI plt.show(); #else - plt.savefig("align_labels_demo.png"); + plt.savefig(args_("align_labels_demo.png")); #endif } diff --git a/gallery/subplots_axes_and_figures/gridspec_multicolumn.cpp b/gallery/subplots_axes_and_figures/gridspec_multicolumn.cpp index 76ce6fd..30461e5 100644 --- a/gallery/subplots_axes_and_figures/gridspec_multicolumn.cpp +++ b/gallery/subplots_axes_and_figures/gridspec_multicolumn.cpp @@ -20,28 +20,29 @@ int main() { py::scoped_interpreter guard{}; auto plt = matplotlibcpp17::pyplot::import(); - auto fig = plt.figure(kwargs_("constrained_layout"_a = true)); + auto fig = plt.figure(args_(), kwargs_("constrained_layout"_a = true)); auto gs = GridSpec(3, 3, kwargs_("figure"_a = fig.unwrap())); - auto ax1 = fig.add_subplot(args_(gs(0, py::slice(0, 3, 1)).unwrap())); - auto ax2 = fig.add_subplot(args_(gs(1, py::slice(0, 2, 1)).unwrap())); - auto ax3 = fig.add_subplot(args_(gs(py::slice(1, 3, 1), 2).unwrap())); - auto ax4 = fig.add_subplot(args_(gs(2, 0).unwrap())); - auto ax5 = fig.add_subplot(args_(gs(2, 1).unwrap())); - - fig.suptitle("GridSpec"); + vector axs; + axs.push_back(fig.add_subplot(args_(gs(0, py::slice(0, 3, 1)).unwrap()))); + axs.push_back(fig.add_subplot(args_(gs(1, py::slice(0, 2, 1)).unwrap()))); + axs.push_back(fig.add_subplot(args_(gs(py::slice(1, 3, 1), 2).unwrap()))); + axs.push_back(fig.add_subplot(args_(gs(2, 0).unwrap()))); + axs.push_back(fig.add_subplot(args_(gs(2, 1).unwrap()))); + fig.suptitle(args_("GridSpec")); int cnt = 1; - for (const auto &ax : {ax1, ax2, ax3, ax4, ax5}) { - ax.text(0.5, 0.5, string("ax" + to_string(cnt)), "va"_a = "center", - "ha"_a = "center"); - ax.tick_params("labelbottom"_a = false, "labelleft"_a = false); + for (auto &ax : axs) { + ax.text(args_(0.5, 0.5, string("ax" + to_string(cnt))), + kwargs_("va"_a = "center", "ha"_a = "center")); + ax.tick_params(args_(), + kwargs_("labelbottom"_a = false, "labelleft"_a = false)); cnt++; } #if USE_GUI plt.show(); #else - plt.savefig("gridspec_multicolumn.png"); + plt.savefig(args_("gridspec_multicolumn.png)"); #endif } diff --git a/gallery/subplots_axes_and_figures/multiple_figs_demo.cpp b/gallery/subplots_axes_and_figures/multiple_figs_demo.cpp index ae1b0e3..774d4b1 100644 --- a/gallery/subplots_axes_and_figures/multiple_figs_demo.cpp +++ b/gallery/subplots_axes_and_figures/multiple_figs_demo.cpp @@ -38,30 +38,30 @@ int main() { [](double x) { return sin(4 * M_PI * x); }); plt.figure(args_(1)); plt.subplot(211); - plt.plot(t, s1); + plt.plot(args_(t, s1)); plt.subplot(212); - plt.plot(t, s2); + plt.plot(args_(t, s2)); #if USE_GUI plt.show(); #else - plt.savefig("multiple_figs_demo1.png"); + plt.savefig(args_("multiple_figs_demo1.png")); #endif plt.figure(args_(2)); - plt.plot(t, s2); + plt.plot(args_(t, s2)); #if USE_GUI plt.show(); #else - plt.savefig("multiple_figs_demo2.png"); + plt.savefig(args_("multiple_figs_demo2.png")); #endif plt.figure(args_(2)); plt.subplot(211); - plt.plot(t, s2, "s"); + plt.plot(args_(t, s2, "s")); auto ax = plt.gca(); - ax.set_xticks(args_()); + ax.set_xticklabels(args_(py::list())); #if USE_GUI plt.show(); #else - plt.savefig("multiple_figs_demo3.png"); + plt.savefig(args_("multiple_figs_demo3.png")); #endif return 0; } diff --git a/include/matplotlibcpp17/axes.h b/include/matplotlibcpp17/axes.h index 3fa31ac..e30956b 100644 --- a/include/matplotlibcpp17/axes.h +++ b/include/matplotlibcpp17/axes.h @@ -157,6 +157,11 @@ struct DECL_STRUCT_ATTR Axes { pybind11::object set_xticks(const pybind11::tuple &args = pybind11::tuple(), const pybind11::dict &kwargs = pybind11::dict()); + // set_xticklabels + pybind11::object + set_xticklabels(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); + // set_ylabel pybind11::object set_ylabel(const pybind11::tuple &args = pybind11::tuple(), const pybind11::dict &kwargs = pybind11::dict()); @@ -208,6 +213,7 @@ struct DECL_STRUCT_ATTR Axes { LOAD_FUNC_ATTR(set_xlabel, self); LOAD_FUNC_ATTR(set_xlim, self); LOAD_FUNC_ATTR(set_xticks, self); + LOAD_FUNC_ATTR(set_xticklabels, self); LOAD_FUNC_ATTR(set_ylabel, self); LOAD_FUNC_ATTR(set_ylim, self); LOAD_FUNC_ATTR(set_yticks, self); @@ -244,6 +250,7 @@ struct DECL_STRUCT_ATTR Axes { pybind11::object set_xlabel_attr; pybind11::object set_xlim_attr; pybind11::object set_xticks_attr; + pybind11::object set_xticklabels_attr; pybind11::object set_ylabel_attr; pybind11::object set_ylim_attr; pybind11::object set_yticks_attr; @@ -468,6 +475,13 @@ pybind11::object Axes::set_xticks(const pybind11::tuple &args, return ret; } +// set_xticklabels +pybind11::object Axes::set_xticklabels(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = set_xticklabels_attr(*args, **kwargs); + return ret; +} + // set_ylabel pybind11::object Axes::set_ylabel(const pybind11::tuple &args, const pybind11::dict &kwargs) { From 58745c874e4b58d018fa04aac63208417644e445 Mon Sep 17 00:00:00 2001 From: soblin Date: Sat, 12 Feb 2022 20:55:47 +0900 Subject: [PATCH 8/8] finished --- CMakeLists.txt | 4 ++-- README.md | 33 ++++++++++++++---------------- gallery/statistics/hist.cpp | 6 +++--- tests/test_shared_lib/test_lib.cpp | 9 ++++---- tests/test_static_lib/test_lib.cpp | 9 ++++---- 5 files changed, 30 insertions(+), 31 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d4e55c..9da12aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,7 +26,7 @@ if(${ADD_DEMO}) set(CMAKE_CXX_FLAGS "-Wall -g -DUSE_GUI=${USE_GUI}") add_subdirectory(gallery/lines_bars_and_markers) add_subdirectory(gallery/subplots_axes_and_figures) - # add_subdirectory(gallery/statistics) + add_subdirectory(gallery/statistics) add_subdirectory(gallery/images_contours_and_fields) add_subdirectory(gallery/shapes_and_collections) add_subdirectory(gallery/artist_animation) @@ -37,5 +37,5 @@ if(NOT DEFINED DO_TEST) endif() if(${DO_TEST}) - # add_subdirectory(tests) + add_subdirectory(tests) endif() diff --git a/README.md b/README.md index 7154e47..05facdb 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,9 @@ Just add include path to `include` directory of this project. ## Syntax -- `void` functions can be called in almost the same way as python code (remind yourself to append `_a` literal to keyword arguments). -- For `non-void` functions that return some objects, basically the user will need to capsulate *arguments* in `args_(arg1, arg2, ...) == pybind11:tuple` and *keyword arguments* in `kwargs_("k1"_a = v1, "k2"_a = v2, ...) == pybind11::dict`. The returned value is a corresponding wrapper class. Please refer to the examples below. - - exception: `subplots`, - - conversion: Wrapper class of matplotlibcpp17 like [::container::BarContainer](https://github.com/soblin/matplotlibcpp17/blob/master/include/matplotlibcpp17/container.h) needs to be passed to python interpreter using `unwrap()` method in *args* and *kwargs*. +The user will need to capsulate *arguments* in `args_(arg1, arg2, ...) == pybind11:tuple` and *keyword arguments* in `kwargs_("k1"_a = v1, "k2"_a = v2, ...) == pybind11::dict`. The returned value is a corresponding wrapper class. Please refer to the reference and examples below. +- exception: `subplots`, `TBD`s +- conversion: Wrapper class of matplotlibcpp17 like [::container::BarContainer](https://github.com/soblin/matplotlibcpp17/blob/master/include/matplotlibcpp17/container.h) needs to be passed to python interpreter using `unwrap()` method in *args* and *kwargs*. ## Examples @@ -46,14 +45,12 @@ From [gallery/subplots_axes_and_figures/align_labels_demo.cpp](https://github.co /// gs is of type gridspec::GridSpec auto gs = GridSpec(2, 2); - // non-void function: capsulate args and kwargs in py::tuple and py::dict /// pass wrapper class object like gs[0, :] of ::gridspec::SubplotSpec to the interpreter using .unwrap() method as python object auto ax = fig.add_subplot(args_(gs(0, py::slice(0, 2, 1)).unwrap())); - // void function: no need to capsulate args and kwargs - ax.plot(arange(0, 1000000, 10000)); - ax.set_ylabel("YLabel0"); - ax.set_xlabel("XLabel0"); + ax.plot(args_(arange(0, 1000000, 10000))); + ax.set_ylabel(args_("YLabel0")); + ax.set_xlabel(args_("XLabel0")); ``` ![subplots_axes_and_figures](./gallery/images/align_labels_demo.png) @@ -66,22 +63,22 @@ From [gallery/lines_bars_and_markers/bar_label_demo.cpp](https://github.com/sobl ```cpp auto [fig, ax] = plt.subplots(); - // non-void function: capsulate args and kwargs in py::tuple and py::dict auto p1 = ax.bar(args_(ind, menMeans, width), kwargs_("yerr"_a = menStd, "label"_a = "Men")); auto p2 = ax.bar( args_(ind, womenMeans, width), kwargs_("bottom"_a = menMeans, "yerr"_a = womenStd, "label"_a = "Women")); - ax.axhline(0, "color"_a = "grey", "linewidth"_a = 0.8); - ax.set_ylabel("Scores"); - ax.set_title("Scores by group and gender"); - ax.set_xticks(ind, py::make_tuple("G1", "G2", "G3", "G4", "G5")); + ax.axhline(args_(0), kwargs_("color"_a = "grey", "linewidth"_a = 0.8)); + ax.set_ylabel(args_("Scores")); + ax.set_title(args_("Scores by group and gender")); + + ax.set_xticks(args_(ind, py::make_tuple("G1", "G2", "G3", "G4", "G5"))); ax.legend(); // pass wrapper class object like p1 of ::container::BarContainer to the interpreter using .unwrap() method as python object - ax.bar_label(p1.unwrap(), "label_type"_a = "center"); - ax.bar_label(p2.unwrap(), "label_type"_a = "center"); - ax.bar_label(p2.unwrap()); + ax.bar_label(args_(p1.unwrap()), kwargs_("label_type"_a = "center")); + ax.bar_label(args_(p2.unwrap()), kwargs_("label_type"_a = "center")); + ax.bar_label(args_(p2.unwrap())); plt.show(); ``` @@ -116,7 +113,7 @@ From [gallery/images_contours_and_fields/quiver_demo.cpp](https://github.com/sob ```cpp auto plt = matplotlibcpp17::pyplot::import(); auto [fig1, ax1] = plt.subplots(); - ax1.set_title("pivot='tip'; scales with x view"); + ax1.set_title(args_("Arrows scale with plot width, not view")); auto Q = ax1.quiver(args_(X, Y, U, V, M), kwargs_("units"_a = "x", "pivot"_a = "tip", "width"_a = 0.022, "scale"_a = 1.0 / 0.15)); diff --git a/gallery/statistics/hist.cpp b/gallery/statistics/hist.cpp index 09e45e0..96925f8 100644 --- a/gallery/statistics/hist.cpp +++ b/gallery/statistics/hist.cpp @@ -33,7 +33,7 @@ int main1() { #if USE_GUI plt.show(); #else - plt.savefig("hist1.png"); + plt.savefig(args_("hist1.png")); #endif return 0; } @@ -59,12 +59,12 @@ int main3() { auto ax1 = axs[0]; ; // We can increase the number of bins on each axis - ax1.hist2d(dist1, dist2, "bins"_a = 40); + ax1.hist2d(args_(dist1, dist2), kwargs_("bins"_a = 40)); // TODO: more examples #if USE_GUI plt.show(); #else - plt.savefig("hist3.png"); + plt.savefig(args_("hist3.png")); #endif return 0; } diff --git a/tests/test_shared_lib/test_lib.cpp b/tests/test_shared_lib/test_lib.cpp index 3d766f9..612f7a2 100644 --- a/tests/test_shared_lib/test_lib.cpp +++ b/tests/test_shared_lib/test_lib.cpp @@ -23,6 +23,7 @@ template std::vector arange(T start, T end, T h) { } using namespace std; +using namespace matplotlibcpp17; void func() { auto plt = matplotlibcpp17::pyplot::import(); @@ -32,11 +33,11 @@ void func() { [](double x) { return 1.0 + sin(2 * M_PI * x); }); auto [fig, ax] = plt.subplots(); - ax.plot(t, s, "color"_a = "blue", "linewidth"_a = 1.0); - ax.set("xlabel"_a = "time (s)", "ylabel"_a = "voltage (mV)", - "title"_a = "About as simple as it gets, folks"); + ax.plot(args_(t, s), kwargs_("color"_a = "blue", "linewidth"_a = 1.0)); + ax.set(args_(), kwargs_("xlabel"_a = "time (s)", "ylabel"_a = "voltage (mV)", + "title"_a = "About as simple as it gets, folks")); ax.grid(); - fig.savefig("test.png"); + fig.savefig(args_("test.png")); plt.show(); } diff --git a/tests/test_static_lib/test_lib.cpp b/tests/test_static_lib/test_lib.cpp index 3d766f9..612f7a2 100644 --- a/tests/test_static_lib/test_lib.cpp +++ b/tests/test_static_lib/test_lib.cpp @@ -23,6 +23,7 @@ template std::vector arange(T start, T end, T h) { } using namespace std; +using namespace matplotlibcpp17; void func() { auto plt = matplotlibcpp17::pyplot::import(); @@ -32,11 +33,11 @@ void func() { [](double x) { return 1.0 + sin(2 * M_PI * x); }); auto [fig, ax] = plt.subplots(); - ax.plot(t, s, "color"_a = "blue", "linewidth"_a = 1.0); - ax.set("xlabel"_a = "time (s)", "ylabel"_a = "voltage (mV)", - "title"_a = "About as simple as it gets, folks"); + ax.plot(args_(t, s), kwargs_("color"_a = "blue", "linewidth"_a = 1.0)); + ax.set(args_(), kwargs_("xlabel"_a = "time (s)", "ylabel"_a = "voltage (mV)", + "title"_a = "About as simple as it gets, folks")); ax.grid(); - fig.savefig("test.png"); + fig.savefig(args_("test.png")); plt.show(); }