Skip to content

Commit 99bcb0c

Browse files
committed
TODO: LinearLocator
1 parent 831f1ec commit 99bcb0c

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

gallery/mplot3d/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ add_demo(lorenz_attractor lorenz_attractor.cpp)
33
add_demo(contour3d contour3d.cpp)
44
add_demo(subplot3d subplot3d.cpp)
55
add_demo(errorbar3d errorbar3d.cpp)
6+
add_demo(surface3d surface3d)
67

78
add_custom_target(mplot3d
8-
DEPENDS lines3d lorenz_attractor contour3d subplot3d errorbar3d
9+
DEPENDS lines3d lorenz_attractor contour3d subplot3d errorbar3d surface3d
910
COMMAND lines3d
1011
COMMAND lorenz_attractor
1112
COMMAND contour3d
1213
COMMAND subplot3d
1314
COMMAND errorbar3d
15+
COMMAND surface3d
1416
COMMENT "running mplot3d"
1517
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../images"
1618
)

gallery/mplot3d/surface3d.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// example from https://matplotlib.org/stable/gallery/mplot3d/surface3d.html
2+
3+
#include <pybind11/embed.h>
4+
#include <pybind11/stl.h>
5+
#include <pybind11/numpy.h>
6+
7+
#include <matplotlibcpp17/pyplot.h>
8+
#include <matplotlibcpp17/cm.h>
9+
10+
#include <xtensor/xbuilder.hpp>
11+
#include <xtensor/xmath.hpp>
12+
13+
#include <vector>
14+
15+
namespace py = pybind11;
16+
using namespace py::literals;
17+
using namespace std;
18+
using namespace matplotlibcpp17;
19+
20+
using mesh2D = vector<vector<double>>;
21+
22+
int main() {
23+
py::scoped_interpreter guard{};
24+
auto plt = matplotlibcpp17::pyplot::import();
25+
auto [fig, ax] =
26+
plt.subplots(Kwargs("subplot_kw"_a = py::dict("projection"_a = "3d")));
27+
28+
auto xs = xt::arange(-5.0, 5.0, 0.25);
29+
auto [X0, Y0] = xt::meshgrid(xs, xs);
30+
auto R0 = xt::sqrt(xt::pow(X0, 2) + xt::pow(Y0, 2));
31+
auto Z0 = xt::sin(R0);
32+
// to vector<vector>
33+
const int sz = xs.shape()[0];
34+
mesh2D X(sz), Y(sz), Z(sz);
35+
for (int i = 0; i < sz; ++i) {
36+
X[i].resize(sz);
37+
Y[i].resize(sz);
38+
Z[i].resize(sz);
39+
for (int j = 0; j < sz; ++j) {
40+
X[i][j] = X0(i, j);
41+
Y[i][j] = Y0(i, j);
42+
Z[i][j] = Z0(i, j);
43+
}
44+
}
45+
// to numpy array (vector<vector> is converted to list of list)
46+
auto X_ = py::array(py::cast(std::move(X)));
47+
auto Y_ = py::array(py::cast(std::move(Y)));
48+
auto Z_ = py::array(py::cast(std::move(Z)));
49+
auto surf = ax.plot_surface(Args(X_, Y_, Z_),
50+
Kwargs("rstride"_a = 1, "cstride"_a = 1,
51+
"linewidth"_a = 0, "antialiased"_a = false,
52+
"cmap"_a = cm::coolwarm()));
53+
ax.set_zlim(Args(-1.01, 1.01));
54+
fig.colorbar(Args(surf), Kwargs("shrink"_a = 0.5, "aspect"_a = 5));
55+
plt.show();
56+
}

include/matplotlibcpp17/cm.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
/**
22
* @file cm.h
3-
* @brief corresponding header for matplotlib.animation
3+
* @brief corresponding header for matplotlib.cm
44
**/
55

66
#ifndef MATPLOTLIBCPP17_CM_H
77
#define MATPLOTLIBCPP17_CM_H
88

99
#include <pybind11/pybind11.h>
1010

11-
#include <matplotlibcpp17/common.h>
12-
1311
namespace matplotlibcpp17::cm {
1412

1513
pybind11::object coolwarm() {

0 commit comments

Comments
 (0)