Skip to content
Snippets Groups Projects
Commit b51db019 authored by soblin's avatar soblin
Browse files

added image_demo demo


Signed-off-by: default avatarsoblin <hilo.soblin@gmail.com>
parent d29f1fb5
Branches
No related tags found
No related merge requests found
......@@ -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<Figure, Axes>`.
......@@ -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<vector<double>> 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<Figure, vector<Axes>>`.
......@@ -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,7 @@ From [gallery/images_contours_and_fields/quiver_demo.cpp](https://github.com/sob
![quiver_demo3](./gallery/images/quiver_demo_3.png)
### example5 - gif
### gif
Currently only `ArtistAnimation` is supported. `FuncAnimation` interface maybe implemented in the future.
......
gallery/images/image_demo.png

37.8 KiB

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")
// example from
// https://matplotlib.org/stable/gallery/images_contours_and_fields/image_demo.html
#include <matplotlibcpp17/pyplot.h>
#include <xtensor/xbuilder.hpp>
#include <xtensor/xview.hpp>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
using namespace matplotlibcpp17;
using mesh2D = vector<vector<double>>;
int main() {
const double delta = 0.025;
const auto x = xt::arange<double>(-3.0, 3.0, delta);
auto [X_, Y_] = xt::meshgrid(x, x);
auto Z1_ = xt::exp(-xt::pow(X_, 2) - xt::pow(Y_, 2));
auto Z2_ = xt::exp(-xt::pow(X_ - 1, 2) - xt::pow(Y_ - 1, 2));
auto Z_ = (Z1_ - Z2_) * 2.0;
// to vector
vector<double> X(X_.begin(), X_.end()), Y(Y_.begin(), Y_.end()),
Z1(Z1_.begin(), Z1_.end()), Z2(Z2_.begin(), Z2_.end());
// to vector<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());
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));
#if USE_GUI
plt.show();
#else
plt.savefig(Args("image_demo.png"));
#endif
return 0;
}
......@@ -118,6 +118,10 @@ public:
pybind11::object hist2d(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// imshow
pybind11::object imshow(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// invert_yaxis
pybind11::object
invert_yaxis(const pybind11::tuple &args = pybind11::tuple(),
......@@ -260,10 +264,12 @@ private:
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.
// 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);
......@@ -315,6 +321,7 @@ private:
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;
......@@ -522,6 +529,13 @@ pybind11::object Axes::invert_yaxis(const pybind11::tuple &args,
return ret;
}
// imshow
pybind11::object Axes::imshow(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = imshow_attr(*args, **kwargs);
return ret;
}
// legend
legend::Legend Axes::legend(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment