From 85ba09b9584d2be465b599bcefbc1b88fe5fc62f Mon Sep 17 00:00:00 2001 From: Mamoru Sobue Date: Sun, 23 Oct 2022 08:12:55 +0900 Subject: [PATCH] Feat/return proxy (#27) * (1) return ObjectWrapper (2) workaround for mplot3d Signed-off-by: soblin * checked all samples working Signed-off-by: soblin * adding const Signed-off-by: soblin * adding const Signed-off-by: soblin Signed-off-by: soblin --- gallery/artist_animation/animate_decay.cpp | 5 +- gallery/artist_animation/random_walk.cpp | 12 +- .../contourf_log.cpp | 18 +- .../images_contours_and_fields/image_demo.cpp | 10 +- .../quiver_demo.cpp | 26 +- .../lines_bars_and_markers/bar_label_demo.cpp | 34 +- .../errorbar_limits_simple.cpp | 10 +- .../errorbar_subsample.cpp | 15 +- gallery/lines_bars_and_markers/fill.cpp | 8 +- .../fill_between_demo.cpp | 20 +- .../fill_betweenx_demo.cpp | 8 +- .../lines_bars_and_markers/scatter_hist.cpp | 12 +- .../lines_bars_and_markers/scatter_symbol.cpp | 10 +- .../scatter_with_legend.cpp | 16 +- .../lines_bars_and_markers/simple_plot.cpp | 6 +- gallery/lines_bars_and_markers/step_demo.cpp | 8 +- gallery/mplot3d/contour3d.cpp | 26 +- gallery/mplot3d/errorbar3d.cpp | 11 +- gallery/mplot3d/lines3d.cpp | 21 +- gallery/mplot3d/lorenz_attractor.cpp | 9 +- gallery/mplot3d/subplot3d.cpp | 49 ++- gallery/mplot3d/surface3d.cpp | 27 +- .../patch_collection.cpp | 7 +- gallery/statistics/errorbar.cpp | 6 +- gallery/statistics/hist.cpp | 14 +- .../align_labels_demo.cpp | 8 +- .../colorbar_placement.cpp | 2 +- .../multiple_figs_demo.cpp | 6 +- .../subplots_axes_and_figures/two_scales.cpp | 8 +- include/matplotlibcpp17/animation.h | 10 +- include/matplotlibcpp17/axes.h | 410 +++++++++--------- include/matplotlibcpp17/cm.h | 7 +- include/matplotlibcpp17/collections.h | 25 +- include/matplotlibcpp17/common.h | 12 +- include/matplotlibcpp17/container.h | 5 +- include/matplotlibcpp17/figure.h | 59 +-- include/matplotlibcpp17/gridspec.h | 5 +- include/matplotlibcpp17/legend.h | 5 +- include/matplotlibcpp17/mplot3d.h | 18 + include/matplotlibcpp17/pyplot.h | 216 ++++----- include/matplotlibcpp17/quiver.h | 6 +- include/matplotlibcpp17/text.h | 17 +- 42 files changed, 635 insertions(+), 572 deletions(-) create mode 100644 include/matplotlibcpp17/mplot3d.h diff --git a/gallery/artist_animation/animate_decay.cpp b/gallery/artist_animation/animate_decay.cpp index a857198..26c4210 100644 --- a/gallery/artist_animation/animate_decay.cpp +++ b/gallery/artist_animation/animate_decay.cpp @@ -27,9 +27,8 @@ int main() { auto [xmin, xmax] = ax.get_xlim(); if (t >= xmax) 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 line = ax.plot(Args(ts, ys), Kwargs("color"_a = "blue", "lw"_a = 1)); + artist_list.append(line.unwrap()); } auto ani = ArtistAnimation(Args(fig.unwrap(), artist_list), Kwargs("interval"_a = 10)); diff --git a/gallery/artist_animation/random_walk.cpp b/gallery/artist_animation/random_walk.cpp index a3dcac5..3fc12ee 100644 --- a/gallery/artist_animation/random_walk.cpp +++ b/gallery/artist_animation/random_walk.cpp @@ -1,7 +1,9 @@ // example from https://matplotlib.org/stable/gallery/animation/random_walk.html +#include #include #include +#include #include #include @@ -32,21 +34,23 @@ int main() { } py::scoped_interpreter guard{}; auto plt = matplotlibcpp17::pyplot::import(); + // this is required for "projection = 3d" + matplotlibcpp17::mplot3d::import(); auto fig = plt.figure(); auto ax = fig.add_subplot(py::make_tuple(), Kwargs("projection"_a = "3d")); py::list artist_list; for (int j = 1; j <= num_steps; ++j) { for (int i = 0; i < M; ++i) { - auto xs0 = xt::view(walks, i, 0, xt::range(1, j + 1)); - auto ys0 = xt::view(walks, i, 1, xt::range(1, j + 1)); - auto zs0 = xt::view(walks, i, 2, xt::range(1, j + 1)); + const auto xs0 = xt::view(walks, i, 0, xt::range(1, j + 1)); + const auto ys0 = xt::view(walks, i, 1, xt::range(1, j + 1)); + const auto zs0 = xt::view(walks, i, 2, xt::range(1, j + 1)); // to vector vector xs(xs0.begin(), xs0.end()); vector ys(ys0.begin(), ys0.end()); vector zs(zs0.begin(), zs0.end()); ax.plot(Args(xs, ys, zs), Kwargs("color"_a = colors[i])); } - artist_list.append(ax.get_lines()); + artist_list.append(ax.get_lines().unwrap()); } auto ani = ArtistAnimation(Args(fig.unwrap(), artist_list), Kwargs("interval"_a = 100)); diff --git a/gallery/images_contours_and_fields/contourf_log.cpp b/gallery/images_contours_and_fields/contourf_log.cpp index 0962e70..38ced46 100644 --- a/gallery/images_contours_and_fields/contourf_log.cpp +++ b/gallery/images_contours_and_fields/contourf_log.cpp @@ -19,12 +19,12 @@ using mesh2D = vector>; int main() { const int N = 100; - auto x_ = xt::linspace(-3.0, 3.0, N); - auto y_ = xt::linspace(-2.0, 2.0, N); + const auto x_ = xt::linspace(-3.0, 3.0, N); + const auto y_ = xt::linspace(-2.0, 2.0, N); - auto [X_, Y_] = xt::meshgrid(x_, y_); - auto Z1_ = xt::exp(-xt::pow(X_, 2) - xt::pow(Y_, 2)); - auto Z2_ = xt::exp(-xt::pow(X_ * 10, 2) - xt::pow(Y_ * 10, 2)); + const auto [X_, Y_] = xt::meshgrid(x_, y_); + const auto Z1_ = xt::exp(-xt::pow(X_, 2) - xt::pow(Y_, 2)); + const auto Z2_ = xt::exp(-xt::pow(X_ * 10, 2) - xt::pow(Y_ * 10, 2)); xt::xarray z_ = Z1_ + 50 * Z2_; // instead of x[:5, :5] = -1.0 auto v = xt::view(z_, xt::range(_, 5), xt::range(_, 5)); @@ -45,14 +45,14 @@ int main() { py::scoped_interpreter guard{}; // to numpy array - auto Xpy = py::array(py::cast(std::move(X))); - auto Ypy = py::array(py::cast(std::move(Y))); - auto zpy = py::array(py::cast(std::move(z))); + const auto Xpy = py::array(py::cast(std::move(X))); + const auto Ypy = py::array(py::cast(std::move(Y))); + const auto zpy = py::array(py::cast(std::move(z))); auto plt = pyplot::import(); auto [fig, ax] = plt.subplots(); auto cs = ax.contourf(Args(Xpy, Ypy, zpy), Kwargs("locator"_a = ticker::LogLocator().unwrap(), "cmap"_a = cm::PuBu_r)); - fig.colorbar(Args(cs)); + fig.colorbar(Args(cs.unwrap())); plt.show(); } diff --git a/gallery/images_contours_and_fields/image_demo.cpp b/gallery/images_contours_and_fields/image_demo.cpp index c152632..c63cfc7 100644 --- a/gallery/images_contours_and_fields/image_demo.cpp +++ b/gallery/images_contours_and_fields/image_demo.cpp @@ -19,10 +19,10 @@ using mesh2D = vector>; int main() { const double delta = 0.025; const auto x = xt::arange(-3.0, 3.0, delta); - auto [X_, Y_] = xt::meshgrid(x, x); - auto Z1_ = xt::exp(-xt::pow(X_, 2) - xt::pow(Y_, 2)); - auto Z2_ = xt::exp(-xt::pow(X_ - 1, 2) - xt::pow(Y_ - 1, 2)); - auto Z_ = (Z1_ - Z2_) * 2.0; + const auto [X_, Y_] = xt::meshgrid(x, x); + const auto Z1_ = xt::exp(-xt::pow(X_, 2) - xt::pow(Y_, 2)); + const auto Z2_ = xt::exp(-xt::pow(X_ - 1, 2) - xt::pow(Y_ - 1, 2)); + const auto Z_ = (Z1_ - Z2_) * 2.0; // to vector vector X(X_.begin(), X_.end()), Y(Y_.begin(), Y_.end()), @@ -42,7 +42,7 @@ int main() { auto [fig, ax] = plt.subplots(); const double vmax = *max_element(Z_.begin(), Z_.end()), vmin = *min_element(Z_.begin(), Z_.end()); - auto Zpy = py::array(py::cast(std::move(Z2D))); + const auto Zpy = py::array(py::cast(std::move(Z2D))); ax.imshow(Args(Zpy), Kwargs("interpolation"_a = "bilinear", "cmap"_a = cm::RdYlGn, "origin"_a = "lower", "extent"_a = py::make_tuple(-3, 3, -3, 3), diff --git a/gallery/images_contours_and_fields/quiver_demo.cpp b/gallery/images_contours_and_fields/quiver_demo.cpp index bea4f82..2deff9f 100644 --- a/gallery/images_contours_and_fields/quiver_demo.cpp +++ b/gallery/images_contours_and_fields/quiver_demo.cpp @@ -14,10 +14,10 @@ using namespace std; using namespace matplotlibcpp17; int main1() { - auto [X0, Y0] = xt::meshgrid(xt::arange(0.0, 2 * M_PI, 0.2), - xt::arange(0.0, 2 * M_PI, 0.2)); - auto U0 = xt::cos(X0); - auto V0 = xt::sin(Y0); + const auto [X0, Y0] = xt::meshgrid(xt::arange(0.0, 2 * M_PI, 0.2), + xt::arange(0.0, 2 * M_PI, 0.2)); + const auto U0 = xt::cos(X0); + const auto V0 = xt::sin(Y0); // to vector vector X(X0.begin(), X0.end()), Y(Y0.begin(), Y0.end()), U(U0.begin(), U0.end()), V(V0.begin(), V0.end()); @@ -38,10 +38,10 @@ int main1() { } int main2() { - auto [X0, Y0] = xt::meshgrid(xt::arange(0.0, 2 * M_PI, 0.6), - xt::arange(0.0, 2 * M_PI, 0.6)); - auto U0 = xt::cos(X0); - auto V0 = xt::sin(Y0); + const auto [X0, Y0] = xt::meshgrid(xt::arange(0.0, 2 * M_PI, 0.6), + xt::arange(0.0, 2 * M_PI, 0.6)); + const auto U0 = xt::cos(X0); + const auto V0 = xt::sin(Y0); // to vector vector X(X0.begin(), X0.end()), Y(Y0.begin(), Y0.end()), U(U0.begin(), U0.end()), V(V0.begin(), V0.end()); @@ -65,11 +65,11 @@ int main2() { } int main3() { - auto [X0, Y0] = xt::meshgrid(xt::arange(0.0, 2 * M_PI, 0.2), - xt::arange(0.0, 2 * M_PI, 0.2)); - auto U0 = xt::cos(X0); - auto V0 = xt::sin(Y0); - auto M0 = xt::hypot(U0, V0); + const auto [X0, Y0] = xt::meshgrid(xt::arange(0.0, 2 * M_PI, 0.2), + xt::arange(0.0, 2 * M_PI, 0.2)); + const auto U0 = xt::cos(X0); + const auto V0 = xt::sin(Y0); + const auto M0 = xt::hypot(U0, V0); vector X(X0.begin(), X0.end()), Y(Y0.begin(), Y0.end()), U(U0.begin(), U0.end()), V(V0.begin(), V0.end()), M(M0.begin(), M0.end()); diff --git a/gallery/lines_bars_and_markers/bar_label_demo.cpp b/gallery/lines_bars_and_markers/bar_label_demo.cpp index 60a213b..adde382 100644 --- a/gallery/lines_bars_and_markers/bar_label_demo.cpp +++ b/gallery/lines_bars_and_markers/bar_label_demo.cpp @@ -11,11 +11,11 @@ using namespace std; using namespace matplotlibcpp17; int main1() { - vector menMeans = {20, 35, 30, 35, -27}; - vector womenMeans = {25, 32, 34, 20, -25}; - vector menStd = {2, 3, 4, 1, 2}; - vector womenStd = {3, 5, 2, 3, 3}; - vector ind = {0, 1, 2, 3, 4}; // the x locations for the groups + const vector menMeans = {20, 35, 30, 35, -27}; + const vector womenMeans = {25, 32, 34, 20, -25}; + const vector menStd = {2, 3, 4, 1, 2}; + const vector womenStd = {3, 5, 2, 3, 3}; + const vector ind = {0, 1, 2, 3, 4}; // the x locations for the groups const double width = 0.35; // the width of the bars: can also be len(x) sequence auto plt = matplotlibcpp17::pyplot::import(); @@ -44,12 +44,12 @@ int main1() { } int main2() { - vector people = {"Tom", "Dick", "Harry", "Slim", "Jim"}; - vector y_pos = {0, 1, 2, 3, 4}; - vector performance = {10.00367304, 10.42750809, 10.09280011, - 8.66745522, 12.77785333}; - vector error = {0.70633485, 0.24791576, 0.15788335, 0.69769852, - 0.71995667}; + const vector people = {"Tom", "Dick", "Harry", "Slim", "Jim"}; + const vector y_pos = {0, 1, 2, 3, 4}; + const vector performance = {10.00367304, 10.42750809, 10.09280011, + 8.66745522, 12.77785333}; + const vector error = {0.70633485, 0.24791576, 0.15788335, 0.69769852, + 0.71995667}; auto plt = matplotlibcpp17::pyplot::import(); auto [fig, ax] = plt.subplots(); auto hbars = ax.barh(Args(y_pos, performance), @@ -71,12 +71,12 @@ int main2() { } int main3() { - vector people = {"Tom", "Dick", "Harry", "Slim", "Jim"}; - vector y_pos = {0, 1, 2, 3, 4}; - vector performance = {10.00367304, 10.42750809, 10.09280011, - 8.66745522, 12.77785333}; - vector error = {0.70633485, 0.24791576, 0.15788335, 0.69769852, - 0.71995667}; + const vector people = {"Tom", "Dick", "Harry", "Slim", "Jim"}; + const vector y_pos = {0, 1, 2, 3, 4}; + const vector performance = {10.00367304, 10.42750809, 10.09280011, + 8.66745522, 12.77785333}; + const vector error = {0.70633485, 0.24791576, 0.15788335, 0.69769852, + 0.71995667}; auto plt = matplotlibcpp17::pyplot::import(); auto [fig, ax] = plt.subplots(); auto hbars = ax.barh(Args(y_pos, performance), diff --git a/gallery/lines_bars_and_markers/errorbar_limits_simple.cpp b/gallery/lines_bars_and_markers/errorbar_limits_simple.cpp index 10fa71e..80c4969 100644 --- a/gallery/lines_bars_and_markers/errorbar_limits_simple.cpp +++ b/gallery/lines_bars_and_markers/errorbar_limits_simple.cpp @@ -15,11 +15,11 @@ int main() { py::scoped_interpreter guard{}; auto plt = pyplot::import(); auto fig = plt.figure(); - auto x_ = xt::arange(0.0, 10.0, 1.0); - auto y_ = 2.5 * xt::sin(x_ / 20 * M_PI); - auto y1_ = y_ + 1.0, y2_ = y_ + 2.0, y3_ = y_ + 3.0; - auto yerr_ = xt::linspace(0.05, 0.2, 10); - vector x(x_.begin(), x_.end()), y(y_.begin(), y_.end()), + const auto x_ = xt::arange(0.0, 10.0, 1.0); + const auto y_ = 2.5 * xt::sin(x_ / 20 * M_PI); + const auto y1_ = y_ + 1.0, y2_ = y_ + 2.0, y3_ = y_ + 3.0; + const auto yerr_ = xt::linspace(0.05, 0.2, 10); + const vector x(x_.begin(), x_.end()), y(y_.begin(), y_.end()), yerr(yerr_.begin(), yerr_.end()), y3(y3_.begin(), y3_.end()), y2(y2_.begin(), y2_.end()), y1(y1_.begin(), y1_.end()); plt.errorbar(Args(x, y3), diff --git a/gallery/lines_bars_and_markers/errorbar_subsample.cpp b/gallery/lines_bars_and_markers/errorbar_subsample.cpp index f9a08d3..ebc80b8 100644 --- a/gallery/lines_bars_and_markers/errorbar_subsample.cpp +++ b/gallery/lines_bars_and_markers/errorbar_subsample.cpp @@ -12,12 +12,12 @@ using namespace std; using namespace matplotlibcpp17; int main() { - auto x_ = xt::arange(0.1, 4.0, 0.1); - auto y1_ = xt::exp(-1.0 * x_); - auto y2_ = xt::exp(-0.5 * x_); - auto y1err_ = 0.1 + 0.1 * xt::sqrt(x_); - auto y2err_ = 0.1 + 0.1 * xt::sqrt(x_ / 2.0); - vector x(x_.begin(), x_.end()), y1(y1_.begin(), y1_.end()), + const auto x_ = xt::arange(0.1, 4.0, 0.1); + const auto y1_ = xt::exp(-1.0 * x_); + const auto y2_ = xt::exp(-0.5 * x_); + const auto y1err_ = 0.1 + 0.1 * xt::sqrt(x_); + const auto y2err_ = 0.1 + 0.1 * xt::sqrt(x_ / 2.0); + const vector x(x_.begin(), x_.end()), y1(y1_.begin(), y1_.end()), y2(y2_.begin(), y2_.end()), y1err(y1err_.begin(), y1err_.end()), y2err(y2err_.begin(), y2err_.end()); @@ -34,11 +34,14 @@ int main() { ax1.errorbar(Args(x, y1), Kwargs("yerr"_a = y1err, "errorevery"_a = 6)); ax1.errorbar(Args(x, y2), Kwargs("yerr"_a = y2err, "errorevery"_a = 6)); + // TODO: TypeError: '<' not supported between instances of 'tuple' and 'int' + /* ax2.set_title(Args("second seris shifted by 3")); ax2.errorbar(Args(x, y1), Kwargs("yerr"_a = y1err, "errorevery"_a = py::make_tuple(0, 6))); ax2.errorbar(Args(x, y2), Kwargs("yerr"_a = y2err, "errorevery"_a = py::make_tuple(3, 6))); + */ fig.suptitle(Args("Errorbar subsampling")); #if USE_GUI diff --git a/gallery/lines_bars_and_markers/fill.cpp b/gallery/lines_bars_and_markers/fill.cpp index 0e763c8..cd322ae 100644 --- a/gallery/lines_bars_and_markers/fill.cpp +++ b/gallery/lines_bars_and_markers/fill.cpp @@ -13,10 +13,10 @@ using namespace matplotlibcpp17; int main() { const double scale = 10; const xt::xarray angles = {90.0, 210.0, 330.0}; - auto x0 = scale / sqrt(3.0) * xt::cos(angles / 360.0 * 2 * M_PI); - auto y0 = scale / sqrt(3.0) * xt::sin(angles / 360.0 * 2 * M_PI); - vector x(x0.begin(), x0.end()); - vector y(y0.begin(), y0.end()); + const auto x0 = scale / sqrt(3.0) * xt::cos(angles / 360.0 * 2 * M_PI); + const auto y0 = scale / sqrt(3.0) * xt::sin(angles / 360.0 * 2 * M_PI); + const vector x(x0.begin(), x0.end()); + const vector y(y0.begin(), y0.end()); py::scoped_interpreter guard{}; auto plt = matplotlibcpp17::pyplot::import(); diff --git a/gallery/lines_bars_and_markers/fill_between_demo.cpp b/gallery/lines_bars_and_markers/fill_between_demo.cpp index 1e4e0b3..ae7337e 100644 --- a/gallery/lines_bars_and_markers/fill_between_demo.cpp +++ b/gallery/lines_bars_and_markers/fill_between_demo.cpp @@ -14,10 +14,10 @@ using namespace std; using namespace matplotlibcpp17; int main1() { - auto x_ = xt::arange(0.0, 2.0, 0.01); - auto y1_ = xt::sin(2 * M_PI * x_); - auto y2_ = 0.8 * xt::sin(4 * M_PI * x_); - vector x(x_.begin(), x_.end()), y1(y1_.begin(), y1_.end()), + const auto x_ = xt::arange(0.0, 2.0, 0.01); + const auto y1_ = xt::sin(2 * M_PI * x_); + const auto y2_ = 0.8 * xt::sin(4 * M_PI * x_); + const vector x(x_.begin(), x_.end()), y1(y1_.begin(), y1_.end()), y2(y2_.begin(), y2_.end()); auto plt = matplotlibcpp17::pyplot::import(); @@ -45,9 +45,9 @@ int main1() { int main2() { auto plt = matplotlibcpp17::pyplot::import(); - vector x = {0, 1, 2, 3}; - vector y1 = {0.8, 0.8, 0.2, 0.2}; - vector y2 = {0, 0, 1, 1}; + const vector x = {0, 1, 2, 3}; + const vector y1 = {0.8, 0.8, 0.2, 0.2}; + const vector y2 = {0, 0, 1, 1}; auto [fig, axs] = plt.subplots(2, 1, Kwargs("sharex"_a = true)); auto ax1 = axs[0], ax2 = axs[1]; @@ -85,8 +85,8 @@ int main3() { auto plt = matplotlibcpp17::pyplot::import(); auto [fig, ax] = plt.subplots(); - auto x0 = xt::arange(0.0, 4 * M_PI, 0.01); - auto y0 = xt::sin(x0); + const auto x0 = xt::arange(0.0, 4 * M_PI, 0.01); + const auto y0 = xt::sin(x0); vector x(x0.begin(), x0.end()), y(y0.begin(), y0.end()); ax.plot(Args(x, y), Kwargs("color"_a = "black")); const double threshold = 0.75; @@ -96,7 +96,7 @@ int main3() { ax.fill_between(Args(x, 0, 1), Kwargs("where"_a = where, "color"_a = "green", "alpha"_a = 0.5, - "transform"_a = ax.get_xaxis_transform())); + "transform"_a = ax.get_xaxis_transform().unwrap())); #if USE_GUI plt.show(); #else diff --git a/gallery/lines_bars_and_markers/fill_betweenx_demo.cpp b/gallery/lines_bars_and_markers/fill_betweenx_demo.cpp index eefa7a3..2a8ad8d 100644 --- a/gallery/lines_bars_and_markers/fill_betweenx_demo.cpp +++ b/gallery/lines_bars_and_markers/fill_betweenx_demo.cpp @@ -13,10 +13,10 @@ using namespace matplotlibcpp17; int main1() { auto plt = matplotlibcpp17::pyplot::import(); - auto y_ = xt::arange(0.0, 2.0, 0.01); - auto x1_ = xt::sin(2 * M_PI * y_); - auto x2_ = 1.2 * xt::sin(4 * M_PI * y_); - vector y(y_.begin(), y_.end()), x1(x1_.begin(), x1_.end()), + const auto y_ = xt::arange(0.0, 2.0, 0.01); + const auto x1_ = xt::sin(2 * M_PI * y_); + const auto x2_ = 1.2 * xt::sin(4 * M_PI * y_); + const vector y(y_.begin(), y_.end()), x1(x1_.begin(), x1_.end()), x2(x2_.begin(), x2_.end()); auto [fig, axes] = plt.subplots( diff --git a/gallery/lines_bars_and_markers/scatter_hist.cpp b/gallery/lines_bars_and_markers/scatter_hist.cpp index 5118a30..1d102c0 100644 --- a/gallery/lines_bars_and_markers/scatter_hist.cpp +++ b/gallery/lines_bars_and_markers/scatter_hist.cpp @@ -14,9 +14,9 @@ using namespace matplotlibcpp17; int main() { int N = 1000; - auto x_ = xt::random::randn({N}); - auto y_ = xt::random::randn({N}); - vector x(x_.begin(), x_.end()), y(y_.begin(), y_.end()); + const auto x_ = xt::random::randn({N}); + const auto y_ = xt::random::randn({N}); + const vector x(x_.begin(), x_.end()), y(y_.begin(), y_.end()); py::scoped_interpreter guard{}; auto plt = matplotlibcpp17::pyplot::import(); // cell1 @@ -40,7 +40,8 @@ int main() { ax_histy.tick_params(Args(), Kwargs("axis"_a = "y", "labelleft"_a = false)); ax.scatter(Args(x, y)); const double binwidth = 0.25; - auto xi = xt::amax(xt::fabs(x_), {0}), yi = xt::amax(xt::fabs(y_), {0}); + const auto xi = xt::amax(xt::fabs(x_), {0}), + yi = xt::amax(xt::fabs(y_), {0}); const double xymax = max(fabs(x_[xi]), fabs(y_[yi])); const double lim = (static_cast(xymax / binwidth) + 1) * binwidth; auto bins_ = xt::arange(-lim, lim + binwidth, binwidth); @@ -73,7 +74,8 @@ int main() { ax_histy.tick_params(Args(), Kwargs("axis"_a = "y", "labelleft"_a = false)); ax.scatter(Args(x, y)); const double binwidth = 0.25; - auto xi = xt::amax(xt::fabs(x_), {0}), yi = xt::amax(xt::fabs(y_), {0}); + const auto xi = xt::amax(xt::fabs(x_), {0}), + yi = xt::amax(xt::fabs(y_), {0}); const double xymax = max(fabs(x_[xi]), fabs(y_[yi])); const double lim = (static_cast(xymax / binwidth) + 1) * binwidth; auto bins_ = xt::arange(-lim, lim + binwidth, binwidth); diff --git a/gallery/lines_bars_and_markers/scatter_symbol.cpp b/gallery/lines_bars_and_markers/scatter_symbol.cpp index 8c82d81..8079b52 100644 --- a/gallery/lines_bars_and_markers/scatter_symbol.cpp +++ b/gallery/lines_bars_and_markers/scatter_symbol.cpp @@ -23,16 +23,16 @@ using namespace matplotlibcpp17; int main() { py::scoped_interpreter guard{}; 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.}; - vector y = { + const 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.}; + const vector y = { 21.01101912, 24.74481311, 27.34126659, 27.27298483, 44.26208785, 41.14266853, 32.72670355, 35.63706738, 57.689303, 64.43917295, 56.86145395, 65.85596686, 91.33222544, 89.93319308, 90.07761828, 104.3101143, 105.86324421, 125.79378295, 127.67869682, 131.83987721, 140.51644988, 140.79566887, 153.22398837, 169.06951457, 174.97156606}; - vector s = { + const vector s = { 736.2911849, 628.75670445, 664.90041181, 607.46030945, 884.4840139, 774.0174507, 790.37543212, 1278.33411095, 588.75488929, 810.61127126, 1126.45270023, 1278.31780809, 886.56768427, 769.13688434, 953.93522899, diff --git a/gallery/lines_bars_and_markers/scatter_with_legend.cpp b/gallery/lines_bars_and_markers/scatter_with_legend.cpp index 57d97d5..ad52ece 100644 --- a/gallery/lines_bars_and_markers/scatter_with_legend.cpp +++ b/gallery/lines_bars_and_markers/scatter_with_legend.cpp @@ -37,19 +37,19 @@ int main1() { int main2() { int N = 45; - auto x_ = xt::random::rand({N}, 0.0, 1.0); - auto y_ = xt::random::rand({N}, 0.0, 1.0); - auto c_ = xt::random::randint({N}, 1, 5); - auto s_ = xt::random::randint({N}, 10, 220); - vector x(x_.begin(), x_.end()), y(y_.begin(), y_.end()); - vector c(c_.begin(), c_.end()), s(s_.begin(), s_.end()); + const auto x_ = xt::random::rand({N}, 0.0, 1.0); + const auto y_ = xt::random::rand({N}, 0.0, 1.0); + const auto c_ = xt::random::randint({N}, 1, 5); + const auto s_ = xt::random::randint({N}, 10, 220); + const vector x(x_.begin(), x_.end()), y(y_.begin(), y_.end()); + const vector c(c_.begin(), c_.end()), s(s_.begin(), s_.end()); auto plt = matplotlibcpp17::pyplot::import(); auto [fig, ax] = plt.subplots(); auto scatter = ax.scatter(Args(x, y), Kwargs("c"_a = c, "s"_a = s)); { auto [handles, labels] = scatter.legend_elements(); auto legend1 = - ax.legend(Args(handles, labels), + ax.legend(Args(handles.unwrap(), labels.unwrap()), Kwargs("loc"_a = "lower left", "title"_a = "Classes")); ax.add_artist(Args(legend1.unwrap())); } @@ -57,7 +57,7 @@ int main2() { auto [handles, labels] = scatter.legend_elements( Args(), Kwargs("prop"_a = "sizes", "alpha"_a = 0.6)); auto legend2 = - ax.legend(Args(handles, labels), + ax.legend(Args(handles.unwrap(), labels.unwrap()), Kwargs("loc"_a = "upper right", "title"_a = "Sizes")); } #if USE_GUI diff --git a/gallery/lines_bars_and_markers/simple_plot.cpp b/gallery/lines_bars_and_markers/simple_plot.cpp index ed7b234..f495171 100644 --- a/gallery/lines_bars_and_markers/simple_plot.cpp +++ b/gallery/lines_bars_and_markers/simple_plot.cpp @@ -14,9 +14,9 @@ using namespace matplotlibcpp17; int main() { py::scoped_interpreter guard{}; auto plt = pyplot::import(); - auto t_ = xt::arange(0.0, 2.0, 0.01); - auto s_ = xt::sin(2 * M_PI * t_) + 1.0; - vector t(t_.begin(), t_.end()), s(s_.begin(), s_.end()); + const auto t_ = xt::arange(0.0, 2.0, 0.01); + const auto s_ = xt::sin(2 * M_PI * t_) + 1.0; + const vector t(t_.begin(), t_.end()), s(s_.begin(), s_.end()); auto [fig, ax] = plt.subplots(); ax.plot(Args(t, s), Kwargs("color"_a = "blue", "linewidth"_a = 1.0)); diff --git a/gallery/lines_bars_and_markers/step_demo.cpp b/gallery/lines_bars_and_markers/step_demo.cpp index e5da76b..05c9c19 100644 --- a/gallery/lines_bars_and_markers/step_demo.cpp +++ b/gallery/lines_bars_and_markers/step_demo.cpp @@ -15,10 +15,10 @@ int main() { py::scoped_interpreter guard{}; auto plt = pyplot::import(); - auto x_ = xt::arange(14) * 1.0; - auto y_ = xt::sin(x_ / 2.0); - auto y1_ = y_ + 1.0, y2_ = y_ + 2.0; - vector x(x_.begin(), x_.end()), y(y_.begin(), y_.end()), + const auto x_ = xt::arange(14) * 1.0; + const auto y_ = xt::sin(x_ / 2.0); + const auto y1_ = y_ + 1.0, y2_ = y_ + 2.0; + const vector x(x_.begin(), x_.end()), y(y_.begin(), y_.end()), y1(y1_.begin(), y1_.end()), y2(y2_.begin(), y2_.end()); plt.step(Args(x, y2), Kwargs("label"_a = "pre (default)")); diff --git a/gallery/mplot3d/contour3d.cpp b/gallery/mplot3d/contour3d.cpp index 60912cf..36cdac5 100644 --- a/gallery/mplot3d/contour3d.cpp +++ b/gallery/mplot3d/contour3d.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -17,18 +18,19 @@ using mesh2D = vector>; // from mpl_toolkits.axes3d.py tuple get_test_data(double delta = 0.05) { - auto xs = xt::arange(-3.0, 3.0, delta); - auto ys = xt::arange(-3.0, 3.0, delta); - auto [X0, Y0] = xt::meshgrid(xs, ys); // 120x160 - auto Z1 = xt::exp(-(xt::pow(X0, 2) + xt::pow(Y0, 2)) / 2.0) / (2 * M_PI); - auto Z2 = + const auto xs = xt::arange(-3.0, 3.0, delta); + const auto ys = xt::arange(-3.0, 3.0, delta); + const auto [X0, Y0] = xt::meshgrid(xs, ys); // 120x160 + const auto Z1 = + xt::exp(-(xt::pow(X0, 2) + xt::pow(Y0, 2)) / 2.0) / (2 * M_PI); + const auto Z2 = xt::exp(-(xt::pow((X0 - 1.0) / 1.5, 2) + xt::pow((Y0 - 1.0) / 0.5, 2)) / 2) / (2 * M_PI * 0.5 * 1.5); - auto Z0 = Z2 - Z1; - auto X = X0 * 10; - auto Y = Y0 * 10; - auto Z = Z0 * 500; + const auto Z0 = Z2 - Z1; + const auto X = X0 * 10; + const auto Y = Y0 * 10; + const auto Z = Z0 * 500; const int szx = xs.shape()[0], szy = ys.shape()[0]; mesh2D X_(szx), Y_(szx), Z_(szx); for (int i = 0; i < szx; ++i) { @@ -47,10 +49,12 @@ tuple get_test_data(double delta = 0.05) { int main() { py::scoped_interpreter guard{}; auto plt = matplotlibcpp17::pyplot::import(); + // this is required for "projection = 3d" + matplotlibcpp17::mplot3d::import(); auto fig = plt.figure(); auto ax = fig.add_subplot(Args(), Kwargs("projection"_a = "3d")); - auto [X, Y, Z] = get_test_data(0.05); - ax.contour(Args(X, Y, Z), Kwargs("cmap"_a = cm::coolwarm())); + const auto [X, Y, Z] = get_test_data(0.05); + ax.contour(Args(X, Y, Z), Kwargs("cmap"_a = cm::coolwarm)); #if USE_GUI plt.show(); #else diff --git a/gallery/mplot3d/errorbar3d.cpp b/gallery/mplot3d/errorbar3d.cpp index 283a0bb..eb58d23 100644 --- a/gallery/mplot3d/errorbar3d.cpp +++ b/gallery/mplot3d/errorbar3d.cpp @@ -1,6 +1,7 @@ // example from https://matplotlib.org/stable/gallery/mplot3d/errorbar3d.html #include +#include #include #include @@ -14,11 +15,13 @@ using namespace matplotlibcpp17; int main() { py::scoped_interpreter guard{}; auto plt = pyplot::import(); + // this is required for "projection = 3d" + matplotlibcpp17::mplot3d::import(); auto ax = plt.figure().add_subplot(Args(), Kwargs("projection"_a = "3d")); - auto t_ = xt::arange(0.0, 2 * M_PI + 0.1, 0.01); - auto x_ = xt::sin(1.0 * t_); - auto y_ = xt::cos(3.0 * t_); - auto z_ = xt::sin(5.0 * t_); + const auto t_ = xt::arange(0.0, 2 * M_PI + 0.1, 0.01); + const auto x_ = xt::sin(1.0 * t_); + const auto y_ = xt::cos(3.0 * t_); + const auto z_ = xt::sin(5.0 * t_); vector t(t_.begin(), t_.end()), x(x_.begin(), x_.end()), y(y_.begin(), y_.end()), z(z_.begin(), z_.end()); diff --git a/gallery/mplot3d/lines3d.cpp b/gallery/mplot3d/lines3d.cpp index 186502f..4752efc 100644 --- a/gallery/mplot3d/lines3d.cpp +++ b/gallery/mplot3d/lines3d.cpp @@ -1,6 +1,7 @@ // example from https://matplotlib.org/stable/gallery/mplot3d/lines3d.html #include +#include #include #include @@ -13,17 +14,19 @@ using namespace matplotlibcpp17; int main() { py::scoped_interpreter guard{}; auto plt = matplotlibcpp17::pyplot::import(); + // this is required for "projection = 3d" + matplotlibcpp17::mplot3d::import(); auto fig = plt.figure(); auto ax = fig.add_subplot(Args(), Kwargs("projection"_a = "3d")); - auto theta_ = xt::linspace(-4 * M_PI, 4 * M_PI, 100); - auto z_ = xt::linspace(-2.0, 2.0, 100); - auto r_ = 1.0 + xt::pow(z_, 2); - auto x_ = r_ * xt::sin(theta_); - auto y_ = r_ * xt::cos(theta_); - vector z(z_.begin(), z_.end()); - vector r(r_.begin(), r_.end()); - vector theta(theta_.begin(), theta_.end()); - vector x(x_.begin(), x_.end()), y(y_.begin(), y_.end()); + const auto theta_ = xt::linspace(-4 * M_PI, 4 * M_PI, 100); + const auto z_ = xt::linspace(-2.0, 2.0, 100); + const auto r_ = 1.0 + xt::pow(z_, 2); + const auto x_ = r_ * xt::sin(theta_); + const auto y_ = r_ * xt::cos(theta_); + const vector z(z_.begin(), z_.end()); + const vector r(r_.begin(), r_.end()); + const vector theta(theta_.begin(), theta_.end()); + const vector x(x_.begin(), x_.end()), y(y_.begin(), y_.end()); ax.plot(Args(x, y, z), Kwargs("label"_a = "parametric curve")); ax.legend(); #if USE_GUI diff --git a/gallery/mplot3d/lorenz_attractor.cpp b/gallery/mplot3d/lorenz_attractor.cpp index 562b6ae..6588a19 100644 --- a/gallery/mplot3d/lorenz_attractor.cpp +++ b/gallery/mplot3d/lorenz_attractor.cpp @@ -2,6 +2,7 @@ // https://matplotlib.org/stable/gallery/mplot3d/lorenz_attractor.html #include +#include #include @@ -11,15 +12,17 @@ using namespace matplotlibcpp17; tuple lorenz(double x, double y, double z, double s = 10, double r = 28, double b = 2.667) { - double x_dot = s * (y - x); - double y_dot = r * x - y - x * z; - double z_dot = x * y - b * z; + const double x_dot = s * (y - x); + const double y_dot = r * x - y - x * z; + const double z_dot = x * y - b * z; return {x_dot, y_dot, z_dot}; } int main() { py::scoped_interpreter guard{}; auto plt = matplotlibcpp17::pyplot::import(); + // this is required for "projection = 3d" + matplotlibcpp17::mplot3d::import(); auto fig = plt.figure(); auto ax = fig.add_subplot(Args(), Kwargs("projection"_a = "3d")); diff --git a/gallery/mplot3d/subplot3d.cpp b/gallery/mplot3d/subplot3d.cpp index 8f9f639..69f5ba9 100644 --- a/gallery/mplot3d/subplot3d.cpp +++ b/gallery/mplot3d/subplot3d.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -17,18 +18,19 @@ using mesh2D = vector>; // from mpl_toolkits.axes3d.py tuple get_test_data(double delta = 0.05) { - auto xs = xt::arange(-3.0, 3.0, delta); - auto ys = xt::arange(-3.0, 3.0, delta); - auto [X0, Y0] = xt::meshgrid(xs, ys); // 120x160 - auto Z1 = xt::exp(-(xt::pow(X0, 2) + xt::pow(Y0, 2)) / 2.0) / (2 * M_PI); - auto Z2 = + const auto xs = xt::arange(-3.0, 3.0, delta); + const auto ys = xt::arange(-3.0, 3.0, delta); + const auto [X0, Y0] = xt::meshgrid(xs, ys); // 120x160 + const auto Z1 = + xt::exp(-(xt::pow(X0, 2) + xt::pow(Y0, 2)) / 2.0) / (2 * M_PI); + const auto Z2 = xt::exp(-(xt::pow((X0 - 1.0) / 1.5, 2) + xt::pow((Y0 - 1.0) / 0.5, 2)) / 2) / (2 * M_PI * 0.5 * 1.5); - auto Z0 = Z2 - Z1; - auto X = X0 * 10; - auto Y = Y0 * 10; - auto Z = Z0 * 500; + const auto Z0 = Z2 - Z1; + const auto X = X0 * 10; + const auto Y = Y0 * 10; + const auto Z = Z0 * 500; const int szx = xs.shape()[0], szy = ys.shape()[0]; mesh2D X_(szx), Y_(szx), Z_(szx); for (int i = 0; i < szx; ++i) { @@ -47,14 +49,16 @@ tuple get_test_data(double delta = 0.05) { int main() { py::scoped_interpreter guard{}; auto plt = matplotlibcpp17::pyplot::import(); + // this is required for "projection = 3d" + matplotlibcpp17::mplot3d::import(); auto [w, h] = plt.figaspect(Args(0.5)); auto fig = plt.figure(Args(), Kwargs("figsize"_a = py::make_tuple(w, h))); { auto ax = fig.add_subplot(Args(1, 2, 1), Kwargs("projection"_a = "3d")); - auto xs = xt::arange(-5.0, 5.0, 0.25); - auto [X0, Y0] = xt::meshgrid(xs, xs); - auto R0 = xt::sqrt(xt::pow(X0, 2) + xt::pow(Y0, 2)); - auto Z0 = xt::sin(R0); + const auto xs = xt::arange(-5.0, 5.0, 0.25); + const auto [X0, Y0] = xt::meshgrid(xs, xs); + const auto R0 = xt::sqrt(xt::pow(X0, 2) + xt::pow(Y0, 2)); + const auto Z0 = xt::sin(R0); // to vector const int sz = xs.shape()[0]; mesh2D X(sz), Y(sz), Z(sz); @@ -70,22 +74,23 @@ int main() { } // NOTE: conversion to numpy array // ref: https://github.com/pybind/pybind11/issues/2066 - auto X_ = py::array(py::cast(std::move(X))); - auto Y_ = py::array(py::cast(std::move(Y))); - auto Z_ = py::array(py::cast(std::move(Z))); + const auto X_ = py::array(py::cast(std::move(X))); + const auto Y_ = py::array(py::cast(std::move(Y))); + const auto Z_ = py::array(py::cast(std::move(Z))); auto surf = ax.plot_surface( Args(X_, Y_, Z_), Kwargs("rstride"_a = 1, "cstride"_a = 1, "linewidth"_a = 0, - "antialiased"_a = false, "cmap"_a = cm::coolwarm())); + "antialiased"_a = false, "cmap"_a = cm::coolwarm)); ax.set_zlim(Args(-1.01, 1.01)); - fig.colorbar(Args(surf), Kwargs("shrink"_a = 0.5, "aspect"_a = 10)); + fig.colorbar(Args(surf.unwrap()), + Kwargs("shrink"_a = 0.5, "aspect"_a = 10)); } { auto ax = fig.add_subplot(Args(1, 2, 2), Kwargs("projection"_a = "3d")); - auto [X, Y, Z] = get_test_data(0.05); - auto X_ = py::array(py::cast(std::move(X))); - auto Y_ = py::array(py::cast(std::move(Y))); - auto Z_ = py::array(py::cast(std::move(Z))); + const auto [X, Y, Z] = get_test_data(0.05); + const auto X_ = py::array(py::cast(std::move(X))); + const auto Y_ = py::array(py::cast(std::move(Y))); + const auto Z_ = py::array(py::cast(std::move(Z))); ax.plot_wireframe(Args(X_, Y_, Z_), Kwargs("rstride"_a = 10, "cstride"_a = 10)); } diff --git a/gallery/mplot3d/surface3d.cpp b/gallery/mplot3d/surface3d.cpp index c9defb5..488e5ec 100644 --- a/gallery/mplot3d/surface3d.cpp +++ b/gallery/mplot3d/surface3d.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -16,13 +17,15 @@ using mesh2D = vector>; int main() { py::scoped_interpreter guard{}; auto plt = matplotlibcpp17::pyplot::import(); + // this is required for "projection = 3d" + matplotlibcpp17::mplot3d::import(); auto [fig, ax] = plt.subplots(Kwargs("subplot_kw"_a = py::dict("projection"_a = "3d"))); - auto xs = xt::arange(-5.0, 5.0, 0.25); - auto [X0, Y0] = xt::meshgrid(xs, xs); - auto R0 = xt::sqrt(xt::pow(X0, 2) + xt::pow(Y0, 2)); - auto Z0 = xt::sin(R0); + const auto xs = xt::arange(-5.0, 5.0, 0.25); + const auto [X0, Y0] = xt::meshgrid(xs, xs); + const auto R0 = xt::sqrt(xt::pow(X0, 2) + xt::pow(Y0, 2)); + const auto Z0 = xt::sin(R0); // to vector const int sz = xs.shape()[0]; mesh2D X(sz), Y(sz), Z(sz); @@ -37,18 +40,18 @@ int main() { } } // to numpy array (vector is converted to list of list) - auto X_ = py::array(py::cast(std::move(X))); - auto Y_ = py::array(py::cast(std::move(Y))); - auto Z_ = py::array(py::cast(std::move(Z))); - auto surf = ax.plot_surface(Args(X_, Y_, Z_), - Kwargs("rstride"_a = 1, "cstride"_a = 1, - "linewidth"_a = 0, "antialiased"_a = false, - "cmap"_a = cm::coolwarm())); + const auto X_ = py::array(py::cast(std::move(X))); + const auto Y_ = py::array(py::cast(std::move(Y))); + const auto Z_ = py::array(py::cast(std::move(Z))); + const auto surf = ax.plot_surface( + Args(X_, Y_, Z_), + Kwargs("rstride"_a = 1, "cstride"_a = 1, "linewidth"_a = 0, + "antialiased"_a = false, "cmap"_a = cm::coolwarm)); ax.set_zlim(Args(-1.01, 1.01)); // TODO // auto locator = ticker::LinearLocator(Args(10)); // ax.zaxis.set_major_locator(Args(locator.unwrap())); // ax.zaxis.set_major_formatter(Args("{x:.02f}")); - fig.colorbar(Args(surf), Kwargs("shrink"_a = 0.5, "aspect"_a = 5)); + fig.colorbar(Args(surf.unwrap()), Kwargs("shrink"_a = 0.5, "aspect"_a = 5)); plt.show(); } diff --git a/gallery/shapes_and_collections/patch_collection.cpp b/gallery/shapes_and_collections/patch_collection.cpp index ea5be4e..88183e5 100644 --- a/gallery/shapes_and_collections/patch_collection.cpp +++ b/gallery/shapes_and_collections/patch_collection.cpp @@ -17,7 +17,6 @@ int main() { auto plt = matplotlibcpp17::pyplot::import(); auto [fig, ax] = plt.subplots(); - const int resolution = 50; const int N = 3; xt::xarray x = xt::random::rand({N}); xt::xarray y = xt::random::rand({N}); @@ -32,8 +31,8 @@ int main() { x = xt::random::rand({N}); y = xt::random::rand({N}); radii = 0.1 * xt::random::rand({N}); - xt::xarray theta1 = 360.0 * xt::random::rand({N}); - xt::xarray theta2 = 360.0 * xt::random::rand({N}); + const xt::xarray theta1 = 360.0 * xt::random::rand({N}); + const xt::xarray theta2 = 360.0 * xt::random::rand({N}); for (int i = 0; i < N; ++i) { const double x1 = x[i], y1 = y[i], r = radii[i], th1 = theta1[i], th2 = theta2[i]; @@ -68,7 +67,7 @@ int main() { } auto colors__ = 100.0 * xt::random::rand({patches.size()}); - vector colors_(colors__.begin(), colors__.end()); + const vector colors_(colors__.begin(), colors__.end()); py::array colors = py::cast(colors_); auto p = collections::PatchCollection(Args(patches), Kwargs("alpha"_a = 0.4)); p.set_array(Args(colors)); diff --git a/gallery/statistics/errorbar.cpp b/gallery/statistics/errorbar.cpp index 5357a8f..3740b7e 100644 --- a/gallery/statistics/errorbar.cpp +++ b/gallery/statistics/errorbar.cpp @@ -12,9 +12,9 @@ using namespace matplotlibcpp17; int main() { py::scoped_interpreter guard{}; auto plt = pyplot::import(); - auto x_ = xt::arange(0.1, 4.0, 0.5); - auto y_ = xt::exp(-x_); - vector x(x_.begin(), x_.end()), y(y_.begin(), y_.end()); + const auto x_ = xt::arange(0.1, 4.0, 0.5); + const auto y_ = xt::exp(-x_); + const vector x(x_.begin(), x_.end()), y(y_.begin(), y_.end()); auto [fig, ax] = plt.subplots(); ax.errorbar(Args(x, y), Kwargs("xerr"_a = 0.2, "yerr"_a = 0.4)); diff --git a/gallery/statistics/hist.cpp b/gallery/statistics/hist.cpp index 70c790f..73e4f34 100644 --- a/gallery/statistics/hist.cpp +++ b/gallery/statistics/hist.cpp @@ -10,9 +10,9 @@ using namespace matplotlibcpp17; int main1() { int N_points = 100000; int n_bins = 20; - auto dist1_ = xt::random::randn({N_points}); - auto dist2_ = 0.4 * xt::random::randn({N_points}) + 5.0; - vector dist1(dist1_.begin(), dist1_.end()), + const auto dist1_ = xt::random::randn({N_points}); + const auto dist2_ = 0.4 * xt::random::randn({N_points}) + 5.0; + const vector dist1(dist1_.begin(), dist1_.end()), dist2(dist2_.begin(), dist2_.end()); auto plt = matplotlibcpp17::pyplot::import(); auto [fig, axs] = @@ -32,10 +32,10 @@ int main1() { // int main2() // pass int main3() { - int N_points = 100000; - auto dist1_ = xt::random::randn({N_points}); - auto dist2_ = 0.4 * xt::random::randn({N_points}) + 5.0; - vector dist1(dist1_.begin(), dist1_.end()), + const int N_points = 100000; + const auto dist1_ = xt::random::randn({N_points}); + const auto dist2_ = 0.4 * xt::random::randn({N_points}) + 5.0; + const vector dist1(dist1_.begin(), dist1_.end()), dist2(dist2_.begin(), dist2_.end()); auto plt = matplotlibcpp17::pyplot::import(); diff --git a/gallery/subplots_axes_and_figures/align_labels_demo.cpp b/gallery/subplots_axes_and_figures/align_labels_demo.cpp index ac14f54..d910371 100644 --- a/gallery/subplots_axes_and_figures/align_labels_demo.cpp +++ b/gallery/subplots_axes_and_figures/align_labels_demo.cpp @@ -18,16 +18,16 @@ int main() { auto gs = GridSpec(2, 2); // instead of gs[0, :] auto ax = fig.add_subplot(Args(gs(0, py::slice(0, 2, 1)).unwrap())); - auto tmp_ = xt::arange(0, 1000000, 10000); + const auto tmp_ = xt::arange(0, 1000000, 10000); vector tmp(tmp_.begin(), tmp_.end()); ax.plot(Args(tmp)); 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_ = xt::arange(1.0, 0.0, -0.1); - auto xs_ = ys_ * 2000; - vector xs(xs_.begin(), xs_.end()), ys(ys_.begin(), ys_.end()); + const auto ys_ = xt::arange(1.0, 0.0, -0.1); + const auto xs_ = ys_ * 2000; + const vector xs(xs_.begin(), xs_.end()), ys(ys_.begin(), ys_.end()); ax.plot(Args(xs, ys)); ax.set_ylabel(Args(string("YLabel1 " + to_string(i)))); ax.set_xlabel(Args(string("XLabel1 " + to_string(i)))); diff --git a/gallery/subplots_axes_and_figures/colorbar_placement.cpp b/gallery/subplots_axes_and_figures/colorbar_placement.cpp index 9f1e341..a580d3f 100644 --- a/gallery/subplots_axes_and_figures/colorbar_placement.cpp +++ b/gallery/subplots_axes_and_figures/colorbar_placement.cpp @@ -16,7 +16,7 @@ int main1() { const vector cmaps = {"RdBu_r", "viridis"}; for (auto col : {0, 1}) { for (auto row : {0, 1}) { - auto x_ = xt::random::randn({20, 20}) * (col + 1.0); + const auto x_ = xt::random::randn({20, 20}) * (col + 1.0); vector> x(20); for (int i = 0; i < 20; ++i) { x[i].resize(20); diff --git a/gallery/subplots_axes_and_figures/multiple_figs_demo.cpp b/gallery/subplots_axes_and_figures/multiple_figs_demo.cpp index b40a8c0..8dc725f 100644 --- a/gallery/subplots_axes_and_figures/multiple_figs_demo.cpp +++ b/gallery/subplots_axes_and_figures/multiple_figs_demo.cpp @@ -14,9 +14,9 @@ using namespace matplotlibcpp17; int main() { py::scoped_interpreter guard{}; auto plt = matplotlibcpp17::pyplot::import(); - auto x_ = xt::arange(0.0, 2.0, 0.01); - auto s1_ = xt::sin(2 * M_PI * x_); - auto s2_ = xt::sin(4 * M_PI * x_); + const auto x_ = xt::arange(0.0, 2.0, 0.01); + const auto s1_ = xt::sin(2 * M_PI * x_); + const auto s2_ = xt::sin(4 * M_PI * x_); vector x(x_.begin(), x_.end()), s1(s1_.begin(), s1_.end()), s2(s2_.begin(), s2_.end()); plt.figure(Args(1)); diff --git a/gallery/subplots_axes_and_figures/two_scales.cpp b/gallery/subplots_axes_and_figures/two_scales.cpp index b012b03..617a87d 100644 --- a/gallery/subplots_axes_and_figures/two_scales.cpp +++ b/gallery/subplots_axes_and_figures/two_scales.cpp @@ -13,10 +13,10 @@ using namespace matplotlibcpp17; int main() { auto t_ = xt::arange(0.01, 10.0, 0.01); - auto data1_ = xt::exp(t_); - auto data2_ = xt::sin(2 * M_PI * t_); - vector t(t_.begin(), t_.end()), data1(data1_.begin(), data1_.end()), - data2(data2_.begin(), data2_.end()); + const auto data1_ = xt::exp(t_); + const auto data2_ = xt::sin(2 * M_PI * t_); + const vector t(t_.begin(), t_.end()), + data1(data1_.begin(), data1_.end()), data2(data2_.begin(), data2_.end()); py::scoped_interpreter guard{}; auto plt = pyplot::import(); diff --git a/include/matplotlibcpp17/animation.h b/include/matplotlibcpp17/animation.h index 9c0424d..90b9ad4 100644 --- a/include/matplotlibcpp17/animation.h +++ b/include/matplotlibcpp17/animation.h @@ -24,8 +24,8 @@ struct DECL_STRUCT_ATTR ArtistAnimation : public BaseWrapper { load_attrs(); } // save - pybind11::object save(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper save(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); private: void load_attrs() { LOAD_FUNC_ATTR(save, self); } @@ -33,10 +33,10 @@ struct DECL_STRUCT_ATTR ArtistAnimation : public BaseWrapper { }; // save -pybind11::object ArtistAnimation::save(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper ArtistAnimation::save(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = save_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } } // namespace matplotlibcpp17::animation diff --git a/include/matplotlibcpp17/axes.h b/include/matplotlibcpp17/axes.h index 87f6983..f5bd394 100644 --- a/include/matplotlibcpp17/axes.h +++ b/include/matplotlibcpp17/axes.h @@ -31,72 +31,73 @@ using HistType = std::tuple, std::vector, **/ struct DECL_STRUCT_ATTR Axes : public BaseWrapper { public: - Axes(pybind11::object axes) { + Axes(const pybind11::object &axes) { self = axes; load_attrs(); } + Axes(pybind11::object &&axes) { + self = std::move(axes); + load_attrs(); + } // add_artist - pybind11::object add_artist(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper add_artist(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // add_collection - pybind11::object - add_collection(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper add_collection(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // add_patch - pybind11::object add_patch(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper add_patch(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // axhline - pybind11::object axhline(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper axhline(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // bar container::BarContainer bar(const pybind11::tuple &args = pybind11::tuple(), const pybind11::dict &kwargs = pybind11::dict()); // bar_label - pybind11::object bar_label(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper bar_label(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // barh container::BarContainer barh(const pybind11::tuple &args = pybind11::tuple(), const pybind11::dict &kwargs = pybind11::dict()); // contour - pybind11::object contour(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper contour(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // contourf - pybind11::object contourf(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper contourf(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // errorbar - pybind11::object errorbar(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper errorbar(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // fill - pybind11::object fill(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper fill(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // fill_between - pybind11::object - fill_between(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper fill_between(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // fill_betweenx - pybind11::object - fill_betweenx(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper fill_betweenx(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // get_lines - pybind11::object get_lines(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper get_lines(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // get_xaxis_transform - pybind11::object + ObjectWrapper get_xaxis_transform(const pybind11::tuple &args = pybind11::tuple(), const pybind11::dict &kwargs = pybind11::dict()); @@ -107,25 +108,24 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper { std::vector get_xticklabels(); // grid - pybind11::object grid(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper grid(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // hist HistType hist(const pybind11::tuple &args = pybind11::tuple(), const pybind11::dict &kwargs = pybind11::dict()); // hist2d - pybind11::object hist2d(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper hist2d(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // imshow - pybind11::object imshow(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper imshow(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // invert_yaxis - pybind11::object - invert_yaxis(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper invert_yaxis(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // legend legend::Legend legend(const pybind11::tuple &args = pybind11::tuple(), @@ -137,18 +137,16 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper { const pybind11::dict &kwargs = pybind11::dict()); // plot - pybind11::object plot(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper plot(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // plot_surface - pybind11::object - plot_surface(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper plot_surface(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // plot_wireframe - pybind11::object - plot_wireframe(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper plot_wireframe(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // quiver quiver::Quiver quiver(const pybind11::tuple &args = pybind11::tuple(), @@ -164,74 +162,73 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper { const pybind11::dict &kwargs = pybind11::dict()); // set - pybind11::object set(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper set(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // set_adjustable - pybind11::object - set_adjustable(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper set_adjustable(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // set_aspect - pybind11::object set_aspect(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper set_aspect(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // set_title - pybind11::object set_title(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper set_title(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // set_xlabel - pybind11::object set_xlabel(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper set_xlabel(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // set_xlim - pybind11::object set_xlim(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper set_xlim(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // set_xscale - pybind11::object set_xscale(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper set_xscale(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // set_xticks - pybind11::object set_xticks(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper set_xticks(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // set_xticklabels - pybind11::object + ObjectWrapper 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()); + ObjectWrapper set_ylabel(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // set_ylim - pybind11::object set_ylim(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper set_ylim(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // set_yscale - pybind11::object set_yscale(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper set_yscale(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // set_yticks - pybind11::object set_yticks(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper set_yticks(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // set_zlabel - pybind11::object set_zlabel(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper set_zlabel(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // set_zlim - pybind11::object set_zlim(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper set_zlim(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // text - pybind11::object text(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper text(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // tick_params - pybind11::object tick_params(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper tick_params(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // twinx Axes twinx(const pybind11::tuple &args = pybind11::tuple(), @@ -239,6 +236,18 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper { private: void load_attrs() { + // NOTE: only when called with projection='3d', `plot_surface`, + // `plot_wireframe`, `set_zlabel` prop exists. + try { + LOAD_FUNC_ATTR(plot_surface, self); + LOAD_FUNC_ATTR(plot_wireframe, self); + LOAD_FUNC_ATTR(set_zlabel, self); + LOAD_FUNC_ATTR(set_zlim, self); + INFO_MSG("Loaded Axes3D"); + projection_3d = true; + } catch (...) { + projection_3d = false; + } LOAD_FUNC_ATTR(add_artist, self); LOAD_FUNC_ATTR(add_collection, self); LOAD_FUNC_ATTR(add_patch, self); @@ -268,18 +277,6 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper { LOAD_FUNC_ATTR(legend, self); LOAD_FUNC_ATTR(pcolormesh, self); LOAD_FUNC_ATTR(plot, self); - // NOTE: only when called with projection='3d', `plot_surface`, - // `plot_wireframe`, `set_zlabel` prop exists. - try { - LOAD_FUNC_ATTR(plot_surface, self); - LOAD_FUNC_ATTR(plot_wireframe, self); - LOAD_FUNC_ATTR(set_zlabel, self); - LOAD_FUNC_ATTR(set_zlim, self); - INFO_MSG("Loaded Axes3D"); - projection_3d = true; - } catch (...) { - projection_3d = false; - } LOAD_FUNC_ATTR(quiver, self); LOAD_FUNC_ATTR(quiverkey, self); LOAD_FUNC_ATTR(scatter, self); @@ -348,35 +345,36 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper { pybind11::object text_attr; pybind11::object tick_params_attr; pybind11::object twinx_attr; + // 3d bool projection_3d; }; // add_artist -pybind11::object Axes::add_artist(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::add_artist(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = add_artist_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // add_collection -pybind11::object Axes::add_collection(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::add_collection(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = add_collection_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // add_patch -pybind11::object Axes::add_patch(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::add_patch(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = add_patch_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // axhline -pybind11::object Axes::axhline(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::axhline(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = axhline_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // bar @@ -387,11 +385,11 @@ container::BarContainer Axes::bar(const pybind11::tuple &args, } // bar_label -pybind11::object Axes::bar_label(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::bar_label(const pybind11::tuple &args, + const pybind11::dict &kwargs) { #if MATPLOTLIB_MINOR_VER_GTE_4 pybind11::object ret = bar_label_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); #else ERROR_MSG( "Call to bar_label is invalid because matplotlib version is < 3.4.0"); @@ -407,22 +405,22 @@ container::BarContainer Axes::barh(const pybind11::tuple &args, } // contour -pybind11::object Axes::contour(const pybind11::tuple &args, - const pybind11::dict &kwargs) { - pybind11::object obj = contour_attr(*args, **kwargs); - return obj; +ObjectWrapper Axes::contour(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = contour_attr(*args, **kwargs); + return ObjectWrapper(std::move(ret)); } // contourf -pybind11::object Axes::contourf(const pybind11::tuple &args, - const pybind11::dict &kwargs) { - pybind11::object obj = contourf_attr(*args, **kwargs); - return obj; +ObjectWrapper Axes::contourf(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = contourf_attr(*args, **kwargs); + return ObjectWrapper(std::move(ret)); } // errorbar -pybind11::object Axes::errorbar(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::errorbar(const pybind11::tuple &args, + const pybind11::dict &kwargs) { if (projection_3d) { #if MATPLOTLIB_MINOR_VER_GTE_4 pybind11::object obj = errorbar_attr(*args, **kwargs); @@ -433,44 +431,44 @@ pybind11::object Axes::errorbar(const pybind11::tuple &args, std::exit(0); #endif } else { - pybind11::object obj = errorbar_attr(*args, **kwargs); - return obj; + pybind11::object ret = errorbar_attr(*args, **kwargs); + return ObjectWrapper(std::move(ret)); } } // fill -pybind11::object Axes::fill(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::fill(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = fill_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // fill_between -pybind11::object Axes::fill_between(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::fill_between(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = fill_between_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // fill_betweenx -pybind11::object Axes::fill_betweenx(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::fill_betweenx(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = fill_betweenx_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // get_lines -pybind11::object Axes::get_lines(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::get_lines(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = get_lines_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // get_xaxis_transform -pybind11::object Axes::get_xaxis_transform(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::get_xaxis_transform(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = get_xaxis_transform_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // get_xlim @@ -492,10 +490,10 @@ std::vector Axes::get_xticklabels() { } // grid -pybind11::object Axes::grid(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::grid(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = grid_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // hist @@ -516,24 +514,24 @@ Axes::hist(const pybind11::tuple &args, const pybind11::dict &kwargs) { } // hist2d -pybind11::object Axes::hist2d(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::hist2d(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = hist2d_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // invert_yaxis -pybind11::object Axes::invert_yaxis(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::invert_yaxis(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = invert_yaxis_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // imshow -pybind11::object Axes::imshow(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::imshow(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = imshow_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // legend @@ -551,24 +549,24 @@ collections::QuadMesh Axes::pcolormesh(const pybind11::tuple &args, } // plot -pybind11::object Axes::plot(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::plot(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = plot_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // plot_surface -pybind11::object Axes::plot_surface(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::plot_surface(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = plot_surface_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // plot_wireframe -pybind11::object Axes::plot_wireframe(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::plot_wireframe(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = plot_wireframe_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // quiver @@ -593,122 +591,122 @@ collections::PathCollection Axes::scatter(const pybind11::tuple &args, } // set -pybind11::object Axes::set(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::set(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = set_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // set_adjustable -pybind11::object Axes::set_adjustable(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::set_adjustable(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = set_adjustable_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // set_aspect -pybind11::object Axes::set_aspect(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::set_aspect(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = set_aspect_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // set_title -pybind11::object Axes::set_title(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::set_title(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = set_title_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // set_xlabel -pybind11::object Axes::set_xlabel(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::set_xlabel(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = set_xlabel_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // set_xlim -pybind11::object Axes::set_xlim(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::set_xlim(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = set_xlim_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // set_xscale -pybind11::object Axes::set_xscale(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::set_xscale(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = set_xscale_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // set_xticks -pybind11::object Axes::set_xticks(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::set_xticks(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = set_xticks_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // set_xticklabels -pybind11::object Axes::set_xticklabels(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::set_xticklabels(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = set_xticklabels_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // set_ylabel -pybind11::object Axes::set_ylabel(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::set_ylabel(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = set_ylabel_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // set_ylim -pybind11::object Axes::set_ylim(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::set_ylim(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = set_ylim_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // set_yscale -pybind11::object Axes::set_yscale(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::set_yscale(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = set_yscale_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // set_yticks -pybind11::object Axes::set_yticks(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::set_yticks(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = set_yticks_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // set_zlabel -pybind11::object Axes::set_zlabel(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::set_zlabel(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = set_zlabel_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // set_zlim -pybind11::object Axes::set_zlim(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::set_zlim(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = set_zlim_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // text -pybind11::object Axes::text(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::text(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = text_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // tick_params -pybind11::object Axes::tick_params(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Axes::tick_params(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = tick_params_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // twinx diff --git a/include/matplotlibcpp17/cm.h b/include/matplotlibcpp17/cm.h index 3d7383b..cc6869a 100644 --- a/include/matplotlibcpp17/cm.h +++ b/include/matplotlibcpp17/cm.h @@ -10,12 +10,7 @@ namespace matplotlibcpp17::cm { -pybind11::object coolwarm() { - pybind11::object ret = - pybind11::module::import("matplotlib.cm").attr("coolwarm"); - return ret; -} - +static const char *coolwarm = "coolwarm"; static const char *PuBu_r = "PuBu_r"; static const char *RdYlGn = "RdYlGn"; diff --git a/include/matplotlibcpp17/collections.h b/include/matplotlibcpp17/collections.h index e9de566..168f6b1 100644 --- a/include/matplotlibcpp17/collections.h +++ b/include/matplotlibcpp17/collections.h @@ -19,13 +19,17 @@ namespace matplotlibcpp17::collections { **/ struct DECL_STRUCT_ATTR PathCollection : public BaseWrapper { public: - PathCollection(pybind11::object pathcollection) { + PathCollection(const pybind11::object &pathcollection) { self = pathcollection; load_attrs(); } + PathCollection(pybind11::object &&pathcollection) { + self = std::move(pathcollection); + load_attrs(); + } // legend_elements - std::pair + std::pair legend_elements(const pybind11::tuple &args = pybind11::tuple(), const pybind11::dict &kwargs = pybind11::dict()); @@ -37,13 +41,13 @@ struct DECL_STRUCT_ATTR PathCollection : public BaseWrapper { // legend_elements /// NOTE: this func does not return list of Line2Ds(handles) and list of /// strs(labels) unlike original python func -std::pair +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]; - return {handles, labels}; + return {ObjectWrapper(std::move(handles)), ObjectWrapper(std::move(labels))}; } /** @@ -60,18 +64,18 @@ struct DECL_STRUCT_ATTR PatchCollection : public BaseWrapper { } // set_array - pybind11::object set_array(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper 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 set_array_attr; }; -pybind11::object PatchCollection::set_array(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PatchCollection::set_array(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = set_array_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } /** @@ -79,7 +83,8 @@ pybind11::object PatchCollection::set_array(const pybind11::tuple &args, **/ struct DECL_STRUCT_ATTR QuadMesh : public BaseWrapper { public: - QuadMesh(pybind11::object quadmesh) { self = quadmesh; } + QuadMesh(const pybind11::object &quadmesh) { self = quadmesh; } + QuadMesh(pybind11::object &&quadmesh) { self = std::move(quadmesh); } }; } // namespace matplotlibcpp17::collections diff --git a/include/matplotlibcpp17/common.h b/include/matplotlibcpp17/common.h index a1029f7..0358397 100644 --- a/include/matplotlibcpp17/common.h +++ b/include/matplotlibcpp17/common.h @@ -9,6 +9,7 @@ #define DECL_STRUCT_ATTR __attribute__((visibility("hidden"))) #include +#include #define INFO_MSG(msg) \ do { \ @@ -37,12 +38,21 @@ namespace matplotlibcpp17 { **/ struct DECL_STRUCT_ATTR BaseWrapper { public: - pybind11::object unwrap() { return self; } + pybind11::object unwrap() const { return self; } protected: pybind11::object self; }; +/** + * @brief A proxy class for pybind object + **/ +struct ObjectWrapper : public BaseWrapper { +public: + ObjectWrapper(const pybind11::object &object) { self = object; } + ObjectWrapper(pybind11::object &&object) { self = std::move(object); } +}; + } // namespace matplotlibcpp17 #endif /* MATPLOTLIBCPP17_COMMON_H */ diff --git a/include/matplotlibcpp17/container.h b/include/matplotlibcpp17/container.h index a795365..70e2043 100644 --- a/include/matplotlibcpp17/container.h +++ b/include/matplotlibcpp17/container.h @@ -17,7 +17,10 @@ namespace matplotlibcpp17::container { **/ struct DECL_STRUCT_ATTR BarContainer : public BaseWrapper { public: - BarContainer(pybind11::object bar_container) { self = bar_container; } + BarContainer(const pybind11::object &bar_container) { self = bar_container; } + BarContainer(pybind11::object &&bar_container) { + self = std::move(bar_container); + } }; } // namespace matplotlibcpp17::container diff --git a/include/matplotlibcpp17/figure.h b/include/matplotlibcpp17/figure.h index 7135102..3d3aa3b 100644 --- a/include/matplotlibcpp17/figure.h +++ b/include/matplotlibcpp17/figure.h @@ -19,11 +19,14 @@ namespace matplotlibcpp17::figure { **/ struct DECL_STRUCT_ATTR Figure : public BaseWrapper { public: - Figure(pybind11::object figure) { + Figure(const pybind11::object &figure) { self = figure; load_attrs(); } - + Figure(pybind11::object &&figure) { + self = std::move(figure); + load_attrs(); + } // add_axes axes::Axes add_axes(const pybind11::tuple &args = pybind11::tuple(), const pybind11::dict &kwargs = pybind11::dict()); @@ -38,26 +41,24 @@ struct DECL_STRUCT_ATTR Figure : public BaseWrapper { const pybind11::dict &kwargs = pybind11::dict()); // align_labels - pybind11::object - align_labels(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper align_labels(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // colorbar - pybind11::object colorbar(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper colorbar(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // savefig - pybind11::object savefig(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper savefig(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // suptitle - pybind11::object suptitle(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper suptitle(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // tight_layout - pybind11::object - tight_layout(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper tight_layout(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); private: void load_attrs() { @@ -101,38 +102,38 @@ axes::Axes Figure::add_subplot(const pybind11::tuple &args, } // align_labels -pybind11::object Figure::align_labels(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Figure::align_labels(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = align_labels_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // colorbar -pybind11::object Figure::colorbar(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Figure::colorbar(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = colorbar_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // savefig -pybind11::object Figure::savefig(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Figure::savefig(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = savefig_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // suptitle -pybind11::object Figure::suptitle(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Figure::suptitle(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = suptitle_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // tight_layout -pybind11::object Figure::tight_layout(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Figure::tight_layout(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = tight_layout_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } } // namespace matplotlibcpp17::figure diff --git a/include/matplotlibcpp17/gridspec.h b/include/matplotlibcpp17/gridspec.h index 3a9844a..24f450b 100644 --- a/include/matplotlibcpp17/gridspec.h +++ b/include/matplotlibcpp17/gridspec.h @@ -17,7 +17,8 @@ namespace matplotlibcpp17::gridspec { **/ struct DECL_STRUCT_ATTR SubplotSpec : public BaseWrapper { public: - SubplotSpec(pybind11::object subplotspec) { self = subplotspec; } + SubplotSpec(const pybind11::object &subplotspec) { self = subplotspec; } + SubplotSpec(pybind11::object &&subplotspec) { self = std::move(subplotspec); } }; /** @@ -36,7 +37,7 @@ struct DECL_STRUCT_ATTR GridSpec : public BaseWrapper { template SubplotSpec operator()(const Rows &r, const Cols &c) { pybind11::object obj = self[pybind11::make_tuple(r, c)]; - return SubplotSpec(obj); + return SubplotSpec(std::move(obj)); } private: diff --git a/include/matplotlibcpp17/legend.h b/include/matplotlibcpp17/legend.h index 6256a64..bf3b027 100644 --- a/include/matplotlibcpp17/legend.h +++ b/include/matplotlibcpp17/legend.h @@ -17,9 +17,8 @@ namespace matplotlibcpp17::legend { **/ struct DECL_STRUCT_ATTR Legend : public BaseWrapper { public: - Legend(pybind11::object obj) { - self = obj; - } + Legend(const pybind11::object &obj) { self = obj; } + Legend(pybind11::object &&obj) { self = std::move(obj); } }; } // namespace matplotlibcpp17::legend diff --git a/include/matplotlibcpp17/mplot3d.h b/include/matplotlibcpp17/mplot3d.h new file mode 100644 index 0000000..0aa922d --- /dev/null +++ b/include/matplotlibcpp17/mplot3d.h @@ -0,0 +1,18 @@ +/** + * @file mplot3d.h + * @brief header file for mplot3d + **/ + +#ifndef MATPLOTLIBCPP17_MPLOT3D_H +#define MATPLOTLIBCPP17_MPLOT3D_H + +#include + +namespace matplotlibcpp17::mplot3d { + +pybind11::object import() { + return pybind11::module::import("mpl_toolkits.mplot3d").attr("Axes3D"); +} + +} // namespace matplotlibcpp17::mplot3d +#endif diff --git a/include/matplotlibcpp17/pyplot.h b/include/matplotlibcpp17/pyplot.h index 7fa4ca0..71e69ff 100644 --- a/include/matplotlibcpp17/pyplot.h +++ b/include/matplotlibcpp17/pyplot.h @@ -33,28 +33,28 @@ struct DECL_STRUCT_ATTR PyPlot { axes::Axes axes(const pybind11::dict &kwargs = pybind11::dict()); // axis - pybind11::object axis(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper axis(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // cla - pybind11::object cla(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper cla(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // clf - pybind11::object clf(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper clf(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // clim - pybind11::object clim(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper clim(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // colorbar - pybind11::object colorbar(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper colorbar(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // errorbar - pybind11::object errorbar(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper errorbar(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // figaspect std::tuple @@ -74,48 +74,48 @@ struct DECL_STRUCT_ATTR PyPlot { const pybind11::dict &kwargs = pybind11::dict()); // gci - pybind11::object gci(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper gci(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // grid - pybind11::object grid(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper grid(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // imshow - pybind11::object imshow(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper imshow(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // legend - pybind11::object legend(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper legend(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // pause - pybind11::object pause(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper pause(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // plot - pybind11::object plot(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper plot(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // quiver - pybind11::object quiver(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper quiver(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // savefig - pybind11::object savefig(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper savefig(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // scatter - pybind11::object scatter(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper scatter(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // show - pybind11::object show(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper show(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // step - pybind11::object step(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper step(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // subplot axes::Axes subplot(const pybind11::dict &kwargs = pybind11::dict()); @@ -128,24 +128,24 @@ struct DECL_STRUCT_ATTR PyPlot { subplots(int r, int c, const pybind11::dict &kwargs = pybind11::dict()); // title - pybind11::object title(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper title(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // xlabel - pybind11::object xlabel(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper xlabel(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // xlim - pybind11::object xlim(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper xlim(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // ylabel - pybind11::object ylabel(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper ylabel(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // ylim - pybind11::object ylim(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper ylim(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); private: void load_attrs() { @@ -218,45 +218,45 @@ axes::Axes PyPlot::axes(const pybind11::dict &kwargs) { } // axis -pybind11::object PyPlot::axis(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::axis(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = axis_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // cla -pybind11::object PyPlot::cla(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::cla(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = cla_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // clf -pybind11::object PyPlot::clf(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::clf(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = clf_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // clim -pybind11::object PyPlot::clim(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::clim(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = clim_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // colorbar -pybind11::object PyPlot::colorbar(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::colorbar(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = colorbar_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // errorbar -pybind11::object PyPlot::errorbar(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::errorbar(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = errorbar_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // figaspect @@ -290,80 +290,80 @@ figure::Figure PyPlot::gcf(const pybind11::tuple &args, } // gci -pybind11::object PyPlot::gci(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::gci(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = gci_attr(*args, **kwargs); return obj; } // grid -pybind11::object PyPlot::grid(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::grid(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = grid_attr(*args, **kwargs); return obj; } // imshow -pybind11::object PyPlot::imshow(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::imshow(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = imshow_attr(*args, **kwargs); return obj; } // legend -pybind11::object PyPlot::legend(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::legend(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = legend_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // pause -pybind11::object PyPlot::pause(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::pause(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = pause_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // plot -pybind11::object PyPlot::plot(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::plot(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = plot_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // quiver -pybind11::object PyPlot::quiver(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::quiver(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = quiver_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // scatter -pybind11::object PyPlot::scatter(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::scatter(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = scatter_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // savefig -pybind11::object PyPlot::savefig(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::savefig(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = savefig_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // show -pybind11::object PyPlot::show(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::show(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = show_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // step -pybind11::object PyPlot::step(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::step(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = step_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // subplot @@ -416,38 +416,38 @@ PyPlot::subplots(int r, int c, const pybind11::dict &kwargs) { } // title -pybind11::object PyPlot::title(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::title(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = title_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // xlabel -pybind11::object PyPlot::xlabel(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::xlabel(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = xlabel_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // xlim -pybind11::object PyPlot::xlim(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::xlim(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = xlim_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // ylabel -pybind11::object PyPlot::ylabel(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::ylabel(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = ylabel_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // ylim -pybind11::object PyPlot::ylim(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper PyPlot::ylim(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = ylim_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } PyPlot import() { @@ -459,7 +459,7 @@ PyPlot import() { } // namespace matplotlibcpp17::pyplot // Args & Kwargs -template pybind11::tuple Args(ArgsT &&... args) { +template pybind11::tuple Args(ArgsT &&...args) { return pybind11::make_tuple(std::forward(args)...); } diff --git a/include/matplotlibcpp17/quiver.h b/include/matplotlibcpp17/quiver.h index 5ba4462..3c1a778 100644 --- a/include/matplotlibcpp17/quiver.h +++ b/include/matplotlibcpp17/quiver.h @@ -17,14 +17,16 @@ namespace matplotlibcpp17::quiver { **/ struct DECL_STRUCT_ATTR Quiver : public BaseWrapper { public: - Quiver(pybind11::object q) { self = q; } + Quiver(const pybind11::object &q) { self = q; } + Quiver(pybind11::object &&q) { self = std::move(q); } }; /** * @brief A wrapper class for matplotlib.quiver.QuiverKey **/ struct DECL_STRUCT_ATTR QuiverKey : public BaseWrapper { - QuiverKey(pybind11::object qk) { self = qk; } + QuiverKey(const pybind11::object &qk) { self = qk; } + QuiverKey(pybind11::object &&qk) { self = std::move(qk); } }; } // namespace matplotlibcpp17::quiver diff --git a/include/matplotlibcpp17/text.h b/include/matplotlibcpp17/text.h index 16ccec5..2834402 100644 --- a/include/matplotlibcpp17/text.h +++ b/include/matplotlibcpp17/text.h @@ -17,24 +17,27 @@ namespace matplotlibcpp17::text { struct DECL_STRUCT_ATTR Text : public BaseWrapper { public: - Text(pybind11::object text) { + Text(const pybind11::object &text) { self = text; load_attrs(); } + Text(pybind11::object &&text) { + self = std::move(text); + load_attrs(); + } - pybind11::object - set_rotation(const pybind11::tuple &args = pybind11::tuple(), - const pybind11::dict &kwargs = pybind11::dict()); + ObjectWrapper 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 set_rotation_attr; }; -pybind11::object Text::set_rotation(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +ObjectWrapper Text::set_rotation(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = set_rotation_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } } // namespace matplotlibcpp17::text