forked from soblin/matplotlibcpp17
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubplot3d.cpp
More file actions
97 lines (90 loc) · 3.06 KB
/
Copy pathsubplot3d.cpp
File metadata and controls
97 lines (90 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// example https://matplotlib.org/stable/gallery/mplot3d/subplot3d.html
#include <matplotlibcpp17/pyplot.h>
#include <matplotlibcpp17/cm.h>
#include <xtensor/xbuilder.hpp>
#include <xtensor/xmath.hpp>
#include <vector>
using namespace std;
using namespace matplotlibcpp17;
// NOTE: looks like vector<vector<T>> is naturally converted thanks to pybind11
// ref: https://github.com/pybind/pybind11/issues/1071
using mesh2D = vector<vector<double>>;
// from mpl_toolkits.axes3d.py
tuple<mesh2D, mesh2D, mesh2D> 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 =
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 int szx = xs.shape()[0], szy = ys.shape()[0];
mesh2D X_(szx), Y_(szx), Z_(szx);
for (int i = 0; i < szx; ++i) {
X_[i].resize(szy);
Y_[i].resize(szy);
Z_[i].resize(szy);
for (int j = 0; j < szy; ++j) {
X_[i][j] = X(i, j);
Y_[i][j] = Y(i, j);
Z_[i][j] = Z(i, j);
}
}
return {X_, Y_, Z_};
}
int main() {
py::scoped_interpreter guard{};
auto plt = matplotlibcpp17::pyplot::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);
// to vector<vector>
const int sz = xs.shape()[0];
mesh2D X(sz), Y(sz), Z(sz);
for (int i = 0; i < sz; ++i) {
X[i].resize(sz);
Y[i].resize(sz);
Z[i].resize(sz);
for (int j = 0; j < sz; ++j) {
X[i][j] = X0(i, j);
Y[i][j] = Y0(i, j);
Z[i][j] = Z0(i, j);
}
}
// 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)));
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));
fig.colorbar(Args(surf), 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)));
ax.plot_wireframe(Args(X_, Y_, Z_),
Kwargs("rstride"_a = 10, "cstride"_a = 10));
}
#if USE_GUI
plt.show();
#else
plt.savefig(Args("subplot3d.png"));
#endif
}