Skip to main content
Sign in
Snippets Groups Projects
Commit 99bcb0ce authored by soblin's avatar soblin
Browse files

TODO: LinearLocator

parent 831f1ec4
No related branches found
No related tags found
No related merge requests found
......@@ -3,14 +3,16 @@ add_demo(lorenz_attractor lorenz_attractor.cpp)
add_demo(contour3d contour3d.cpp)
add_demo(subplot3d subplot3d.cpp)
add_demo(errorbar3d errorbar3d.cpp)
add_demo(surface3d surface3d)
add_custom_target(mplot3d
DEPENDS lines3d lorenz_attractor contour3d subplot3d errorbar3d
DEPENDS lines3d lorenz_attractor contour3d subplot3d errorbar3d surface3d
COMMAND lines3d
COMMAND lorenz_attractor
COMMAND contour3d
COMMAND subplot3d
COMMAND errorbar3d
COMMAND surface3d
COMMENT "running mplot3d"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../images"
)
// example from https://matplotlib.org/stable/gallery/mplot3d/surface3d.html
#include <pybind11/embed.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
#include <matplotlibcpp17/pyplot.h>
#include <matplotlibcpp17/cm.h>
#include <xtensor/xbuilder.hpp>
#include <xtensor/xmath.hpp>
#include <vector>
namespace py = pybind11;
using namespace py::literals;
using namespace std;
using namespace matplotlibcpp17;
using mesh2D = vector<vector<double>>;
int main() {
py::scoped_interpreter guard{};
auto plt = matplotlibcpp17::pyplot::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);
// 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);
}
}
// to numpy array (vector<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()));
ax.set_zlim(Args(-1.01, 1.01));
fig.colorbar(Args(surf), Kwargs("shrink"_a = 0.5, "aspect"_a = 5));
plt.show();
}
/**
* @file cm.h
* @brief corresponding header for matplotlib.animation
* @brief corresponding header for matplotlib.cm
**/
#ifndef MATPLOTLIBCPP17_CM_H
......@@ -8,8 +8,6 @@
#include <pybind11/pybind11.h>
#include <matplotlibcpp17/common.h>
namespace matplotlibcpp17::cm {
pybind11::object coolwarm() {
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment