diff --git a/CMakeLists.txt b/CMakeLists.txt index de4cc9e..5c46a8d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,12 +33,6 @@ else() set(MATPLOTLIB_MINOR_VER_GTE_4 1) endif() -target_include_directories( - ${PROJECT_NAME} - INTERFACE $ - $) -target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_17) - # ############################################################################## # (2) for add_subdirectory # ############################################################################## @@ -89,6 +83,12 @@ set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE") # create .deb include("${PROJECT_SOURCE_DIR}/cmake/package.cmake") +target_include_directories( + ${PROJECT_NAME} + INTERFACE $ + $) +target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_17) + # ############################################################################## # (4) uninstall # ############################################################################## @@ -125,9 +125,7 @@ endif() function(add_demo name path) add_executable(${name} ${path}) - target_include_directories(${name} PUBLIC ${Python3_INCLUDE_DIRS} - ${matplotlibcpp17_INCLUDE_DIRS}) - target_link_libraries(${name} ${Python3_LIBRARIES} pybind11::embed xtensor) + target_link_libraries(${name} xtensor matplotlibcpp17::matplotlibcpp17) endfunction() if(${ADD_DEMO}) diff --git a/README.md b/README.md index 011c669..51ca868 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ descibed in [minimal example](#minimal-example). ## Syntax -The user will need to capsulate *arguments* in `Args(arg1, arg2, ...) == pybind11:tuple` and *keyword arguments* in `Kwargs("k1"_a = v1, "k2"_a = v2, ...) == pybind11::dict`. The returned value is either a `pybind11::object` or a corresponding wrapper class. Please refer to the reference and examples below. +The user will need to capsulate *arguments* in `Args(arg1, arg2, ...) == pybind11:tuple` and *keyword arguments* in `Kwargs("k1"_a = v1, "k2"_a = v2, ...) == pybind11::dict`. The returned value is a wrapper class for pybind. Please refer to the reference and examples below. - exception: `subplots`, `TBD`s - conversion: Wrapper class of matplotlibcpp17 like [::container::BarContainer](https://github.com/soblin/matplotlibcpp17/blob/master/include/matplotlibcpp17/container.h) needs to be passed to python interpreter using `unwrap()` method in *args* and *kwargs*. @@ -68,7 +68,7 @@ gives ![minimal example](./gallery/images/hello_world.png) -### example1 - subplots +### subplots From [gallery/subplots_axes_and_figures/align_labels_demo.cpp](https://github.com/soblin/matplotlibcpp17/blob/master/gallery/subplots_axes_and_figures/align_labels_demo.cpp). @@ -91,7 +91,7 @@ From [gallery/subplots_axes_and_figures/align_labels_demo.cpp](https://github.co ![subplots_axes_and_figures](./gallery/images/align_labels_demo.png) -### example2 - bar plot +### bar plot From [gallery/lines_bars_and_markers/bar_label_demo.cpp](https://github.com/soblin/matplotlibcpp17/blob/master/gallery/lines_bars_and_markers/bar_label_demo.cpp). Here `subplots()` returns `tuple`. @@ -120,7 +120,26 @@ From [gallery/lines_bars_and_markers/bar_label_demo.cpp](https://github.com/sobl ![bar_label_demo1](./gallery/images/bar_label_demo1.png) -### example3 - fill +### image + +2D-style pybind11 array can be plotted as an image using `imshow()` function. + +From [images_contours_and_fields/image_demo](https://github.com/soblin/matplotlibcpp17/blob/master/gallery/images_contours_and_fields/image_demo.cpp) + +- [original python code](https://matplotlib.org/stable/gallery/images_contours_and_fields/image_demo.html) + +```cpp + vector> Z2D{...}; + auto Zpy = py::array(py::cast(std::move(Z2D))); + ax.imshow(Args(Zpy), Kwargs("interpolation"_a = "bilinear", + "cmap"_a = "RdYlGn", "origin"_a = "lower", + "extent"_a = py::make_tuple(-3, 3, -3, 3), + "vmax"_a = vmax, "vmin"_a = vmin)); +``` + +![image_demo](./gallery/images/image_demo.png) + +### fill Fucntions like `subplots`, `TBD`s are overloaded because they return different types depending on the arguments. Here `subplots()` returns `tuple>`. @@ -138,7 +157,7 @@ From [gallery/lines_bars_and_markers](https://github.com/soblin/matplotlibcpp17/ ![fill](./gallery/images/fill.png) -### example4 - quiver +### quiver Use `.unwrap()` method to pass wrapper class of matplotlibcpp17 to plotting functions. @@ -161,7 +180,17 @@ From [gallery/images_contours_and_fields/quiver_demo.cpp](https://github.com/sob ![quiver_demo3](./gallery/images/quiver_demo_3.png) -### example5 - gif +### 3D + +To plot 3D graph with `projection = "3d"`, following code is required. + +```cpp +#include + +matplotlibcpp17::mplot3d::import(); +``` + +### gif Currently only `ArtistAnimation` is supported. `FuncAnimation` interface maybe implemented in the future. 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/contrib/sombrero.cpp b/gallery/contrib/sombrero.cpp new file mode 100644 index 0000000..c7c5b00 --- /dev/null +++ b/gallery/contrib/sombrero.cpp @@ -0,0 +1,36 @@ +#include +#include + +int main() { + pybind11::scoped_interpreter guard{}; + + const int size = 1000; + const double sigma = 100; + + const auto i = nc::arange(size) - (0.5 * size); + const auto [x, y] = nc::meshgrid(i, i); + + const auto xy = + (nc::power(x, 2) + nc::power(y, 2)) / (-2.0 * nc::power(sigma, 2)); + + const auto sombrero = + nc::exp(xy) * (xy + 1.0) / (std::acos(-1.0) * nc::power(sigma, 4)); + const auto pysombrero = nc::pybindInterface::nc2pybind(sombrero); + + const auto min = nc::min(sombrero)[0]; + const auto max = nc::max(sombrero)[0]; + + auto plt = matplotlibcpp17::pyplot::import(); + + plt.imshow(Args(pysombrero), + Kwargs("extent"_a = pybind11::make_tuple(-1, +1, -1, +1), + "cmap"_a = "inferno")); + + plt.colorbar(); + + plt.clim(pybind11::make_tuple(min * 0.8, max * 0.8)); + + plt.show(); + + return 0; +} \ No newline at end of file diff --git a/gallery/contrib/sombrero.png b/gallery/contrib/sombrero.png new file mode 100644 index 0000000..16c7610 Binary files /dev/null and b/gallery/contrib/sombrero.png differ diff --git a/gallery/images/image_demo.png b/gallery/images/image_demo.png new file mode 100644 index 0000000..c1e6e1c Binary files /dev/null and b/gallery/images/image_demo.png differ diff --git a/gallery/images_contours_and_fields/CMakeLists.txt b/gallery/images_contours_and_fields/CMakeLists.txt index 56ce421..089c85b 100644 --- a/gallery/images_contours_and_fields/CMakeLists.txt +++ b/gallery/images_contours_and_fields/CMakeLists.txt @@ -1,10 +1,12 @@ add_demo(quiver_demo quiver_demo.cpp) add_demo(contourf_log contourf_log) +add_demo(image_demo image_demo) add_custom_target( images_contours_and_fields - DEPENDS quiver_demo contourf_log + DEPENDS quiver_demo contourf_log image_demo COMMAND quiver_demo COMMAND contourf_log + COMMAND image_demo COMMENT "running images_contours_and_fields" WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../images") diff --git a/gallery/images_contours_and_fields/contourf_log.cpp b/gallery/images_contours_and_fields/contourf_log.cpp index f90323a..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)); + "cmap"_a = cm::PuBu_r)); + 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 new file mode 100644 index 0000000..c63cfc7 --- /dev/null +++ b/gallery/images_contours_and_fields/image_demo.cpp @@ -0,0 +1,56 @@ +// example from +// https://matplotlib.org/stable/gallery/images_contours_and_fields/image_demo.html + +#include +#include + +#include +#include + +#include +#include +#include + +using namespace std; +using namespace matplotlibcpp17; + +using mesh2D = vector>; + +int main() { + const double delta = 0.025; + const auto x = xt::arange(-3.0, 3.0, delta); + 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()), + Z1(Z1_.begin(), Z1_.end()), Z2(Z2_.begin(), Z2_.end()); + // to vector + const int xsz = x.shape()[0], ysz = x.shape()[0]; + mesh2D Z2D(xsz); + for (int i = 0; i < xsz; ++i) { + Z2D[i].resize(ysz); + for (int j = 0; j < ysz; ++j) { + Z2D[i][j] = Z_(i, j); + } + } + + py::scoped_interpreter guard{}; + auto plt = matplotlibcpp17::pyplot::import(); + auto [fig, ax] = plt.subplots(); + const double vmax = *max_element(Z_.begin(), Z_.end()), + vmin = *min_element(Z_.begin(), Z_.end()); + 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), + "vmax"_a = vmax, "vmin"_a = vmin)); +#if USE_GUI + plt.show(); +#else + plt.savefig(Args("image_demo.png")); +#endif + return 0; +} 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/gallery/tests/test_shared_lib/CMakeLists.txt b/gallery/tests/test_shared_lib/CMakeLists.txt index 925d2dc..d677480 100644 --- a/gallery/tests/test_shared_lib/CMakeLists.txt +++ b/gallery/tests/test_shared_lib/CMakeLists.txt @@ -1,10 +1,7 @@ # create library add_library(test_shared_lib SHARED test_lib.cpp) target_compile_options(test_shared_lib PUBLIC "-fPIC") -target_include_directories( - test_shared_lib PUBLIC ${Python3_INCLUDE_DIRS} - ${matplotlibcpp17_INCLUDE_DIRS}) -target_link_libraries(test_shared_lib ${Python3_LIBRARIES} pybind11::embed) +target_link_libraries(test_shared_lib matplotlibcpp17::matplotlibcpp17) # link it add_executable(test_lib_main test_lib_main.cpp) target_link_libraries(test_lib_main test_shared_lib) diff --git a/gallery/tests/test_static_lib/CMakeLists.txt b/gallery/tests/test_static_lib/CMakeLists.txt index 3955dfe..cd128e0 100644 --- a/gallery/tests/test_static_lib/CMakeLists.txt +++ b/gallery/tests/test_static_lib/CMakeLists.txt @@ -1,10 +1,7 @@ # create library add_library(test_static_lib STATIC test_lib.cpp) target_compile_options(test_static_lib PUBLIC "-fPIC") -target_include_directories( - test_static_lib PUBLIC ${Python3_INCLUDE_DIRS} - ${matplotlibcpp17_INCLUDE_DIRS}) -target_link_libraries(test_static_lib ${Python3_LIBRARIES} pybind11::embed) +target_link_libraries(test_static_lib matplotlibcpp17::matplotlibcpp17) # link it add_executable(test_lib_static_main test_lib_main.cpp) target_link_libraries(test_lib_static_main test_static_lib) diff --git a/include/matplotlibcpp17/animation.h b/include/matplotlibcpp17/animation.h index 9c0424d..18c557d 100644 --- a/include/matplotlibcpp17/animation.h +++ b/include/matplotlibcpp17/animation.h @@ -2,9 +2,7 @@ * @file animation.h * @brief corresponding header for matplotlib.animation **/ - -#ifndef MATPLOTLIBCPP17_ANIMATION_H -#define MATPLOTLIBCPP17_ANIMATION_H +#pragma once #include @@ -24,8 +22,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,12 +31,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 - -#endif /* MATPLOTLIBCPP17_ANIMATION_H */ diff --git a/include/matplotlibcpp17/axes.h b/include/matplotlibcpp17/axes.h index 0970c3b..07ecf3a 100644 --- a/include/matplotlibcpp17/axes.h +++ b/include/matplotlibcpp17/axes.h @@ -2,15 +2,13 @@ * @file axes.h * @brief corresponding header for matplotlib.axes **/ +#pragma once -#ifndef MATPLOTLIBCPP17_AXES_H -#define MATPLOTLIBCPP17_AXES_H - -#include #include +#include -#include #include +#include #include #include #include @@ -31,72 +29,80 @@ 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()); + // axvline + ObjectWrapper axvline(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()); + // cla + ObjectWrapper cla(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,21 +113,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 + 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(), @@ -133,18 +142,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(), @@ -160,74 +167,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(), @@ -235,10 +241,23 @@ 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); LOAD_FUNC_ATTR(axhline, self); + LOAD_FUNC_ATTR(axvline, self); LOAD_FUNC_ATTR(bar, self); #if MATPLOTLIB_MINOR_VER_GTE_4 LOAD_FUNC_ATTR(bar_label, self); @@ -246,6 +265,7 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper { WARN_MSG("Not loading bar_label because matplotlib version is < 3.4.0"); #endif LOAD_FUNC_ATTR(barh, self); + LOAD_FUNC_ATTR(cla, self); LOAD_FUNC_ATTR(contour, self); LOAD_FUNC_ATTR(contourf, self); LOAD_FUNC_ATTR(errorbar, self); @@ -260,20 +280,10 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper { LOAD_FUNC_ATTR(hist, self); LOAD_FUNC_ATTR(hist2d, self); LOAD_FUNC_ATTR(invert_yaxis, self); + LOAD_FUNC_ATTR(imshow, self); 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); @@ -298,9 +308,11 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper { pybind11::object add_collection_attr; pybind11::object add_patch_attr; pybind11::object axhline_attr; + pybind11::object axvline_attr; pybind11::object bar_attr; pybind11::object bar_label_attr; pybind11::object barh_attr; + pybind11::object cla_attr; pybind11::object contour_attr; pybind11::object contourf_attr; pybind11::object errorbar_attr; @@ -315,6 +327,7 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper { pybind11::object hist_attr; pybind11::object hist2d_attr; pybind11::object invert_yaxis_attr; + pybind11::object imshow_attr; pybind11::object legend_attr; pybind11::object pcolormesh_attr; pybind11::object plot_attr; @@ -341,50 +354,58 @@ 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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)); +} + +// axvline +inline ObjectWrapper Axes::axvline(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = axvline_attr(*args, **kwargs); + return ObjectWrapper(std::move(ret)); } // bar -container::BarContainer Axes::bar(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline container::BarContainer Axes::bar(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = bar_attr(*args, **kwargs); return container::BarContainer(obj); } // bar_label -pybind11::object Axes::bar_label(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline 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"); @@ -393,29 +414,36 @@ pybind11::object Axes::bar_label(const pybind11::tuple &args, } // barh -container::BarContainer Axes::barh(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline container::BarContainer Axes::barh(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = barh_attr(*args, **kwargs); return container::BarContainer(obj); } -// contour -pybind11::object Axes::contour(const pybind11::tuple &args, +// cla +inline ObjectWrapper Axes::cla(const pybind11::tuple &args, const pybind11::dict &kwargs) { - pybind11::object obj = contour_attr(*args, **kwargs); - return obj; + pybind11::object ret = cla_attr(*args, **kwargs); + return ObjectWrapper(std::move(ret)); +} + +// contour +inline 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; +inline 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) { +inline 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); @@ -426,48 +454,48 @@ 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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 -std::tuple Axes::get_xlim() { +inline std::tuple Axes::get_xlim() { pybind11::list ret = get_xlim_attr(); double x1 = ret[0].cast(); double x2 = ret[1].cast(); @@ -475,7 +503,7 @@ std::tuple Axes::get_xlim() { } // get_xticklabels -std::vector Axes::get_xticklabels() { +inline std::vector Axes::get_xticklabels() { pybind11::list ret = get_xticklabels_attr(); std::vector texts; for (pybind11::size_t i = 0; i < ret.size(); ++i) { @@ -485,14 +513,15 @@ std::vector Axes::get_xticklabels() { } // grid -pybind11::object Axes::grid(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline 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 -std::tuple, std::vector, container::BarContainer> +inline std::tuple, std::vector, + container::BarContainer> Axes::hist(const pybind11::tuple &args, const pybind11::dict &kwargs) { pybind11::list ret = hist_attr(*args, **kwargs); // parse N, bins, patches @@ -509,200 +538,206 @@ Axes::hist(const pybind11::tuple &args, const pybind11::dict &kwargs) { } // hist2d -pybind11::object Axes::hist2d(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline 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) { +inline 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 +inline ObjectWrapper Axes::imshow(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = imshow_attr(*args, **kwargs); + return ObjectWrapper(std::move(ret)); } // legend -legend::Legend Axes::legend(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline legend::Legend Axes::legend(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = legend_attr(*args, **kwargs); return legend::Legend(obj); } // pcolormesh -collections::QuadMesh Axes::pcolormesh(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline collections::QuadMesh Axes::pcolormesh(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = pcolormesh_attr(*args, **kwargs); return collections::QuadMesh(ret); } // plot -pybind11::object Axes::plot(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline 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) { +inline 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) { +inline 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 -quiver::Quiver Axes::quiver(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline quiver::Quiver Axes::quiver(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = quiver_attr(*args, **kwargs); return quiver::Quiver(obj); } // quiverkey -quiver::QuiverKey Axes::quiverkey(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline quiver::QuiverKey Axes::quiverkey(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = quiverkey_attr(*args, **kwargs); return quiver::QuiverKey(obj); } // scatter -collections::PathCollection Axes::scatter(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline collections::PathCollection Axes::scatter(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = scatter_attr(*args, **kwargs); return collections::PathCollection(obj); } // set -pybind11::object Axes::set(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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 -Axes Axes::twinx(const pybind11::tuple &args, const pybind11::dict &kwargs) { +inline Axes Axes::twinx(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = twinx_attr(*args, **kwargs); return Axes(ret); } } // namespace matplotlibcpp17::axes - -#endif /* MATPLOTLIBCPP17_AXES_H */ diff --git a/include/matplotlibcpp17/cm.h b/include/matplotlibcpp17/cm.h index c436e5c..8b66760 100644 --- a/include/matplotlibcpp17/cm.h +++ b/include/matplotlibcpp17/cm.h @@ -2,26 +2,15 @@ * @file cm.h * @brief corresponding header for matplotlib.cm **/ - -#ifndef MATPLOTLIBCPP17_CM_H -#define MATPLOTLIBCPP17_CM_H +#pragma once #include namespace matplotlibcpp17::cm { -pybind11::object coolwarm() { - pybind11::object ret = - pybind11::module::import("matplotlib.cm").attr("coolwarm"); - return ret; -} - -pybind11::object PuBu_r() { - pybind11::object ret = - pybind11::module::import("matplotlib.cm").attr("PuBu_r"); - return ret; -} +// TODO: more colors +static const char *coolwarm = "coolwarm"; +static const char *PuBu_r = "PuBu_r"; +static const char *RdYlGn = "RdYlGn"; } // namespace matplotlibcpp17::cm - -#endif /* MATPLOTLIBCPP17_CM_H */ diff --git a/include/matplotlibcpp17/collections.h b/include/matplotlibcpp17/collections.h index e9de566..97762b6 100644 --- a/include/matplotlibcpp17/collections.h +++ b/include/matplotlibcpp17/collections.h @@ -2,9 +2,7 @@ * @file collections.h * @brief corresponding header for matplotlib.collections **/ - -#ifndef MATPLOTLIBCPP17_COLLECTIONS_H -#define MATPLOTLIBCPP17_COLLECTIONS_H +#pragma once #include @@ -19,13 +17,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 +39,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 +inline 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 +62,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) { +inline 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,9 +81,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 - -#endif /* MATPLOTLIBCPP17_COLLECTIONS_H */ diff --git a/include/matplotlibcpp17/common.h b/include/matplotlibcpp17/common.h index a1029f7..e39c1c4 100644 --- a/include/matplotlibcpp17/common.h +++ b/include/matplotlibcpp17/common.h @@ -1,5 +1,5 @@ -#ifndef MATPLOTLIBCPP17_COMMON_H -#define MATPLOTLIBCPP17_COMMON_H +#pragma once + #define LOAD_FUNC_ATTR(obj, mod) \ do { \ @@ -9,6 +9,7 @@ #define DECL_STRUCT_ATTR __attribute__((visibility("hidden"))) #include +#include #define INFO_MSG(msg) \ do { \ @@ -37,12 +38,19 @@ namespace matplotlibcpp17 { **/ struct DECL_STRUCT_ATTR BaseWrapper { public: - pybind11::object unwrap() { return self; } + pybind11::object unwrap() const { return self; } protected: pybind11::object self; }; -} // namespace matplotlibcpp17 +/** + * @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); } +}; -#endif /* MATPLOTLIBCPP17_COMMON_H */ +} // namespace matplotlibcpp17 diff --git a/include/matplotlibcpp17/container.h b/include/matplotlibcpp17/container.h index a795365..41f817b 100644 --- a/include/matplotlibcpp17/container.h +++ b/include/matplotlibcpp17/container.h @@ -2,9 +2,7 @@ * @file container.h * @brief corresponding header for matplotlib.axes **/ - -#ifndef MATPLOTLIBCPP17_CONTAINER_H -#define MATPLOTLIBCPP17_CONTAINER_H +#pragma once #include @@ -17,9 +15,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 - -#endif /* MATPLOTLIBCPP17_CONTAINER_H */ diff --git a/include/matplotlibcpp17/figure.h b/include/matplotlibcpp17/figure.h index 7135102..e0c422d 100644 --- a/include/matplotlibcpp17/figure.h +++ b/include/matplotlibcpp17/figure.h @@ -2,12 +2,10 @@ * @file figure.h * @brief corresponding header for matplotlib.figure **/ +#pragma once -#ifndef MATPLOTLIBCPP17_FIGURE_H -#define MATPLOTLIBCPP17_FIGURE_H - -#include #include +#include #include #include @@ -19,11 +17,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 +39,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() { @@ -81,60 +80,58 @@ struct DECL_STRUCT_ATTR Figure : public BaseWrapper { }; // add_axes -axes::Axes Figure::add_axes(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline axes::Axes Figure::add_axes(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = add_axes_attr(*args, **kwargs); return axes::Axes(obj); } // add_gridspec -gridspec::GridSpec Figure::add_gridspec(int nrow, int ncol, - const pybind11::dict &kwargs) { +inline gridspec::GridSpec Figure::add_gridspec(int nrow, int ncol, + const pybind11::dict &kwargs) { return gridspec::GridSpec(nrow, ncol, kwargs); } // add_subplot -axes::Axes Figure::add_subplot(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline axes::Axes Figure::add_subplot(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = add_subplot_attr(*args, **kwargs); return axes::Axes(obj); } // align_labels -pybind11::object Figure::align_labels(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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 - -#endif /* MATPLOTLIBCPP17_FIGURE_H */ diff --git a/include/matplotlibcpp17/gridspec.h b/include/matplotlibcpp17/gridspec.h index 3a9844a..2bef269 100644 --- a/include/matplotlibcpp17/gridspec.h +++ b/include/matplotlibcpp17/gridspec.h @@ -2,9 +2,7 @@ * @file gridspec.h * @brief corresponding header for matplotlib.axes **/ - -#ifndef MATPLOTLIBCPP17_GRIDSPEC_H -#define MATPLOTLIBCPP17_GRIDSPEC_H +#pragma once #include @@ -17,7 +15,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 +35,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: @@ -45,5 +44,3 @@ struct DECL_STRUCT_ATTR GridSpec : public BaseWrapper { }; } // namespace matplotlibcpp17::gridspec - -#endif /* MATPLOTLIBCPP17_GRIDSPEC_H */ diff --git a/include/matplotlibcpp17/legend.h b/include/matplotlibcpp17/legend.h index 6256a64..9076fa0 100644 --- a/include/matplotlibcpp17/legend.h +++ b/include/matplotlibcpp17/legend.h @@ -2,9 +2,7 @@ * @file legend.h * @brief corresponding header for matplotlib.legend **/ - -#ifndef MATPLOTLIBCPP17_LEGEND_H -#define MATPLOTLIBCPP17_LEGEND_H +#pragma once #include @@ -17,11 +15,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 - -#endif /* MATPLOTLIBCPP17_LEGEND_H */ diff --git a/include/matplotlibcpp17/mplot3d.h b/include/matplotlibcpp17/mplot3d.h new file mode 100644 index 0000000..f7d1f12 --- /dev/null +++ b/include/matplotlibcpp17/mplot3d.h @@ -0,0 +1,15 @@ +/** + * @file mplot3d.h + * @brief header file for mplot3d + **/ +#pragma once + +#include + +namespace matplotlibcpp17::mplot3d { + +pybind11::object import() { + return pybind11::module::import("mpl_toolkits.mplot3d").attr("Axes3D"); +} + +} // namespace matplotlibcpp17::mplot3d \ No newline at end of file diff --git a/include/matplotlibcpp17/patches.h b/include/matplotlibcpp17/patches.h index 83ac366..e3e838f 100644 --- a/include/matplotlibcpp17/patches.h +++ b/include/matplotlibcpp17/patches.h @@ -2,9 +2,7 @@ * @file pathces.h * @brief corresponding header for matplotlib.patches **/ - -#ifndef MATPLOTLIBCPP17_PATCHES_H -#define MATPLOTLIBCPP17_PATCHES_H +#pragma once #include @@ -91,5 +89,3 @@ struct DECL_STRUCT_ATTR Polygon : public BaseWrapper { }; } // namespace matplotlibcpp17::patches - -#endif /* MATPLOTLIBCPP17_PATCHES_H */ diff --git a/include/matplotlibcpp17/pyplot.h b/include/matplotlibcpp17/pyplot.h index 9e8da89..a3e1455 100644 --- a/include/matplotlibcpp17/pyplot.h +++ b/include/matplotlibcpp17/pyplot.h @@ -2,20 +2,18 @@ * @file pyplot.h * @brief corresponding header for matplotlib.pyplot **/ - -#ifndef MATPLOTLIBCPP17_PYPLOT_H -#define MATPLOTLIBCPP17_PYPLOT_H +#pragma once #include -#include #include +#include #include -#include #include -#include #include +#include +#include namespace matplotlibcpp17::pyplot { @@ -33,20 +31,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 + ObjectWrapper clim(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); + + // colorbar + ObjectWrapper colorbar(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); // errorbar - 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 @@ -65,41 +71,49 @@ struct DECL_STRUCT_ATTR PyPlot { figure::Figure gcf(const pybind11::tuple &args = pybind11::tuple(), const pybind11::dict &kwargs = pybind11::dict()); + // gci + ObjectWrapper gci(const pybind11::tuple &args = pybind11::tuple(), + const pybind11::dict &kwargs = pybind11::dict()); + // grid - 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 + 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()); @@ -112,24 +126,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() { @@ -137,12 +151,16 @@ struct DECL_STRUCT_ATTR PyPlot { LOAD_FUNC_ATTR(axis, mod); LOAD_FUNC_ATTR(cla, mod); LOAD_FUNC_ATTR(clf, mod); + LOAD_FUNC_ATTR(clim, mod); + LOAD_FUNC_ATTR(colorbar, mod); LOAD_FUNC_ATTR(errorbar, mod); LOAD_FUNC_ATTR(figaspect, mod); LOAD_FUNC_ATTR(figure, mod); LOAD_FUNC_ATTR(gca, mod); LOAD_FUNC_ATTR(gcf, mod); + LOAD_FUNC_ATTR(gci, mod); LOAD_FUNC_ATTR(grid, mod); + LOAD_FUNC_ATTR(imshow, mod); LOAD_FUNC_ATTR(legend, mod); LOAD_FUNC_ATTR(pause, mod); LOAD_FUNC_ATTR(plot, mod); @@ -164,12 +182,16 @@ struct DECL_STRUCT_ATTR PyPlot { pybind11::object axis_attr; pybind11::object cla_attr; pybind11::object clf_attr; + pybind11::object clim_attr; + pybind11::object colorbar_attr; pybind11::object errorbar_attr; pybind11::object figaspect_attr; pybind11::object figure_attr; pybind11::object gca_attr; pybind11::object gcf_attr; + pybind11::object gci_attr; pybind11::object grid_attr; + pybind11::object imshow_attr; pybind11::object legend_attr; pybind11::object pause_attr; pybind11::object plot_attr; @@ -188,42 +210,56 @@ struct DECL_STRUCT_ATTR PyPlot { }; // axes -axes::Axes PyPlot::axes(const pybind11::dict &kwargs) { +inline axes::Axes PyPlot::axes(const pybind11::dict &kwargs) { pybind11::object ax_obj = axes_attr(**kwargs); return axes::Axes(ax_obj); } // axis -pybind11::object PyPlot::axis(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline 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) { +inline 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) { +inline 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)); } -// errorbar -pybind11::object PyPlot::errorbar(const pybind11::tuple &args, +// clim +inline ObjectWrapper PyPlot::clim(const pybind11::tuple &args, const pybind11::dict &kwargs) { + pybind11::object ret = clim_attr(*args, **kwargs); + return ObjectWrapper(std::move(ret)); +} + +// colorbar +inline ObjectWrapper PyPlot::colorbar(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object ret = colorbar_attr(*args, **kwargs); + return ObjectWrapper(std::move(ret)); +} + +// errorbar +inline ObjectWrapper PyPlot::errorbar(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object ret = errorbar_attr(*args, **kwargs); - return ret; + return ObjectWrapper(std::move(ret)); } // figaspect -std::tuple PyPlot::figaspect(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline std::tuple +PyPlot::figaspect(const pybind11::tuple &args, const pybind11::dict &kwargs) { pybind11::list l = figaspect_attr(*args, **kwargs); double width = l[0].cast(); double height = l[1].cast(); @@ -231,101 +267,115 @@ std::tuple PyPlot::figaspect(const pybind11::tuple &args, } // figure -figure::Figure PyPlot::figure(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline figure::Figure PyPlot::figure(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object fig_obj = figure_attr(*args, **kwargs); return figure::Figure(fig_obj); } // gca -axes::Axes PyPlot::gca(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline axes::Axes PyPlot::gca(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = gca_attr(*args, **kwargs); return axes::Axes(obj); } // gcf -figure::Figure PyPlot::gcf(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline figure::Figure PyPlot::gcf(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = gcf_attr(*args, **kwargs); return figure::Figure(obj); } +// gci +inline ObjectWrapper PyPlot::gci(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object obj = gci_attr(*args, **kwargs); + return obj; +} + // grid -pybind11::object PyPlot::grid(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline ObjectWrapper PyPlot::grid(const pybind11::tuple &args, + const pybind11::dict &kwargs) { pybind11::object obj = grid_attr(*args, **kwargs); return obj; } +// imshow +inline ObjectWrapper PyPlot::imshow(const pybind11::tuple &args, + const pybind11::dict &kwargs) { + pybind11::object obj = imshow_attr(*args, **kwargs); + return obj; +} + // legend -pybind11::object PyPlot::legend(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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 -axes::Axes PyPlot::subplot(const pybind11::dict &kwargs) { +inline axes::Axes PyPlot::subplot(const pybind11::dict &kwargs) { return axes::Axes(subplot_attr(**kwargs)); } -axes::Axes PyPlot::subplot(int cri) { +inline axes::Axes PyPlot::subplot(int cri) { pybind11::object obj = subplot_attr(cri); return axes::Axes(obj); } // subplots -std::tuple +inline std::tuple PyPlot::subplots(const pybind11::dict &kwargs) { pybind11::list ret = subplots_attr(**kwargs); pybind11::object fig = ret[0]; @@ -333,7 +383,7 @@ PyPlot::subplots(const pybind11::dict &kwargs) { return {figure::Figure(fig), axes::Axes(ax)}; } -std::tuple> +inline std::tuple> PyPlot::subplots(int r, int c, const pybind11::dict &kwargs) { // subplots() returns [][] (if r > 1 && c > 1) else [] // return []axes in row-major @@ -364,41 +414,41 @@ PyPlot::subplots(int r, int c, const pybind11::dict &kwargs) { } // title -pybind11::object PyPlot::title(const pybind11::tuple &args, - const pybind11::dict &kwargs) { +inline 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) { +inline 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) { +inline 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) { +inline 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) { +inline 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() { +inline PyPlot import() { auto mod = pybind11::module::import("matplotlib.pyplot"); auto g_pyplot = PyPlot(mod); return g_pyplot; @@ -407,7 +457,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)...); } @@ -416,5 +466,3 @@ using Kwargs = pybind11::dict; // Export this namespace py = pybind11; using namespace py::literals; - -#endif /* MATPLOTLIBCPP17_PYPLOT_H */ diff --git a/include/matplotlibcpp17/quiver.h b/include/matplotlibcpp17/quiver.h index 5ba4462..caab9cd 100644 --- a/include/matplotlibcpp17/quiver.h +++ b/include/matplotlibcpp17/quiver.h @@ -2,9 +2,7 @@ * @file quiver.h * @brief corresponding header for matplotlib.quiver **/ - -#ifndef MATPLOTLIBCPP17_QUIVER_H -#define MATPLOTLIBCPP17_QUIVER_H +#pragma once #include @@ -17,16 +15,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 - -#endif /* MATPLOTLIBCPP17_QUIVER_H */ diff --git a/include/matplotlibcpp17/text.h b/include/matplotlibcpp17/text.h index 16ccec5..b8d002b 100644 --- a/include/matplotlibcpp17/text.h +++ b/include/matplotlibcpp17/text.h @@ -2,9 +2,7 @@ * @file text.h * @brief corresponding header for matplotlib.text **/ - -#ifndef MATPLOTLIBCPP17_TEXT_H -#define MATPLOTLIBCPP17_TEXT_H +#pragma once #include @@ -17,26 +15,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) { +inline 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 - -#endif /* MATPLOTLIBCPP17_TEXT_H */ diff --git a/include/matplotlibcpp17/ticker.h b/include/matplotlibcpp17/ticker.h index 7051ccd..b9d024e 100644 --- a/include/matplotlibcpp17/ticker.h +++ b/include/matplotlibcpp17/ticker.h @@ -2,9 +2,7 @@ * @file ticker.h * @brief corresponding header for matplotlib.ticker **/ - -#ifndef MATPLOTLIBCPP17_TICKER_H -#define MATPLOTLIBCPP17_TICKER_H +#pragma once #include @@ -22,5 +20,3 @@ struct DECL_STRUCT_ATTR LogLocator : public BaseWrapper { }; } // namespace matplotlibcpp17::ticker - -#endif /* MATPLOTLIBCPP17_ANIMATION_H */